我需要在C#Framework 3.5中创建一个自定义MessageBox,它显示一些MesageBoxButtons,并返回DialogResult值。如果没有用户反应,在一定的超时时间之后,MessageBox应该关闭,返回null。
我跟着DmitryG的回答here,稍作修改:
static DialogResult? dialogResult_ = null;
public AutoClosingMessageBox(string text, string caption, int timeout, MessageBoxButtons msbb)
{
_caption = caption;
_timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
dialogResult_ = MessageBox.Show(text, caption, msbb);
}
public static DialogResult? Show(string text, string caption, int timeout, MessageBoxButtons efb)
{
new AutoClosingMessageBox(text, caption, timeout, efb);
return dialogResult_;
}
void OnTimerElapsed(object state)
{
IntPtr mbWnd = FindWindow("#32770", _caption);
if (mbWnd != IntPtr.Zero)
{
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
_timeoutTimer.Dispose();
}
dialogResult_ = null;
}
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,我们只需要调用Show函数
AutoClosingMessageBox.Show("Show me sth", "capt", 3000, MessageBoxButtons.AbortRetryIgnore);
当用户单击MessageBox中的按钮时,此方法会返回dialogResult_值,但WM_Close消息在超时后不再关闭MessageBox。
这是因为MessageBox仍在等待对话结果吗?如果是,我该如何避免呢?我想避免在新线程中启动消息框并且必须终止该线程。
答案 0 :(得分:1)
我同意其他评论,你应该自己制作一个消息框。
也就是说,如果你仍然想要使用其他方法,你应该能够通过向被识别的对话框发送适当的消息来使其工作;例如Alt-I for"忽略"。
这是您发布的代码版本:
class AutoClosingMessageBox
{
System.Threading.Timer _timeoutTimer;
string _caption;
static DialogResult? dialogResult_ = null;
private AutoClosingMessageBox(string text, string caption, int timeout, MessageBoxButtons msbb)
{
_caption = caption;
_timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
dialogResult_ = MessageBox.Show(text, caption, msbb);
}
public static DialogResult? Show(string text, string caption, int timeout, MessageBoxButtons efb)
{
new AutoClosingMessageBox(text, caption, timeout, efb);
return dialogResult_;
}
void OnTimerElapsed(object state)
{
IntPtr mbWnd = FindWindow("#32770", _caption);
if (mbWnd != IntPtr.Zero)
{
SetForegroundWindow(mbWnd);
SendKeys.SendWait("%I");
_timeoutTimer.Dispose();
}
dialogResult_ = null;
}
[DllImport("user32.dll", SetLastError = true)]
extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
extern static IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
extern static bool SetForegroundWindow(IntPtr hwnd);
}
SendKeys
类仅适用于当前活动窗口,因此我调用了SetForegroundWindow()
以确保密钥到达正确的窗口。
当然,上面的硬代码需要 Alt - I 。如果您想要更通用的解决方案,可以包含一个字典,将MessageBoxButtons
值映射到解除该对话框所需的相应SendKeys
字符串和/或让调用者提供该信息(强制它们提供实际的SendKeys
字符串或(更好)让它们传递一个枚举值,指示他们想要使用哪个按钮来关闭对话框,然后将实现映射到相应的字符串。