GVKun编程网logo

几秒钟后关闭MessageBox(自动关闭messagebox)

16

对于想了解几秒钟后关闭MessageBox的读者,本文将是一篇不可错过的文章,我们将详细介绍自动关闭messagebox,并且为您提供关于AfxMessageBox()和MessageBox()的区别

对于想了解几秒钟后关闭MessageBox的读者,本文将是一篇不可错过的文章,我们将详细介绍自动关闭messagebox,并且为您提供关于AfxMessageBox () 和 MessageBox () 的区别 & PCH 预编译头文件(201...、AfxMessageBox()和MessageBox()的区别 & PCH预编译头文件(201...、C# MessageBox 自动关闭、C# MessageBox自动关闭的有价值信息。

本文目录一览:

几秒钟后关闭MessageBox(自动关闭messagebox)

几秒钟后关闭MessageBox(自动关闭messagebox)

我有一个Windows Forms应用程序VS2010 C#,在其中显示一个MessageBox以显示消息。

我有一个好的按钮,但是如果他们走开,我想超时并关闭消息框,例如说5秒钟,然后自动关闭消息框。

有自定义的MessageBox(从Form继承)或另一个报告程序Forms,但是有趣的是不必使用Form。

有任何建议或样品吗?

更新:

对于WPF,
在C#中自动关闭消息框

自定义MessageBox(使用表单继承)
http://www.codeproject.com/Articles/17253/A-Custom-Message-
Box

http://www.codeproject.com/Articles/327212/Custom-Message-Box-in-
VC

http://tutplusplus.blogspot.com.es/2010/07/c-tutorial-create-your-own-
custom.html

http://medmondson2011.wordpress.com/2010/04/07/easy-to-use-custom-c-message-
box-with-a-configurable-
checkbox/

可滚动
的消息框C#中的可滚动的消息框

异常报告器
https://stackoverflow.com/questions/49224/good-crash-reporting-library-in-c-
sharp

http://www.codeproject.com/Articles/6895/A-Reusable-Flexible-Error-Reporting-
Framework

解:

也许我认为以下答案是很好的解决方案,无需使用表格。

https://stackoverflow.com/a/14522902/206730https://stackoverflow.com/a/14522952/206730

答案1

小编典典

请尝试以下方法:

AutoClosingMessageBox.Show("Text", "Caption", 1000);

AutoClosingMessageBox类实现如下:

public class AutoClosingMessageBox {    System.Threading.Timer _timeoutTimer;    string _caption;    AutoClosingMessageBox(string text, string caption, int timeout) {        _caption = caption;        _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,            null, timeout, System.Threading.Timeout.Infinite);        using(_timeoutTimer)            MessageBox.Show(text, caption);    }    public static void Show(string text, string caption, int timeout) {        new AutoClosingMessageBox(text, caption, timeout);    }    void OnTimerElapsed(object state) {        IntPtr mbWnd = FindWindow("#32770", _caption); // lpClassName is #32770 for MessageBox        if(mbWnd != IntPtr.Zero)            SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);        _timeoutTimer.Dispose();    }    const int WM_CLOSE = 0x0010;    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);}

更新: 如果希望在超时之前选择某些内容时要获取基础MessageBox的返回值,则可以使用以下版本的代码:

var userResult = AutoClosingMessageBox.Show("Yes or No?", "Caption", 1000, MessageBoxButtons.YesNo);if(userResult == System.Windows.Forms.DialogResult.Yes) {     // do something}...public class AutoClosingMessageBox {    System.Threading.Timer _timeoutTimer;    string _caption;    DialogResult _result;    DialogResult _timerResult;    AutoClosingMessageBox(string text, string caption, int timeout, MessageBoxButtons buttons = MessageBoxButtons.OK, DialogResult timerResult = DialogResult.None) {        _caption = caption;        _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,            null, timeout, System.Threading.Timeout.Infinite);        _timerResult = timerResult;        using(_timeoutTimer)            _result = MessageBox.Show(text, caption, buttons);    }    public static DialogResult Show(string text, string caption, int timeout, MessageBoxButtons buttons = MessageBoxButtons.OK, DialogResult timerResult = DialogResult.None) {        return new AutoClosingMessageBox(text, caption, timeout, buttons, timerResult)._result;    }    void OnTimerElapsed(object state) {        IntPtr mbWnd = FindWindow("#32770", _caption); // lpClassName is #32770 for MessageBox        if(mbWnd != IntPtr.Zero)            SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);        _timeoutTimer.Dispose();        _result = _timerResult;    }    const int WM_CLOSE = 0x0010;    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);}

另一个更新

我用YesNo按钮检查了@Jack的情况,发现发送WM_CLOSE消息的方法根本不起作用。
我将在单独的AutoclosingMessageBox库的上下文中提供一个
修复程序 。该库包含重新设计的方法,我认为这对某人可能有用。
也可以通过NuGet包获得:

Install-Package AutoClosingMessageBox

发行说明(v1.0.0.2):
-新的Show(IWin32Owner)API支持大多数流行的场景(在#1的上下文中);
-新的Factory()API提供对MessageBox显示的完全控制;

AfxMessageBox () 和 MessageBox () 的区别 & PCH 预编译头文件(201...

AfxMessageBox () 和 MessageBox () 的区别 & PCH 预编译头文件(201...

----------------------------------------------------------------------------------------------  
    问:AfxMessageBox () 和 MessageBox () 的区别?
    答:带 Afx 的是全局函数,可以在程序任何地方使用,不带的是 CWnd 的子函数,只能在 CWnd 窗口类对象里面使用

    附: int AfxMessageBox (LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0);
---------------------------------------------------------------------------------------------- 
    因为看到 *.pch 文件很大,于是便把它给删了,编译的时候出现这样的问题 Cannot open precompiled header file: ''Debug/6_6 消息对话框.pch'': No such file
    网上找了一下解决办法,其实很简单,只需要让编译器生成一个 pch 文件就可以了。也就是说把 Stdafx.cpp(即指定 / Yc 的那个 cpp 文件)重新编译一遍,当然你可以傻傻的 Rebuild All
----------------------------------------------------------------------------------------------

AfxMessageBox()和MessageBox()的区别 & PCH预编译头文件(201...

AfxMessageBox()和MessageBox()的区别 & PCH预编译头文件(201...

----------------------------------------------------------------------------------------------  
    问:AfxMessageBox()和MessageBox()的区别?
    答:带Afx的是全局函数,可以在程序任何地方使用,不带的是CWnd的子函数,只能在CWnd窗口类对象里面使用

    附: int AfxMessageBox( LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0 );
---------------------------------------------------------------------------------------------- 
    因为看到*.pch文件很大,于是便把它给删了,编译的时候出现这样的问题 Cannot open precompiled header file: ''Debug/6_6消息对话框.pch'': No such file
    网上找了一下解决办法,其实很简单,只需要让编译器生成一个pch文件就可以了。也就是说把 Stdafx.cpp(即指定/Yc的那个cpp文件)重新编译一遍,当然你可以傻傻的 Rebuild All
----------------------------------------------------------------------------------------------

C# MessageBox 自动关闭

C# MessageBox 自动关闭

本文以一个简单的小例子,介绍如何让 MessageBox 弹出的对话框,在几秒钟内自动关闭。特别是一些第三方插件(如:dll)弹出的对话框,最为适用。本文仅供学习分享使用,如有不足之处,还请指正。

概述

在程序中 MessageBox 弹出的对话框,用于向用户展示消息,这是一个模式窗口,可阻止应用程序中的其他操作,直到用户将其关闭。但是有时候在自动化程序中,如果弹出对话框,程序将会中断,等待人工的干预,这是一个非常不好的交互体验,如果程序能够自动帮我们点击其中一个按钮,让对话框消失,该有多好。

原理

通过对话框的标题查找对话框,获取对话框的句柄,然后对话框发送指令。

涉及知识点

  • MessageBox   显示消息窗口(也称为对话框)向用户展示消息。这是一个模式窗口,可阻止应用程序中的其他操作,直到用户将其关闭。System.Windows.Forms.MessageBox 可包含通知并指示用户的文本、按钮和符号。
  • Thread 创建和控制线程,设置其优先级并获取其状态。本例子主要创建一个线程,查找弹出的窗口。
  • WIN32 API 也就是 Microsoft Windows 32 位平台的应用程序编程接口。每一个服务,就是一个函数,用于和 Windows 进行交互。
  • MessageBoxButtons 是一个 Enum,表示对话框上显示哪些按钮。
  • PostMessage 是 Windows API (应用程序接口) 中的一个常用函数,用于将一条消息放入到消息队列中。消息队列里的消息通过调用 GetMessage 和 PeekMessage 取得。

示例截图如下:

关键代码

核心代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Drawing;
  4 using System.Linq;
  5 using System.Runtime.InteropServices;
  6 using System.Text;
  7 using System.Threading;
  8 using System.Threading.Tasks;
  9 
 10 namespace DemoMessageBox
 11 {
 12     /// <summary>
 13     /// 作者:Alan.hsiang
 14     /// 日期:2018-04-18
 15     /// 描述:通过WinAPI进行查找窗口,并对窗口进行操作
 16     /// </summary>
 17     public class MessageBoxHelper
 18     {
 19         /// <summary>
 20         /// 查找窗口
 21         /// </summary>
 22         /// <param name="hwnd">窗口句柄</param>
 23         /// <param name="title">窗口标题</param>
 24         /// <returns></returns>
 25         [DllImport("user32.dll", CharSet = CharSet.Auto)]
 26         static extern IntPtr FindWindow(IntPtr hwnd, string title);
 27 
 28         /// <summary>
 29         /// 移动窗口
 30         /// </summary>
 31         /// <param name="hwnd">窗口句柄</param>
 32         /// <param name="x">起始位置X</param>
 33         /// <param name="y">起始位置Y</param>
 34         /// <param name="nWidth">窗口宽度</param>
 35         /// <param name="nHeight">窗口高度</param>
 36         /// <param name="rePaint">是否重绘</param>
 37         [DllImport("user32.dll", CharSet = CharSet.Auto)]
 38         static extern void MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool rePaint);
 39         
 40         /// <summary>
 41         /// 获取窗口矩形
 42         /// </summary>
 43         /// <param name="hwnd">窗口句柄</param>
 44         /// <param name="rect"></param>
 45         /// <returns></returns>
 46         [DllImport("user32.dll", CharSet = CharSet.Auto)]
 47         static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect);
 48 
 49         /// <summary>
 50         /// 向窗口发送信息
 51         /// </summary>
 52         /// <param name="hwnd">窗口句柄</param>
 53         /// <param name="msg">信息</param>
 54         /// <param name="wParam">高字节</param>
 55         /// <param name="lParam">低字节</param>
 56         /// <returns></returns>
 57         [DllImport("user32.dll", CharSet = CharSet.Auto)]
 58         static extern int PostMessage(IntPtr hwnd, int msg, uint wParam, uint lParam);
 59 
 60         public const int WM_CLOSE = 0x10; //关闭命令
 61 
 62         public const int WM_KEYDOWN = 0x0100;//按下键
 63 
 64         public const int WM_KEYUP = 0x0101;//按键起来
 65 
 66         public const int VK_RETURN = 0x0D;//回车键
 67 
 68         public static bool IsWorking = false;
 69 
 70         /// <summary>
 71         /// 对话框标题
 72         /// </summary>
 73         public static string[] titles = new string[4] { "请选择", "提示", "错误", "警告" };
 74 
 75         /// <summary>
 76         /// 查找和移动窗口
 77         /// </summary>
 78         /// <param name="title">窗口标题</param>
 79         /// <param name="x">起始位置X</param>
 80         /// <param name="y">起始位置Y</param>
 81         public static void FindAndMoveWindow(string title, int x, int y)
 82         {
 83             Thread t = new Thread(() =>
 84             {
 85                 IntPtr msgBox = IntPtr.Zero;
 86                 while ((msgBox = FindWindow(IntPtr.Zero, title)) == IntPtr.Zero) ;
 87                 Rectangle r = new Rectangle();
 88                 GetWindowRect(msgBox, out r);
 89                 MoveWindow(msgBox, x, y, r.Width - r.X, r.Height - r.Y, true);
 90             });
 91             t.Start();
 92         }
 93 
 94         /// <summary>
 95         /// 查找和关闭窗口
 96         /// </summary>
 97         /// <param name="title">标题</param>
 98         private static void FindAndKillWindow(string title)
 99         {
100             IntPtr ptr = FindWindow(IntPtr.Zero, title);
101             if (ptr != IntPtr.Zero)
102             {
103                 int ret = PostMessage(ptr, WM_CLOSE, 0, 0);
104                 Thread.Sleep(1000);
105                 ptr = FindWindow(IntPtr.Zero, title);
106                 if (ptr != IntPtr.Zero)
107                 {
108                     PostMessage(ptr, WM_KEYDOWN, VK_RETURN, 0);
109                     PostMessage(ptr, WM_KEYUP, VK_RETURN, 0);
110                 }
111             }
112         }
113 
114         /// <summary>
115         /// 查找和关闭窗口
116         /// </summary>
117         public static void FindAndKillWindow()
118         {
119             Thread t = new Thread(() =>
120             {
121                 while (IsWorking)
122                 {
123                     //按标题查找
124                     foreach (string title in titles)
125                     {
126                         FindAndKillWindow(title);
127                     }
128                     Thread.Sleep(3000);
129                 }
130             });
131             t.Start();
132         }
133     }
134 }
View Code

 

备注

关于源码,请点击链接自行下载。

关于 PostMessage 和 SendMessage 的区分,请点链接

 

C# MessageBox自动关闭

C# MessageBox自动关闭

本文以一个简单的小例子,介绍如何让MessageBox弹出的对话框,在几秒钟内自动关闭。特别是一些第三方插件(如:dll)弹出的对话框,最为适用。本文仅供学习分享使用,如有不足之处,还请指正。

概述

在程序中MessageBox弹出的对话框,用于向用户展示消息,这是一个模式窗口,可阻止应用程序中的其他操作,直到用户将其关闭。但是有时候在自动化程序中,如果弹出对话框,程序将会中断,等待人工的干预,这是一个非常不好的交互体验,如果程序能够自动帮我们点击其中一个按钮,让对话框消失,该有多好。

原理

通过对话框的标题查找对话框,获取对话框的句柄,然后对话框发送指令。

涉及知识点

  • MessageBox   显示消息窗口(也称为对话框)向用户展示消息。这是一个模式窗口,可阻止应用程序中的其他操作,直到用户将其关闭。System.Windows.Forms.MessageBox可包含通知并指示用户的文本、按钮和符号。
  • Thread 创建和控制线程,设置其优先级并获取其状态。本例子主要创建一个线程,查找弹出的窗口。
  • WIN32 API 也就是Microsoft Windows 32位平台的应用程序编程接口。每一个服务,就是一个函数,用于和Windows进行交互。
  • MessageBoxButtons 是一个Enum,表示对话框上显示哪些按钮。
  • PostMessage 是Windows API(应用程序接口)中的一个常用函数,用于将一条消息放入到消息队列中。消息队列里的消息通过调用GetMessage和PeekMessage取得。

示例截图如下:

关键代码

核心代码如下:

  1 using System;
  2  System.Collections.Generic;
  3  System.Drawing;
  4  System.Linq;
  5  System.Runtime.InteropServices;
  6  System.Text;
  7  System.Threading;
  8  System.Threading.Tasks;
  9 
 10 namespace DemoMessageBox
 11 {
 12     /// <summary>
 13     /// 作者:Alan.hsiang
 14      日期:2018-04-18
 15      描述:通过WinAPI进行查找窗口,并对窗口进行操作
 16     </summary>
 17     public class MessageBoxHelper
 18     {
 19          20          查找窗口
 21          22         <param name="hwnd">窗口句柄</param>
 23         <param name="title">窗口标题 24         <returns></returns>
 25         [DllImport("user32.dll",CharSet = CharSet.Auto)]
 26         static extern IntPtr FindWindow(IntPtr hwnd,string title);
 27 
 28          29          移动窗口
 30          31          32         <param name="x">起始位置X 33         <param name="y">起始位置Y 34         <param name="nWidth">窗口宽度 35         <param name="nHeight">窗口高度 36         <param name="rePaint">是否重绘 37         [DllImport( 38         extern void MoveWindow(IntPtr hwnd,1)">int x,1)">int y,1)">int nWidth,1)">int nHeight,1)">bool rePaint);
 39         
 40          41          获取窗口矩形
 42          43          44         <param name="rect"></param>
 45          46         [DllImport( 47         bool GetwindowRect(IntPtr hwnd,1)">out Rectangle rect);
 48 
 49          50          向窗口发送信息
 51          52          53         <param name="msg">信息 54         <param name="wParam">高字节 55         <param name="lParam">低字节 56          57         [DllImport( 58         int PostMessage(IntPtr hwnd,1)">int msg,1)">uint wParam,1)">uint lParam);
 59 
 60         const int WM_CLOSE = 0x10; //关闭命令
 61 
 62         int WM_KEYDOWN = 0x0100;按下键
 63 
 64         int WM_KEYUP = 0x0101;按键起来
 65 
 66         int VK_RETURN = 0x0D;回车键
 67 
 68         bool IsWorking = false;
 69 
 70          71          对话框标题
 72          73         string[] titles = new string[4] { 请选择提示错误警告" };
 74 
 75          76          查找和移动窗口
 77          78          79          80          81         void FindAndMoveWindow(string title,1)">int y)
 82         {
 83             Thread t = new Thread(() =>
 84             {
 85                 IntPtr msgBox = IntPtr.Zero;
 86                 while ((msgBox = FindWindow(IntPtr.Zero,title)) == IntPtr.Zero) ;
 87                 Rectangle r = new Rectangle();
 88                 GetwindowRect(msgBox,1)"> r);
 89                 MoveWindow(msgBox,x,y,r.Width - r.X,r.Height - r.Y,1)">true);
 90             });
 91             t.Start();
 92         }
 93 
 94          95          查找和关闭窗口
 96          97         标题 98         private void FindAndKillWindow( title)
 99 100             IntPtr ptr = FindWindow(IntPtr.Zero,title);
101             if (ptr != IntPtr.Zero)
102 103                 int ret = PostMessage(ptr,WM_CLOSE,1)">0,1)">0104                 Thread.Sleep(1000105                 ptr =106                 107                 {
108                     PostMessage(ptr,WM_KEYDOWN,VK_RETURN,1)">109                     PostMessage(ptr,WM_KEYUP,1)">110                 }
111             }
112 113 
114         115         116         117         void FindAndKillWindow()
118 119             Thread t = 120 121                 while (IsWorking)
122 123                     按标题查找
124                     foreach (string title in titles)
125                     {
126                         FindAndKillWindow(title);
127                     }
128                     Thread.Sleep(3000129 130 131 132 133     }
134 }
View Code

 

备注

关于源码,请点击链接自行下载。

关于PostMessage和SendMessage的区分,请点链接

 

总结

以上是小编为你收集整理的C# MessageBox自动关闭全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

今天关于几秒钟后关闭MessageBox自动关闭messagebox的分享就到这里,希望大家有所收获,若想了解更多关于AfxMessageBox () 和 MessageBox () 的区别 & PCH 预编译头文件(201...、AfxMessageBox()和MessageBox()的区别 & PCH预编译头文件(201...、C# MessageBox 自动关闭、C# MessageBox自动关闭等相关知识,可以在本站进行查询。

本文标签: