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

 更新时间:2020年7月18日 14:26  点击:1464

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

创建警告框窗口

首先我们创建一个警告框窗口(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实现警告消息框的资料请关注猪先飞其它相关文章!

[!--infotagslink--]

相关文章

  • php语言实现redis的客户端

    php语言实现redis的客户端与服务端有一些区别了因为前面介绍过服务端了这里我们来介绍客户端吧,希望文章对各位有帮助。 为了更好的了解redis协议,我们用php来实现...2016-11-25
  • jQuery+jRange实现滑动选取数值范围特效

    有时我们在页面上需要选择数值范围,如购物时选取价格区间,购买主机时自主选取CPU,内存大小配置等,使用直观的滑块条直接选取想要的数值大小即可,无需手动输入数值,操作简单又方便。HTML首先载入jQuery库文件以及jRange相关...2015-03-15
  • JS实现的简洁纵向滑动菜单(滑动门)效果

    本文实例讲述了JS实现的简洁纵向滑动菜单(滑动门)效果。分享给大家供大家参考,具体如下:这是一款纵向布局的CSS+JavaScript滑动门代码,相当简洁的手法来实现,如果对颜色不满意,你可以试着自己修改CSS代码,这个滑动门将每一...2015-10-21
  • jQuery+slidereveal实现的面板滑动侧边展出效果

    我们借助一款jQuery插件:slidereveal.js,可以使用它控制面板左右侧滑出与隐藏等效果,项目地址:https://github.com/nnattawat/slideReveal。如何使用首先在页面中加载jquery库文件和slidereveal.js插件。复制代码 代码如...2015-03-15
  • PHP+jQuery翻板抽奖功能实现

    翻板抽奖的实现流程:前端页面提供6个方块,用数字1-6依次表示6个不同的方块,当抽奖者点击6个方块中的某一块时,方块翻转到背面,显示抽奖中奖信息。看似简单的一个操作过程,却包含着WEB技术的很多知识面,所以本文的读者应该熟...2015-10-21
  • SQLMAP结合Meterpreter实现注入渗透返回shell

    sqlmap 是一个自动SQL 射入工具。它是可胜任执行一个广泛的数据库管理系统后端指印, 检索遥远的DBMS 数据库等,下面我们来看一个学习例子。 自己搭建一个PHP+MYSQ...2016-11-25
  • PHP实现今天是星期几的几种写法

    复制代码 代码如下: // 第一种写法 $da = date("w"); if( $da == "1" ){ echo "今天是星期一"; }else if( $da == "2" ){ echo "今天是星期二"; }else if( $da == "3" ){ echo "今天是星期三"; }else if( $da == "4"...2013-10-04
  • 原生js实现fadein 和 fadeout淡入淡出效果

    js里面设置DOM节点透明度的函数属性:filter= "alpha(opacity=" + value+ ")"(兼容ie)和opacity=value/100(兼容FF和GG)。 先来看看设置透明度的兼容性代码: 复制代码 代码如下: function setOpacity(ele, opacity) { if (...2014-06-07
  • Android中用HttpClient实现Http请求通信

    本文我们需要解决的问题是如何实现Http请求来实现通信,解决Android 2.3 版本以后无法使用Http请求问题,下面请看正文。 Android开发中使用HttpClient来开发Http程序...2016-09-20
  • mysql存储过程实现split示例

    复制代码 代码如下:call PROCEDURE_split('分享,代码,片段',',');select * from splittable;复制代码 代码如下:drop PROCEDURE if exists procedure_split;CREATE PROCEDURE `procedure_split`( inputstring varc...2014-05-31
  • PHP+Mysql+Ajax+JS实现省市区三级联动

    基本思想就是:在JS动态创建select控件的option,通过Ajax获取在PHP从SQL数据库获取的省市区信息,代码有点长,但很多都是类似的,例如JS中省、市、区获取方法类似,PHP中通过参数不同执行不同的select语句。index.html代码:复制...2014-05-31
  • JS实现程序暂停与继续功能代码解读

    下面代码用JS实现了程序的暂停与继续 复制代码 代码如下: <script type="text/javascript"> /*Javascript中暂停功能的实现 Javascript本身没有暂停功能(sleep不能使用)同时 vbscript也不能使用doEvents,故编写此函数实...2013-10-13
  • PHPCMS实现自动推送URL到百度站长平台

    我们一起来看一篇关于PHPCMS实现自动推送URL到百度站长平台,希望此教程能够帮助到各位朋友。 百度站长平台开放url推送接口,可以使用调用接口的形式主动及时推送u...2016-11-25
  • CSS+JS实现苹果cover flow效果示例

    cover flow效果就一个超级漂亮的图片切换效果了,下面我们来看看CSS+JS实现苹果cover flow效果示例吧,具体的操作步骤细节如下文介绍。 废话不多说, 直接上最终效果...2016-10-02
  • 兼容ie和firefox css alpha实现透明效果

    文章介绍了利用了css hack来实现兼容ie和firefox 的div透明效果,有需要的朋友可以参考一下,好了费话不说多了。 为了实现一些特殊效果,需要将页面元素变透明,本文介...2017-07-06
  • css实现文字发光效果方法汇总

    文字发光效果我们可以直接使用css来实现了今天我们来看一篇关于文字发光效果的例子,希望这篇文章能够帮助到各位朋友哦。 前言 我录制的慕课网视频一直没有上线,慕...2016-09-14
  • 基于PHP实现假装商品限时抢购繁忙的效果

    最近要做一个项目,有关商品显示抢购的功能。比如我们的网站很带流量,那么成千上万的用户在几秒内同时点你的商品,确实会出现“抢购人数过多,会提示,系统繁忙。 但是呢,大部分网站然而并没有这么牛叉。为了让用户感受到商...2015-10-21
  • php防止伪造跨站请求实现程序

    CSRF站外类型的漏洞其实就是传统意义上的外部提交数据问题,一般程序员会考虑给一些留言评论等的表单加上水印以防止SPAM问题,但是为了用户的体验性,一些操作可能没有做任...2016-11-25
  • apache中如何实现301转向

    编辑.htaccess的方法。 注意:在设置301重定向之前务必备份相应目录下的.htaccess文件。 1.重定向111cn.net到www.111cn.net 这种重定向旨在使域名唯一,是网站seo教程必...2016-01-28
  • php防止恶意刷新与刷票实现代码

    恶意刷新就是不停的去刷新提交页面,导致大量无效数据了,下面我们来总结一下php 防止恶意刷新页面方法总结 防止恶意刷页面的原理是 要求在页面间传递一个验证字符...2016-11-25