服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - WindowsForm实现警告消息框的实例代码

WindowsForm实现警告消息框的实例代码

2022-09-27 13:59zhuanghamiao C#

这篇文章主要介绍了WindowsForm如何实现警告消息框,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下

警告消息框主要是用来向用户户展示诸如警告、异常、完成和提示消息。一般实现的效果就是从系统窗口右下角弹出,然后加上些简单的显示和消失的动画。

WindowsForm实现警告消息框的实例代码

创建警告框窗口

首先我们创建一个警告框窗口(Form),将窗口设置为无边框(FormBoderStyle=None),添加上图片和内容显示控件

WindowsForm实现警告消息框的实例代码

创建好警告框后,我们先让他能够从窗口右下角显示出来,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public partial class AlertMessageForm : Form
{
  public AlertMessageForm()
  {
    InitializeComponent();
  }
 
  private int x, y;
 
  public void Show(string message)
  {
    this.StartPosition = FormStartPosition.Manual;
    this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
    this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
    this.Location = new Point(x, y);
    labelContent.Text = message;
    this.Show();
  }
}

警告框显示和关闭动画

添加一个计时器,通过时钟控制窗口背景渐入和淡出

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 警告框的行为(显示,停留,退出)
public enum AlertFormAction
{
  Start,
  Wait,
  Close
}
 
public partial class AlertMessageForm : Form
{
  public AlertMessageForm()
  {
    InitializeComponent();
  }
 
  private int x, y;
  private AlertFormAction action;
 
  private void timer1_Tick(object sender, EventArgs e)
  {
    switch (action)
    {
      case AlertFormAction.Start:
        timer1.Interval = 50;//警告显示的时间
        this.Opacity += 0.1;
        if (this.Opacity == 1.0)
        {
          action = AlertFormAction.Wait;
        }
        break;
      case AlertFormAction.Wait:
        timer1.Interval = 3000;//警告框停留时间
        action = AlertFormAction.Close;
        break;
      case AlertFormAction.Close:
        timer1.Interval = 50;//警告退出的时间
        this.Opacity -= 0.1;
        if (this.Opacity == 0.0)
        {
          this.Close();
        }
        break;
      default:
        break;
    }
  }
 
  public void Show(string message)
  {
    //设置窗口启始位置
    this.StartPosition = FormStartPosition.Manual;
    this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
    this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
    this.Location = new Point(x, y);
 
    labelContent.Text = message;
    this.Opacity = 0.0;
 
    this.Show();
 
    action = AlertFormAction.Start;
    //启动时钟
    timer1.Start();
  }
}

WindowsForm实现警告消息框的实例代码

处理多种不同类型的警告框

添加AlertType枚举,让警告框显示不同类型的消息,根据消息类型变换不同的消息主题颜色,并未Show方法添加警告框类型参数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public enum AlertType
 {
   Info,
   Success,
   Warning,
   Error
 }
 
// 设置警告框主题
private void SetAlertTheme(AlertType type)
{
  switch (type)
  {
    case AlertType.Info:
      this.pictureBox1.Image = Properties.Resources.info;
      this.BackColor = Color.RoyalBlue;
      break;
    case AlertType.Success:
      this.pictureBox1.Image = Properties.Resources.success;
      this.BackColor = Color.SeaGreen;
      break;
    case AlertType.Warning:
      this.pictureBox1.Image = Properties.Resources.warning;
      this.BackColor = Color.DarkOrange;
      break;
    case AlertType.Error:
      this.pictureBox1.Image = Properties.Resources.error;
      this.BackColor = Color.DarkRed;
      break;
    default:
      break;
  }
}
 
// 显示警告框
public void Show(string message, AlertType type){
 // ...
 SetAlertTheme(type);
}

WindowsForm实现警告消息框的实例代码

处理多个警告框重叠问题

当然,完成上面的处理是不够的,当有多个消息的时候,消息框会重叠在一起;多个消息时,需要将消息窗口按一定的规则排列,这里我们设置每个消息窗口间隔一定的距离

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public void Show(string message, AlertType type)
{
  // 设置窗口启始位置
  this.StartPosition = FormStartPosition.Manual;
 
  // 设置程序每个打开的消息窗口的位置,超过10个就不做处理,这个可以根据自己的需求设定
  string fname;
  for (int i = 1; i < 10; i++)
  {
    fname = "alert" + i.ToString();
    AlertMessageForm alert = (AlertMessageForm)Application.OpenForms[fname];
    if (alert == null)
    {
      this.Name = fname;
      this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
      this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
      this.Location = new Point(x, y);
      break;
    }
  }
 
  labelContent.Text = message;
  this.Opacity = 0.0;
  SetAlertTheme(type);
  this.Show();
 
  action = AlertFormAction.Start;
  //启动时钟
  timer1.Start();
}

鼠标悬停警告框处理

想要警告框停留的时间长一些,一中方式是直接设置警告框停留的时间长一些,另一种方式是通过判断鼠标在警告框窗口是否悬停,所以可以通过鼠标的悬停和离开事件进行处理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
private void AlertMessageForm_MouseMove(object sender, MouseEventArgs e)
{
  this.Opacity = 1.0;
  timer1.Interval = int.MaxValue;//警告框停留时间
  action = AlertFormAction.Close;
}
 
private void AlertMessageForm_MouseLeave(object sender, EventArgs e)
{
  this.Opacity = 1.0;
  timer1.Interval = 3000;//警告框停留时间
  action = AlertFormAction.Close;
}

WindowsForm实现警告消息框的实例代码

警告框的完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
public enum AlertType
{
  Info,
  Success,
  Warning,
  Error
}
 
