Introduction
Have you ever wanted a message box that times out? Well here you go.
Using the code
- Add MessageBoxEx.cs to your project or assembly your project references.
- Change
MessageBox.Show(...);
calls to MessageBoxEx.Show(..., timeout);
.
- That's it!
Notes
MessageBoxEx
has all 12 of the MessageBox.Show
overloads.
- The timeout is expressed in milliseconds.
MessageBoxEx
does not support reentrancy. Good thing too. Why would you want to have more than one message box at the same time?
- If the user doesn't press a button before the timeout elapses the function will return the
DialogResult
of the messagebox dialogs default button.
How does it work?
Just before calling MessageBox.Show(...)
, SetWindowsHookEx(WH_CALLWNDPROCRET, ...)
is called. The hook proc looks for a
WM_INITDIALOG
on a window with text equal to the message box caption. A windows timer is started on that window with the appropriate timeout. When the timeout fires
EndDialog
is called with the result set to the dialog default button ID. You can get that ID by sending a dialog box a
DM_GETDEFID
message. Pretty simple.
References
KB318804: HOW TO: Set a Windows Hook in Visual C#.NET
Acknowledgements
GipsySoft: http://www.gipsysoft.com/messagebox. You'll find a C++ implementation with many more features.