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

(0)

相关推荐

  • WPF中引入WindowsForms控件的方法

    本文实例讲述了WPF中引入WindowsForms控件的方法.分享给大家供大家参考,具体如下: 环境: [1]WindowsXP with SP3 [2]VS2008 with SP1 正文: Step1:在现有工程中引入Windows Forms 鼠标右键[References]->选择[Add Reference]->[.NET]标签页 加入[WindowsFormsIntegration]和[System.Windows.Forms]两项 Step2:在XAML文件里加入 [S2-1]加

  • 如何让WindowsForm缩小到系统匣过程详解

    如何让windowsForm能像MSN一样缩小后会跑到右下方的系统匣内, 只要利用NotifyIcon就可以做到相同的功能,MSDN NotifyIcon组件说明 首先先开启一个windowsForm项目,在工具栏将NotifyIcon拉到windowsForm上. 接着先为NotifyIcon指定Icon和Text,就是在系统匣会出现的小图和鼠标移过去时出现的文字. 执行结果 如果要在窗口缩小时,只显示在系统匣显示, 反之则变成只显示在工作列,就必须要在Form的SizeChanged事件做控

  • C# WindowsForm程序同时启动多个窗口类

    C# WindowsForm程序同时启动多个窗口类,具体内容如下 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MVCProject { /// <summary> /// 多窗口同时启动类 /// <remarks>继承A

  • WPF中不规则窗体与WindowsFormsHost控件兼容问题的解决方法

    本文实例讲述了WPF中不规则窗体与WindowsFormsHost控件兼容问题的解决方法.分享给大家供大家参考.具体方法如下: 这里首先说明一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的很多解决方案不能满足所有的情况,是有特定条件的,比如有一篇<WPF中不规则窗体与WebBrowser控件的兼容问题解决办法>(感兴趣的朋友可以自己百度一下这篇文章).该网友的解决办法也是别出心裁的,为什么这样说呢,他的webBrowser控件的是单独放在一个Form中

  • 3种方法实现WindowsForm切换窗口

    在Windows Form平台下实现窗口跳转,常见的有以下几种方式,比如通过Show/ShowDialog方法显示新的窗口,通过多文档界面(MDI)在父窗口中加载子窗口,或者是通关过在窗口中动态加载自定义控件,比如通过将窗口中的Panel控件作为容器,将一些自定义元素作为视图界面控件,动态加载到Panel控件中.下面我们将简单介绍这几种方式 Show/ShowDialog 通过这两种方法可以很简单的实现窗口跳转, Home home = new Home(); home.Show(); Home

  • WindowsForm实现TextBox占位符Placeholder提示功能

    在WinForm程序中,实现TextBox文本输入框占位符的方式也很多,最常用的是方式基于Windows Api SendMessage函数发送EM_SETCUEBANNER消息,或者通过TextBox自带的焦点事件处理. SendMessage函数实现 创建一个继承TextBox的ZhmTextBox输入框控件,新增Placeholder属性,在Placeholder的set方法中发送EM_SETCUEBANNER消息 public class ZhmTextBox: TextBox { pr

  • 如何用WindowsForm给窗口添加一些简单的动画效果

    在显示或者隐藏窗口的时候,可以利用Windows API中的AnimateWindow函数实现一些特殊的效果.主要的动画类型有四种:滚动.幻灯片.折叠或展开和alpha混合渐变. 窗口动画效果 首先定义动画工具类,引入AnimateWindow函数. public class WindowsEffects { public const int AW_ACTIVATE = 0x00020000; // 激活窗口.不要在AW_HIDE中使用此值. public const int AW_BLEND

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

    警告消息框主要是用来向用户户展示诸如警告.异常.完成和提示消息.一般实现的效果就是从系统窗口右下角弹出,然后加上些简单的显示和消失的动画. 创建警告框窗口 首先我们创建一个警告框窗口(Form),将窗口设置为无边框(FormBoderStyle=None),添加上图片和内容显示控件 创建好警告框后,我们先让他能够从窗口右下角显示出来, public partial class AlertMessageForm : Form { public AlertMessageForm() { Initia

  • WPF自动隐藏的消息框的实例代码

    (鼠标放上去将一直显示,移开动画继续),提供normal和error两种边框. 介绍:传统的确定,取消,OK,CANCAL之类的对话框太繁琐了,由于项目需要而诞生的仿手机式提示对话框.当然传统的对话框项目中也有,这里就不做介绍了. 出场和退场动画做得很简单,就用Blend随便鼓捣了一番,将就用吧. 预览效果如下: 思路其实很简单:将窗体透明化->布局和样式设计->后台传值调用. 准备工作:Microsoft.Expression.Interactions.dll和System.Windows.

  • 使用 Vue.js 仿百度搜索框的实例代码

    整理文档,搜刮出一个使用 Vue.js 仿百度搜索框的实例代码,稍微整理精简一下做下分享. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue demo</title> <style type="text/css"> .bg { background: #ccc; } </style> <s

  • Ajax实现动态加载组合框的实例代码

    一  province.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <script type="text/javascript" language="javaScript"> var xmlHttp = false; //全局变量,

  • 使用JS组件实现带ToolTip验证框的实例代码

    本组件依赖JQuery 本人测试的JQuery 是1.8, 兼容IE8,IE9,谷歌,火狐等. //验证输入框 function ValidateCompent(input){ var _input = $(input).clone(true); _input.css("height",$(input).css("height")); _input.css("width", $(input).css("width")); va

  • iOS中 LGLAlertView 提示框的实例代码

    使用与iOS8 以后,只是把系统的UIAlertController进行了封装,省的每次用的时候要写很多的代码.封装后只需要一句代码即可 , deome 地址 :https://github.com/liguoliangiOS/LGLAlertView.git 上代码LGLAlertView.h: #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, LGLAlert

  • Yii实现复选框批量操作实例代码

    整理文档,搜刮出一个Yii实现复选框批量操作实例代码,稍微整理精简一下做下分享. 在视图下 <?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'user-grid', 'dataProvider'=>$model->search(),//Model里需要有个search()方法,$model是Controller中传递的Model对象 // /'filter'=>$model, 'columns

  • jQuery中ztree 点击文本框弹出下拉框的实例代码

    废话不多说了,具体代码如下所示: <link rel="stylesheet" href="${ctx}/res/js/ztree/css/demo.css" type="text/css"/> <link rel="stylesheet" href="${ctx}/res/js/ztree/css/zTreeStyle/zTreeStyle.css" type="text/cs

  • jQuery+ThinkPHP+Ajax实现即时消息提醒功能实例代码

    心血来潮想为自己的小项目做一个提醒系统,譬如私信,评论等消息都能及时传递过来.由于道行尚浅,网上那些长轮询对于我略微复杂,于是觉得还是自己写一写试试比较好. 我的思路是,单独在数据库中建一个提醒表,表主要由接收者的id和消息类型两个字段组成 /* 前台提醒表 */ CREATE TABLE IF NOT EXISTS notification( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, -- 主键自增 mid INT NOT NULL DEFAULT

  • 原生JavaScript实现动态省市县三级联动下拉框菜单实例代码

    像平时购物选择地址时一样,通过选择的省动态加载城市列表,通过选择的城市动态加载县区列表,从而可以实现省市县的三级联动,下面使用原生的JavaScript来实现这个功能: 先给大家展示下测试结果: 未做任何选择时: 选择时: 代码如下所示: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>三级联动测试</titl

随机推荐