gpt4 book ai didi

c# - 几秒钟后关闭 MessageBox

转载 作者:IT王子 更新时间:2023-10-29 03:42:20 32 4
gpt4 key购买 nike

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

我有一个确定按钮,但如果他们走开,我想超时并在假设 5 秒后关闭消息框,自动关闭消息框。

有自定义 MessageBox(继承自 Form)或其他报告者 Forms,但如果不需要 Form 会很有趣。

关于它有什么建议或示例吗?

更新:

对于 WPF
Automatically close messagebox in C#

自定义MessageBox(使用Form继承)
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/

可滚动的消息框
A Scrollable MessageBox in 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/206730
https://stackoverflow.com/a/14522952/206730

最佳答案

尝试以下方法:

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 package 获得:

Install-Package AutoClosingMessageBox

发行说明 (v1.0.0.2):

  • 新的 Show(IWin32Owner) API 支持最流行的场景(在#1 的上下文);
  • 新的 AutoClosingMessageBox.Factory() API 可提供对 MessageBox 显示的完全控制;

发行说明 (v1.0.0.3):

  • 新的倒计时功能(在 #4 的上下文中);
  • NET.6 迁移;

关于c# - 几秒钟后关闭 MessageBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14522540/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com