zoukankan      html  css  js  c++  java
  • 自动关闭MessageBox(原创)

    MessageBox.Show相信用.net开发程序的同仁们都非常熟悉。这个弹出窗口的函数有很多优点,可以编辑显示内容,可以编辑窗口名称,可以指定显示哪些按钮,可以指定提示图标,可以自动排文同时调整窗体大小,可以...,可以的确实很多,信手拈来很方便。但无论多么强大的控件,总会有开发者自己的一些功能需求不能满足。

    以下针对MessageBox的自动关闭功能扩展了一个类和三个静态方法。主要的思路有两个:

    1. 在调用MessageBox.Show之前打开一个计时器,计时器的间隔时间由外部通过函数传入。到了计时器的间隔时间后,调用系统的API函数FindWindow和EndDialog来找到弹出窗口并关闭窗口。完成这一切之后停止计时器,释放资源。

    2. 直接调用API的隐藏函数MessageBoxTimeout。相较第一个方法来讲,代码量更加少,而且窗口的样式和返回的结果更加多样化,更加灵活。

    在此我将两个方法都完整的写出来,以供参考。

     /*------------------------------调用代码----------------------------
      * AutoClosedMsgBox.Show("嘘...\r\n窗口5秒后关闭...", "提示", 5000);
      * AutoClosedMsgBox.Show("嘘...\r\n窗口5秒后关闭...", "提示", 5000, 0);
      * AutoClosedMsgBox.Show("嘘...\r\n窗口5秒后关闭...", "提示", 5000, MsgBoxStyle.OK);
      */
     public class AutoClosedMsgBox
     {
         [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
         static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
         [DllImport("user32.dll")]
         static extern bool EndDialog(IntPtr hDlg, int nResult);
    
         [DllImport("user32.dll")]
         static extern int MessageBoxTimeout(IntPtr hwnd, string txt, string caption, 
             int wtype, int wlange, int dwtimeout);
    
         const int WM_CLOSE = 0x10;
         /// <summary>
         /// 弹出自动关闭的MessageBox窗口,只有“确定”按钮
         /// </summary>
         /// <param name="text">弹出窗口的显示内容</param>
         /// <param name="caption">弹出窗口的标题</param>
         /// <param name="milliseconds">窗口持续显示时间(毫秒)</param>
         /// <returns>固定返回DialogResult.OK</returns>
         public static DialogResult Show(string text, string caption, int milliseconds)
         {
             Timer timer = new Timer();
             timer.Interval = milliseconds;
             timer.Tick += (a, b) =>
             {
                 IntPtr ptr = FindWindow(null, caption);
                 if (ptr != IntPtr.Zero) EndDialog(ptr, 0);
                 timer.Stop();
             };
             timer.Start();
             MessageBox.Show(text, caption);
             return DialogResult.OK;
         }
    
         /// <summary>
         /// 弹出自动关闭的MessageBox窗口,有多种显示方式
         /// </summary>
         /// <param name="txt">弹出窗口的显示内容</param>
         /// <param name="caption">弹出窗口的标题</param>
         /// <param name="style">窗口样式(枚举)</param>
         /// <param name="dwtimeout">窗口持续显示时间(毫秒)</param>
         /// <returns>0-无显示 1-确定 2-取消 3-终止 4-重试 5-忽略 6-是 7-否 10-重试 11-继续 32000-系统关闭</returns>
         public static int Show(string text, string caption, int milliseconds, MsgBoxStyle style)
         {
             return MessageBoxTimeout(IntPtr.Zero, text, caption, (int)style, 0, milliseconds);
         }
    
         public static int Show(string text, string caption, int milliseconds, int style)
         {
             return MessageBoxTimeout(IntPtr.Zero, text, caption, style, 0, milliseconds);
         }
     }
     public enum MsgBoxStyle
     {
         OK = 0, OKCancel = 1, AbortRetryIgnore = 2, YesNoCancel = 3, YesNo = 4,
         RetryCancel = 5, CancelRetryContinue = 6,
    
         //红叉 + ...
         RedCritical_OK = 16, RedCritical_OKCancel = 17, RedCritical_AbortRetryIgnore = 18,
         RedCritical_YesNoCancel = 19, RedCritical_YesNo = 20,
         RedCritical_RetryCancel = 21, RedCritical_CancelRetryContinue = 22,
    
         //蓝问号 + ...
         BlueQuestion_OK = 32, BlueQuestion_OKCancel = 33, BlueQuestion_AbortRetryIgnore = 34,
         BlueQuestion_YesNoCancel = 35, BlueQuestion_YesNo = 36,
         BlueQuestion_RetryCancel = 37, BlueQuestion_CancelRetryContinue = 38,
    
         //黄叹号 + ...
         YellowAlert_OK = 48, YellowAlert_OKCancel = 49, YellowAlert_AbortRetryIgnore = 50,
         YellowAlert_YesNoCancel = 51, YellowAlert_YesNo = 52,
         YellowAlert_RetryCancel = 53, YellowAlert_CancelRetryContinue = 54,
    
         //蓝叹号 + ...
         BlueInfo_OK = 64, BlueInfo_OKCancel = 65, BlueInfo_AbortRetryIgnore = 66,
         BlueInfo_YesNoCancel = 67, BlueInfo_YesNo = 68,
         BlueInfo_RetryCancel = 69, BlueInfo_CancelRetryContinue = 70,
     }

    笔者(IcyJiang)著。转载请注明出处:http://www.cnblogs.com/icyJ/archive/2012/11/08/AutoClosedMsgBox.html

    留文一则交流,二则备用。

  • 相关阅读:
    js中cookie的操作
    javascript HTML静态页面传值的四种方法
    更改ligerui源码实现分页样式修改
    javascript Date format(js日期格式化)
    LigerUI用PostGetAjax前后台交互方式的写法
    html中 accept 属性
    jQuery.ajax() 设置 Headers 中的 Accept 内容
    C# 事件(Event)
    C# 委托(Delegate)
    C# 反射(Reflection)
  • 原文地址:https://www.cnblogs.com/icyJ/p/AutoClosedMsgBox.html
Copyright © 2011-2022 走看看