5
votes

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. The other application can successfully launch my application.

I am trying to hide the window of my application when the other application launches it.

BOOL CMyApp::InitInstance()
{
    CMyAppDlg dlg;
    m_pMainWnd = &dlg;        

    INT_PTR nResponse = dlg.DoModal();

    if (nResponse == IDOK)
    {
    }
    else if (nResponse == IDCANCEL)
    { 
    }

    return FALSE;
}

I tried to use:

dlg.ShowWindow(SW_HIDE) 

but it still does not hide the window.

How can I accomplish this task?

6
Does your CMyAppDlg inherit from CDialog?Mike Kwan
Thanks. It inherits from CDialogEx,chintan s
Is your dialog modeless ? CMyAppDlg will not show anything. If you call dlg.DoModal(), then the code will not reach the ShowWindow just after it.ixe013
@ixe013, Thanks for your reply. I am calling dlg.DoModal(). Please look at the updated code.chintan s
Isn't this a duplicate of Hiding an MFC dialog box?user34660

6 Answers

3
votes

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;
}
2
votes

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; 
} 
1
votes

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;
}
1
votes

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.

1
votes

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
votes

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