C#窗体右下角弹框
1. 弹框窗体
1.1 属性
- 窗体属性
- AtuoScaleMode: None
- FormBorderStyle: None 无边框
- pictureBox 属性
- BackColor: Highlight
- SizeMode: Zoom
- Button 属性
- FlatStyle: Flat
- ForeColor: White
- Image:图片
- ImageAlign: MiddleCenter
1.2 代码
private Form_Alert.enmAction action;
private int x, y;
public enum enmAction
{
Wait,
Start,
Close
}
public enum enmType
{
Success,
Warning,
Error,
Info
}
public void showAlert(string msg, enmType type)
{
this.Opacity = 0.0;
this.StartPosition = FormStartPosition.Manual;
string fname;
for (int i = 1; i < 10; i++)
{
fname = "alert" + i.ToString();
Form_Alert frm = (Form_Alert)Application.OpenForms[fname];
if (frm == null)
{
this.Name = fname;
this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width + 15;
this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
this.Location = new Point(this.x, this.y);
break;
}
}
this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width - 5;
switch (type)
{
case enmType.Success:
this.pictureBox1.Image = Resources.success;
this.BackColor = Color.SeaGreen;
break;
case enmType.Error:
this.pictureBox1.Image = Resources.error;
this.BackColor = Color.DarkRed;
break;
case enmType.Info:
this.pictureBox1.Image = Resources.info;
this.BackColor = Color.RoyalBlue;
break;
case enmType.Warning:
this.pictureBox1.Image = Resources.warning;
this.BackColor = Color.DarkOrange;
break;
}
this.LblMsg.Text = msg;
this.Show();
this.action = enmAction.Start;
this.timer1.Interval = 1;
timer1.Start();
}
private void Button1_Click(object sender, EventArgs e)
{
timer1.Interval = 1;
action = enmAction.Close;
}
private void Timer1_Tick(object sender, EventArgs e)
{
switch (this.action)
{
case enmAction.Wait:
timer1.Interval = 5000;
action = enmAction.Close;
break;
case enmAction.Start:
timer1.Interval = 1;
this.Opacity += 0.1;
if (this.x < this.Location.X)
{
this.Left--;
}
else
{
if (this.Opacity == 1.0)
{
action = enmAction.Wait;
}
}
break;
case enmAction.Close:
this.timer1.Interval = 1;
this.Opacity -= 0.1;
this.Left -= 3;
if (base.Opacity == 0.0)
{
base.Close();
}
break;
}
}
2. 父窗体
属性
序号 | 名称 | 颜色 |
---|
1 | Success | SeaGreen |
2 | Error | DarRed |
3 | Warning | DarkOrange |
4 | Info | RoyalBlue |
代码
- 弹窗显示部分代码
public void Alert(string msg,Form_Alert.enmType type)
{
Form_Alert frm = new Form_Alert();
frm.showAlert(msg,type);
}
- 按钮调用部分
private void Button1_Click(object sender, EventArgs e)
{
this.Alert("Success Alert", Form_Alert.enmType.Success);
}
private void Button2_Click(object sender, EventArgs e)
{
this.Alert("Warning Alert", Form_Alert.enmType.Warning);
}
private void Button3_Click(object sender, EventArgs e)
{
this.Alert("Error Alert", Form_Alert.enmType.Error);
}
private void Button4_Click(object sender, EventArgs e)
{
this.Alert("Info Alert", Form_Alert.enmType.Info);
}
弹框效果