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")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

 

[DllImport("coredll.dll", EntryPoint="SendMessage")]
private static extern uint SendMessage(IntPtr hWnd, uint msg, uint wParam, uint lParam);

 

public static void KillWindow(string className, string title)
{
    IntPtr handle = FindWindow(className, title);
    SendMessage(handle, WM_DESTROY, 0, 0);
}

public static void CloseMessageBox(string title)
{
    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.

No Comments to “How to programmatically close a message box in .NET Compact Framework”

Leave a Reply

You must be logged in to post a comment.