How to programmatically close a message box in .NET Compact Framework
Monday, March 24th, 2008
On Windows Mobile devices, there is a Time out setting for the Home or Today screen. When this time out value is reached, WM will hide whatever program is running and display the Home/Today screen.
This causes a problem for one of my project. In my project, if the MessageBox.Show(…) function is called and the home/today screen kicks in before the user closes the message box, my program is hidden, and the user has no way to get back in, except turn the device off then on again.
The reason is obvious, modal dialog boxes, like message boxes, block the UI thread until they are closed. But how do you close a message box while it’s hidden? After some googling plus try and error, I came up with the following solution:
const int WM_DESTROY = 0x02; [DllImport("coredll.dll", EntryPoint = "FindWindow")] [DllImport("coredll.dll", EntryPoint="SendMessage")] public static void KillWindow(string className, string title) public static void CloseMessageBox(string title)
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
private static extern uint SendMessage(IntPtr hWnd, uint msg, uint wParam, uint lParam);
{
IntPtr handle = FindWindow(className, title);
SendMessage(handle, WM_DESTROY, 0, 0);
}
{
IntPtr handle = FindWindow("Dialog", title);
SendMessage(handle, WM_DESTROY, 0, 0);
}
This entry is filed under .NET, Mobile. You can follow any responses to this entry through the RSS 2.0 feed.You can leave a response, or trackback from your own site.