1
votes

I am developing an MFC application and exporting it into dll. The application has only one window, and I want that window modeless. Inside InitInstance(), if I want it to be modal, I only need to do this:

AFX_MANAGE_STATE(AfxGetStaticModuleState());
CUIWelcomeDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
}
return false;

It works just fine as a modal. This is the code for the modeless one:

AFX_MANAGE_STATE(AfxGetStaticModuleState());
CUIWelcomeDlg * dlg;
dlg=new CUIWelcomeDlg();
m_pMainWnd=dlg;
if(dlg!=NULL) {
    dlg->Create(IDD_UIWELCOME_DIALOG,NULL);
    dlg->ShowWindow(SW_SHOW);
} 
return true;

I tried to debug it. It is fine until it reaches return true; After that, the dialog window freezes and is not responding. Does anyone know how to fix this?

2
Out of curiosity, only: what sense does a modeless dialog based application make? What scenarios make that necessary?MikMik

2 Answers

0
votes

Try to remove the following line:
m_pMainWnd = dlg;

(if dlg is a pointer here, you should call it pdlg).

0
votes

You need to implement your own endless loop. Of course you don't want to stop the UI thread to be responsive so you need to capture and dispatch message inside this loop. Try adding this after ShowWindow:

MSG msg;

// Handle dialog messages
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
    if(!IsDialogMessage(&msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);  
    }
}