Hide an MFC dialog window

后端 未结 6 2071
轮回少年
轮回少年 2020-12-19 17:46

I have written an MFC dialog based application which is launched by some another application. For now, I have not added any code. It is just the default files that I got. Th

相关标签:
6条回答
  • 2020-12-19 17:55

    Solution to the above issue. The InitInstance code should be as follows:

    BOOL CMyApp::InitInstance()
    {
        CWinApp::InitInstance();
        AfxEnableControlContainer();
    
        CMyAppDlg dlg;
        dlg.Create(IDD_MyAppUI_DIALOG,NULL);
        dlg.ShowWindow(SW_HIDE);
        dlg.UpdateWindow();
        m_pMainWnd = &dlg;
    
        return TRUE;
    }
    
    0 讨论(0)
  • 2020-12-19 18:05

    The showWindow method has 2 variable.

    • handle of window
    • nCmdShow(Controls how the window is to be shown)

      BOOL WINAPI ShowWindow( In HWND hWnd, In int nCmdShow );

      HWND hWnd = GetSafeHwnd();

      ShowWindow(hWnd,SW_HIDE);

    See HERE

    0 讨论(0)
  • 2020-12-19 18:09

    As soon as you call DoModal your dialog is doomed to be shown. There is only one workaround that successfully avoids focus/flicker problems. See my answer here: Hiding an MFC dialog box

    Hence, your code should look like this:

    BOOL CMyApp::InitInstance() 
    { 
        CMyAppDlg dlg;
        dlg.SetVisible(FALSE); // Sets m_visible flag to FALSE.
    
        m_pMainWnd = &dlg;         
    
        INT_PTR nResponse = dlg.DoModal(); 
    
        if (nResponse == IDOK) 
        { 
        } 
        else if (nResponse == IDCANCEL) 
        {  
        } 
    
        return FALSE; 
    } 
    
    0 讨论(0)
  • 2020-12-19 18:14

    I'd suggest you have another problem someplace.

    If you create a totally new, blank MFC app (Visual Studio 2010) then in App::InitInstance, setting SW_HIDE rather than SW_SHOW does cause the resultant window to be hidden.

    BOOL CProj1App::InitInstance()
    {
    
    // boilerplate code
          . . . 
    
    // The one and only window has been initialized, so show and update it
    m_pMainWnd->ShowWindow(SW_HIDE);   // WORKS!
    m_pMainWnd->UpdateWindow();
    
    return TRUE;
    }
    
    0 讨论(0)
  • 2020-12-19 18:14

    First of all let me address some issues with previous solutions.

    chintan s: Indeed dialog will be killed when function goes out of scope. It would be a valid solution if dialog was declared as a member variable of the app class.

    Vikky: No need to call Windows API, since dialog is derived from CWnd and it inherits ShowWindow member that take only one parameter: show command.

    ixe013: This solution will work, however, before dialog hides, it will flash, since ShowWindow is called before OnInitDialog is called.

    Pete: This won’t work, since, modal dialog starts before m_pMainWnd has any value assigned to it.

    The solution is pointed by ixe013.

    This is so far the only solution that works but you will have to declare member variable in you dialog class, as described in the article.

    0 讨论(0)
  • 2020-12-19 18:15

    You must hide the dialog from the inside.

    1. Overload OnInitDialog
    2. Call CDialogEx::OnInitDialog()
    3. Hide your window and return

    Here is the code

    BOOL CMyAppDlg::OnInitDialog()
    {
        BOOL result = CDialogEx::OnInitDialog();
    
        this->ShowWindow(SW_HIDE);
    
        return result;  // return TRUE  unless you set the focus to a control
    }
    

    There is another method with a sentinel value, YMMV.

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