public enum AlertFormAction
{
  Start,
  Wait,
  Close
}
 
public partial class AlertMessageForm : Form
{
  public AlertMessageForm()
  {
    InitializeComponent();
  }
 
  private int x, y;
  private AlertFormAction action;
 
  private void timer1_Tick(object sender, EventArgs e)
  {
    switch (action)
    {
      case AlertFormAction.Start:
        timer1.Interval = 50;//警告显示的时间
        this.Opacity += 0.1;
        if (this.Opacity == 1.0)
        {
          action = AlertFormAction.Wait;
        }
        break;
      case AlertFormAction.Wait:
        timer1.Interval = 3000;//警告框停留时间
        action = AlertFormAction.Close;
        break;
      case AlertFormAction.Close:
        timer1.Interval = 50;//警告关闭的时间
        this.Opacity -= 0.1;
        if (this.Opacity == 0.0)
        {
          this.Close();
        }
        break;
      default:
        break;
    }
  }
 
  public void Show(string message, AlertType type)
  {
    // 设置窗口启始位置
    this.StartPosition = FormStartPosition.Manual;
 
    // 设置程序每个打开的消息窗口的位置,超过10个就不做处理,这个可以根据自己的需求设定
    string fname;
    for (int i = 1; i < 10; i++)
    {
      fname = "alert" + i.ToString();
      AlertMessageForm alert = (AlertMessageForm)Application.OpenForms[fname];
      if (alert == null)
      {
        this.Name = fname;
        this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
        this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
        this.Location = new Point(x, y);
        break;
      }
    }
 
    labelContent.Text = message;
    this.Opacity = 0.0;
    SetAlertTheme(type);
    this.Show();
 
    action = AlertFormAction.Start;
    //启动时钟
    timer1.Start();
  }
 
  private void AlertMessageForm_MouseMove(object sender, MouseEventArgs e)
  {
    this.Opacity = 1.0;
    timer1.Interval = int.MaxValue;//警告框停留时间
    action = AlertFormAction.Close;
  }
 
  private void AlertMessageForm_MouseLeave(object sender, EventArgs e)
  {
    this.Opacity = 1.0;
    timer1.Interval = 3000;//警告框停留时间
    action = AlertFormAction.Close;
  }
 
  private void buttonClose_Click(object sender, EventArgs e)
  {
    // 注销鼠标事件
    this.MouseLeave-= new System.EventHandler(this.AlertMessageForm_MouseLeave);
    this.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.AlertMessageForm_MouseMove);
 
    timer1.Interval = 50;//警告关闭的时间
    this.Opacity -= 0.1;
    if (this.Opacity == 0.0)
    {
      this.Close();
    }
  }
 
  // 设置警告框主题
  private void SetAlertTheme(AlertType type)
  {
    switch (type)
    {
      case AlertType.Info:
        this.pictureBox1.Image = Properties.Resources.info;
        this.BackColor = Color.RoyalBlue;
        break;
      case AlertType.Success:
        this.pictureBox1.Image = Properties.Resources.success;
        this.BackColor = Color.SeaGreen;
        break;
      case AlertType.Warning:
        this.pictureBox1.Image = Properties.Resources.warning;
        this.BackColor = Color.DarkOrange;
        break;
      case AlertType.Error:
        this.pictureBox1.Image = Properties.Resources.error;
        this.BackColor = Color.DarkRed;
        break;
      default:
        break;
    }
  }
}

以上就是WindowsForm实现警告消息框的实例代码的详细内容,更多关于WindowsForm实现警告消息框的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/zhuanghamiao/p/winform-alert.html

延伸 · 阅读

精彩推荐
  • C#Unity常用命令模式详解

    Unity常用命令模式详解

    这篇文章主要为大家详细介绍了Unity常用命令模式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    探求虚无10212022-07-11
  • C#C# 灵活使用类的方法

    C# 灵活使用类的方法

    本文主要介绍了C# 灵活使用类的方法,具有很好的参考价值,下面跟着小编一起来看下吧...

    郜飞4682021-12-27
  • C#Winform让DataGridView左侧显示图片

    Winform让DataGridView左侧显示图片

    本文主要介绍在如何让DataGridView左侧显示图片,这里主要讲解重写DataGridView的OnRowPostPaint方法,需要的朋友可以参考下。...

    秦风5142021-11-22
  • C#C#实现用户自定义控件中嵌入自己的图标

    C#实现用户自定义控件中嵌入自己的图标

    这篇文章主要介绍了C#实现用户自定义控件中嵌入自己的图标,较为详细的分析了C#实现自定义控件中嵌入图标的具体步骤与相关实现技巧,需要的朋友可以参...

    Microblue8692021-11-15
  • C#C#微信分享代码

    C#微信分享代码

    这篇文章主要为大家详细介绍了C#微信分享的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    阿炬9282022-01-25
  • C#C#发送HttpPost请求来调用WebService的方法

    C#发送HttpPost请求来调用WebService的方法

    在C#中发送HttpPost请求来调用WebService中的MyAction方法,代码如下:需要的朋友可以参考一下...

    C#教程网10052020-12-18
  • C#详解C#之事件

    详解C#之事件

    这篇文章主要介绍了C#之事件的知识点,文中代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以参考下...

    千金不如一默7662022-09-09
  • C#C# FileStream读写的文本操作代码分析

    C# FileStream读写的文本操作代码分析

    这篇文章主要分享了个人使用C# FileStream实现的读写的文本操作的小程序,主要是复习下对filestream的理解,希望对大家学习C#能够有所帮助...

    C#教程网6102021-12-02