MFC modeless dialog close immediately

心不动则不痛 提交于 2019-12-01 12:19:49

Returning FALSE from your application class's InitInstance method tells MFC that initialization failed and the application should terminate.

Change that like to return TRUE; and everything should work fine.

BOOL CCsetkliensApp::InitInstance()
{
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);

    CWinApp::InitInstance();

    if (!AfxSocketInit())
    {
        AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
        return FALSE;
    }

    CShellManager *pShellManager = new CShellManager;

    dlg = new CCsetkliensDlg();
    m_pMainWnd = dlg;
    dlg->Create(CCsetkliensDlg::IDD);
    dlg->ShowWindow(SW_SHOW); // this is not a blocking call!


    if (pShellManager != NULL)
    {
        delete pShellManager;
    }

    return TRUE; // change this one!
}

The reason that it works with a modal dialog (shown by calling the DoModal method) is because a modal dialog creates its own message loop, which runs until you close the dialog. That means that execution effectively "blocks" at the DoModal call without returning control to your InitInstance method, so it doesn't return FALSE and MFC doesn't quit. At least not until you close the dialog, in which case you want it to quit, so everything appears the work.

I'm not seeing anything that tells the application to stay alive after opening the modeless window. You need at least one 'modal' style window, or something else to control termination of the application.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!