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

 更新时间:2020年07月18日 09:23:47   作者:zhuanghamiao  
这篇文章主要介绍了WindowsForm如何实现警告消息框,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下

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

创建警告框窗口

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

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

  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();
    }
  }

警告框显示和关闭动画

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

  // 警告框的行为(显示,停留,退出)
  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();
    }
  }

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

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

  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);
 }

处理多个警告框重叠问题

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

    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;
    }

警告框的完整代码

  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实现警告消息框的资料请关注脚本之家其它相关文章!

相关文章

  • 如何搭建新的WPF项目框架

    如何搭建新的WPF项目框架

    这篇文章主要介绍了如何搭建新的WPF项目框架,在项目开发中比较常见的开发模式就是MVVM模式,使用MVVM框架开发好处:1、框架较轻,2、学习成本低、3、适用大多数中小型项目,4、相对于微软的prism框架更容易上手,需要的朋友可以参考下
    2015-07-07
  • C#实现多选项卡的浏览器控件

    C#实现多选项卡的浏览器控件

    这篇文章主要为大家详细介绍了C#实现多选项卡的浏览器控件的相关资料,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • C#在Excel表格中插入、编辑和删除批注

    C#在Excel表格中插入、编辑和删除批注

    这篇文章主要为大家详细介绍了C#如何在Excel表格中插入、编辑和删除批注,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • DevExpress实现TreeList按条件隐藏节点CheckBox的方法

    DevExpress实现TreeList按条件隐藏节点CheckBox的方法

    这篇文章主要介绍了DevExpress实现TreeList按条件隐藏节点CheckBox的方法,需要的朋友可以参考下
    2014-08-08
  • 利用FlubuCore用C#来写DevOps脚本的方法详解

    利用FlubuCore用C#来写DevOps脚本的方法详解

    这篇文章主要介绍了利用FlubuCore用C#来写DevOps脚本的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • C#中的正则表达式介绍

    C#中的正则表达式介绍

    关于正则表达式,我们都知道挺繁琐的。本文介绍的是C#中的正则表达式,希望对你有帮助,一起来看。
    2015-10-10
  • 浅析C# 9.0 新特性之 Lambda 弃元参数

    浅析C# 9.0 新特性之 Lambda 弃元参数

    这篇文章主要介绍了C# 9.0 新特性之 Lambda 弃元参数的的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,想学习c#的朋友可以了解下
    2020-06-06
  • C#使用Json.Net对JSON与对象的序列化与反序列化

    C#使用Json.Net对JSON与对象的序列化与反序列化

    这篇文章介绍了Json.Net对JSON与对象的序列化与反序列化,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • 完成OSS.Http底层HttpClient重构封装 支持标准库

    完成OSS.Http底层HttpClient重构封装 支持标准库

    OSS.Http项目对于.Net Standard标准库的支持已经迁移完毕,OSS开源系列两个最底层的类库已经具备跨运行时支持的能力。本篇文章主要包含 1. HttpClient的介绍,2. 重构的思路, 3. 容易遇到的问题。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • C#面向对象编程中开闭原则的示例详解

    C#面向对象编程中开闭原则的示例详解

    在面向对象编程中,SOLID 是五个设计原则的首字母缩写,旨在使软件设计更易于理解、灵活和可维护。本文将通过实例详细讲讲C#面向对象编程中开闭原则,需要的可以参考一下
    2022-07-07

最新评论