如何显示深色背景的弹出消息 [英] How to show a pop up message with dark background
问题描述
我将在程序中显示错误消息.
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 .
我遵循两种解决方案:
1-我截取了程序的屏幕截图(我将其与黑色混合,显得有点暗),然后将其附加到面板上,然后显示该面板,然后弹出消息表单.它不起作用,因为有时它会从屏幕上的其他程序中获取屏幕截图(例如电报通知)
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-我使用一个丰富的面板(来自互联网),它可以具有不透明度属性,然后将面板的颜色设置为黑色,将不透明度设置为0.5.然后我弹出消息. 它不起作用,因为此新面板未涵盖所有元素(IDK为什么!)
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 ?
我使用获胜表格而不是WPF
im using win forms not WPF
这是我的丰富面板:
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);
}
}
这就是我的使用方式:
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();
}
推荐答案
您可以使用两个 Forms
达到效果:
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();
}
我对msg
所做的大部分事情都应该放在您自己的自定义消息框表单类中..
Most of the things I do to msg
should go into your own custom messagebox form class, of course..
一个问题是shadow
表单显示在任务栏中,但是您可以将ShowInTaskbar
设置为false.
One problem is that the shadow
form shows in the task bar, but you can set ShowInTaskbar
to false..
这篇关于如何显示深色背景的弹出消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!