Running modeless dialog from within modal dialog's WM_INITDIALOG routine

Running modeless dialog from within modal dialog's WM_INITDIALOG routine

Post by Edward Die » Fri, 04 Dec 2009 06:13:01


When initializing a modal dialog I have a long-running routine and I
wanted to run a modeless dialog which shows the progress of the
long-running routine from within the WM_INITDIALOG handler.

Is this possible ?

In the WM_INITDIALOG I used CreateDialogParam, got back the HWND, did a
ShowWindow, and subsequently within my long running initialization
routine did a PeekMessage loop just for messages for the modeless
dialog, like so:

while (true)
{

MSG msg;

while (PeekMessage(&msg, lwnd, 0, 0, PM_REMOVE))
{
IsDialogMessage(lwnd, &msg);
}

// long running processing, and break when finished
}

where lwnd is the HWND for my modeless dialog. Then at the end of my
long-running initialization I did a SendMessage(lwnd,WM_CLOSE,0,0) and
in the modeless dialog's windows procedure it does a DestroyWindow when
it receives the WM_CLOSE message.

Unfortunately the modeless dialog never showed itself. I checked the
CreateDialogParam and it does return a valid HWND. The documentation for
the return from ShowWindow does not tell how I can determine whether it
is successful or not. Nevertheless the window never appears.

If I start the dialog as a modal dialog, it appears correctly, but of
course that's not what I want. Any ideas why the modeless dialog is not
appearing would be appreciated.

The application does not run a standard message loop since the main
window is a modal dialog. But I do not think that should affect my
ability to read the thread's message input queue for modeless dialog
messages.
 
 
 

Running modeless dialog from within modal dialog's WM_INITDIALOG routine

Post by Edward Die » Fri, 04 Dec 2009 07:14:04


I got this to work. My mistake was in not specifying the modeless dialog
as a popup window and then passing in a 0 HWND for its parent. Instead I
was erroneously trying to have the modeless dialog be a child window and
passing in the HWND of my modal dialog as the parent.

 
 
 

Running modeless dialog from within modal dialog's WM_INITDIALOG routine

Post by Chris Beck » Tue, 08 Dec 2009 20:46:26


Yes



And any child windows of lwnd? What about messages sent to child windows
or the main dialog during this time? Its far better to Peek and
_Dispatch_ *all* messages. IsDialogMessage's real job is to check for
certain dialog accellerators (tab presses for example) that are
otherwise dispatched to child windows. IsDialogMessage is going to
return FALSE for a whole bunch of important to handle messages including
WM_PAINT.

Your message dispatching loop should look more like:
while(PeekMessage(&msg,0,0,0,PM_REMOVE))
{
if(!IsDialogMessage(hMainDialog,&msg) && !IsDialogMessage(lwnd,&msg))
{
if(msg.message == WM_QUIT)
{
PostQuitMessage();
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}


It has to paint to appear: - The principal problem is you were not
calling DispatchMessage.