MFC modeless dialog close immediately

前端 未结 2 2070
栀梦
栀梦 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条回答
  • 2021-01-15 23:53

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题