1

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?

jonsca
  • 10,218
  • 26
  • 54
  • 62
user654894
  • 195
  • 5
  • 12
  • Out of curiosity, only: what sense does a modeless dialog based application make? What scenarios make that necessary? – MikMik Jun 28 '11 at 09:37

2 Answers2

0

Try to remove the following line:
m_pMainWnd = dlg;

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

FKDev
  • 2,266
  • 1
  • 20
  • 23
0

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);  
    }
}
Javier De Pedro
  • 2,219
  • 4
  • 32
  • 49
  • hmm, actually I don't really understand, but isn't this infinite loop? Then if I do this after ShowWindow (inside InitInstance), the InitInstance will never return? – user654894 Jun 29 '11 at 02:45
  • Yes and no. It is and infinite loop but you're processing messages inside this loop so it will process the close window message and the application will end. I'll give it a try...but of course is up to you. – Javier De Pedro Jun 30 '11 at 06:50