How to show a pop up message with dark background

感情迁移 提交于 2019-12-22 04:01:43

问题


I'm going to show a error message in my program .

I'd like my messages to be like image below,

as you can see, I need a dark shadow to be shown when message pops up .

I follow two solution to do this :

1 - I take a screenshot of program (and I mix it with a black color, to be bit a dark) and then I attach it to a panel and I show the panel and then message form pops up . it doesn't work because sometimes it takes screenshot from other programs that is on the screen (for example telegram notifications)

2 - I use a rich panel (that I got from internet) that it can have opacity property, and then I set panel's color to black and opacity to 0.5. then I pop up the message. it doesn't work because this new panel does not cover all elements (IDK why !)

both of this solutions had some problems and did not work . Is there any solution to show such this messages ?

im using win forms not WPF

this my riched panel :

public class ExtendedPanel : Panel
{
    private const int WS_EX_TRANSPARENT = 0x20;
    public ExtendedPanel()
    {
        SetStyle(ControlStyles.Opaque, true);
    }

    private int opacity = 50;
    [DefaultValue(50)]
    public int Opacity
    {
        get
        {
            return this.opacity;
        }
        set
        {
            if (value < 0 || value > 100)
                throw new ArgumentException("value must be between 0 and 100");
            this.opacity = value;
        }
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
            return cp;
        }
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        using (var brush = new SolidBrush(Color.FromArgb(this.opacity * 255 / 100, this.BackColor)))
        {
            e.Graphics.FillRectangle(brush, this.ClientRectangle);
        }
        base.OnPaint(e);
    }
}

this is how i use it :

using (ExtendedPanel p = new ExtendedPanel())
{
  p.Location = new Point(0, 0);
  p.Size = f.ClientRectangle.Size;
  p.BackgroundImage = bmp;
  e.f.Controls.Add(p);
  //e.p = p;
  p.BringToFront();
  e.StartPosition = FormStartPosition.CenterParent;
  e.lblTitr.Text = header;
  e.lblText.Text = text;
  e.ShowDialog(f);
  // p.Visible = false;
  // e.f.Controls.Remove(p);
  e.f.Controls.Remove(p);
  e.f.Refresh();

}


回答1:


You can use two Forms to achieve the effect:

private void button_Click(object sender, EventArgs e)
{
    Enabled = false;
    Form shadow = new Form();
    shadow.MinimizeBox = false;
    shadow.MaximizeBox = false;
    shadow.ControlBox = false;

    shadow.Text = "";
    shadow.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    shadow.Size = Size;
    shadow.BackColor = Color.Black;
    shadow.Opacity = 0.3;
    shadow.Show();
    shadow.Location = Location;
    shadow.Enabled = false;

    // here you should use your own messageBox class!
    Form msg = new Form();
    Button btn = new Button() { Text = "OK"};
    btn.Click += (ss, ee) => { msg.Close(); };
    msg.Controls.Add(btn);
    msg.FormClosed += (ss, ee) => { shadow.Close(); Enabled = true; };

    msg.Size = new System.Drawing.Size(200, 100);
    msg.StartPosition = FormStartPosition.Manual;
    msg.Location = new Point(shadow.Left + (shadow.Width - msg.Width) / 2, 
                             shadow.Top + (shadow.Height - msg.Height) / 2);
    msg.ShowDialog();
}

Most of the things I do to msg should go into your own custom messagebox form class, of course..

One problem is that the shadow form shows in the task bar, but you can set ShowInTaskbar to false..




回答2:


This is not a trivial task. The best way to do it is to create your own custom control. You can use the attached project as a basis although it is doing the job it would be much better if it inherits from Control and not UserControl directly :)

Download Project

You'll need 7zip to decompress it.

Good luck



来源:https://stackoverflow.com/questions/34947504/how-to-show-a-pop-up-message-with-dark-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!