[C++/Win32] Modal dialog box without DialogBox()

Started by
3 comments, last by Colin Jeanne 17 years, 7 months ago
My program currently starts in WinMain, creates a non-modal dialog box with CreateDialog and a dialog resource file, and then it pumps the message loop until program exit. Inside the dialog proc, I process the various WM_COMMAND messages for the controls on my main dialog window. Straightforward so far. One of my controls is a button that I want to launch a second dialog window, have that window modal, but I want control of the message loop of this new dialog because I'm doing intensive calculations through the life of the dialog window until I either meet my stopping condition or the user presses cancel. Therefore, I need to be able to process the messages and then loop through my calculation code. I'm failing right now in creating a window which operates normally and disables the previous main dialog and and returns proper control once it closes. code:


// in WinMain
dlg = CreateDialog(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, dialogProc);

while ((result = GetMessage(&msg, NULL, 0, 0)) != FALSE)
{
    if (!IsDialogMessage(dlg, &msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

// done



// in the dialog proc
	switch (msg)
	{
        case WM_INITDIALOG:
            // init stuff
            return TRUE;

        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
            case CMD_BUTTON:
                // here's where I want to disable input to the main dlg window,
                // launch my new dialog box, have it complete, then return
                // control to the main dlg
                EnableWindow(dlg, FALSE);  // is this necessary?
                launchNewDialog();
                EnableWindow(dlg, TRUE);
                return TRUE;
            }
        }


// now for launchNewDialog

// parent hWnd is the dlg
newDlg = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(DLG_CALCULATE), dlg, calculationProc);

while (!done)
{
    // check if there are messages to process
    if (PeekMessage(&msg, newDlg, 0, 0, PM_NOREMOVE) != 0)
    {
        while (PeekMessage(&msg, newDlg, 0, 0, PM_REMOVE) != 0)
        {
            if (msg.message == WM_QUIT)
                done = TRUE;
            else
            {
                if (!IsDialogMessage(newDlg, &msg))
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }
        }
    }

    // here's where I do my calculations
    calculationFunction();
}

//and finally, the newDlg's proc
switch (msg)
{
case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case CMD_CANCEL_CALCULATION:
        DestroyWindow(hDlg);
        return TRUE;
    }

    case WM_DESTROY:
        PostQuitMessage(0);
        return TRUE;
}


...am I going about this correctly? Currently, once I close the new dialog box (which for now is just hitting cancel), it does close as expected, but the previous main dialog is frozen and unresponsive. I have to close it with ctrl-alt-del. I'm confused :( Any help is greatly appreciated!
Advertisement
er crap, I just realized I posted this in Game Programming and not in General Programming like I intended. if an admin wants to move it, please do :P
Why not offload the calculations to another thread? This will allow you to do the calculations while also receiving messages for the dialog.
That's an option, of course, but I'm really rusty with threaded programming :P Do you have any ideas on how to coordinate/synchronize data reading/writing between my calculation thread and my main thread (which will display results) ?

Something using globals and mutexes and some such?
A mutex would work.

This topic is closed to new replies.

Advertisement