栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

几秒钟后关闭MessageBox

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

几秒钟后关闭MessageBox

请尝试以下方法:

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显示的完全控制;



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/576286.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号