MFC modeless dialog close immediately

前端 未结 2 2069
栀梦
栀梦 2021-01-15 23:10

I like to write a modeless dialog based app, but I have a problem. When the program starts, the window close immediately.

The same code works fine when I make a moda

2条回答
  •  -上瘾入骨i
    2021-01-15 23:58

    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.

提交回复
热议问题