MessageBox and Modal dialog
-
Sorry if this is a dumb ? but I am fairly new to C++. I am writing an application that has no user events. I am using only WIN32 - NO MFC - because it is basically a bootstrapper application and I do not want any dependencies other than core Windows APIs. I display a small status dialog that provides progress messages. If all goes well, it runs, displays it's messages, and the bootstrapper application terminates after launching another application. That part works fine. But, if a condition is not met, I would like to display a message box. When I do this, the whole thing locks up. I am creating the messagebox as a child of the dialog, but it still does not process it's messages or even receive focus. I've tried making the main dialog modeless - did not help. I find the SDK docs on message processing rather confusing. Can anyone point me in the right direction? Thanks!!!! R :-)
-
Sorry if this is a dumb ? but I am fairly new to C++. I am writing an application that has no user events. I am using only WIN32 - NO MFC - because it is basically a bootstrapper application and I do not want any dependencies other than core Windows APIs. I display a small status dialog that provides progress messages. If all goes well, it runs, displays it's messages, and the bootstrapper application terminates after launching another application. That part works fine. But, if a condition is not met, I would like to display a message box. When I do this, the whole thing locks up. I am creating the messagebox as a child of the dialog, but it still does not process it's messages or even receive focus. I've tried making the main dialog modeless - did not help. I find the SDK docs on message processing rather confusing. Can anyone point me in the right direction? Thanks!!!! R :-)
The problem with
MessageBox
is that it takes over input and doesn't allow the creating thread to run. In effect, the MessageBox takes over the focus, just like a modal dialog. So you'll want to come up with a way to run the MessageBox function in a thread separate from your main dialog's thread. You may consider creating an additional modeless dialog that hangs off of your main dialog. You don't have to display the dialog (never call itsShowWindow(SW_SHOW)
), and it doesn't have to have any special features. It only exists to pop up the MessageBox. Maybe add a method to this modeless dialog,DisplayMessage(msg)
or something, that calls MessageBox. I'm not able to test this now, but I think it'll work. It shouldn't tie up your main dialog's message loop, only the message loop of the invisible modeless dialog. Just make sure that you don't call theDisplayMessage
method in the modeless while another message is being displayed. You'll also have to make sure the modeless message box gets cleaned up correctly (viaDestroyWindow
and (just like all modeless dialogs) don't let it be closed by the user. Alternately, if you're good with Win32 threads, you can define a thread that you can fire from your main dialog. Given a string to display (and the MB_OK, etc. codes), this thread's main execution function simply callsMessageBox(your params)
, then exits. Just be sure you clean up the threads nicely so as not to leave "zombie" threads. Hope this gives you some ideas. Bob Ciora -
The problem with
MessageBox
is that it takes over input and doesn't allow the creating thread to run. In effect, the MessageBox takes over the focus, just like a modal dialog. So you'll want to come up with a way to run the MessageBox function in a thread separate from your main dialog's thread. You may consider creating an additional modeless dialog that hangs off of your main dialog. You don't have to display the dialog (never call itsShowWindow(SW_SHOW)
), and it doesn't have to have any special features. It only exists to pop up the MessageBox. Maybe add a method to this modeless dialog,DisplayMessage(msg)
or something, that calls MessageBox. I'm not able to test this now, but I think it'll work. It shouldn't tie up your main dialog's message loop, only the message loop of the invisible modeless dialog. Just make sure that you don't call theDisplayMessage
method in the modeless while another message is being displayed. You'll also have to make sure the modeless message box gets cleaned up correctly (viaDestroyWindow
and (just like all modeless dialogs) don't let it be closed by the user. Alternately, if you're good with Win32 threads, you can define a thread that you can fire from your main dialog. Given a string to display (and the MB_OK, etc. codes), this thread's main execution function simply callsMessageBox(your params)
, then exits. Just be sure you clean up the threads nicely so as not to leave "zombie" threads. Hope this gives you some ideas. Bob CioraThanks for the input. I had already considered spawning a second thread but instead I kept reading the SDK info on message loops and thinking "Surely this cannot be as difficult as I am making it". So I went with your second suggestion. I just call a wrapper function that spawns the thread and goes into a WaitForSingleObject state to suspend the main execution until the user dismisses the messagebox. Works great! Thanks, Robert