Q: Starting Doc/View application hidden Using Visual studio 2010 and MFC Doc/View Applications  I want my SDI application to start up completely hidden, and after sometime or with receiving some message from tray icon it shows the mainframe, view and so on. I change the line m_pMainWnd->ShowWindow(SW_NORMAL); to m_pMainWnd->ShowWindow(SW_HIDE); in BOOL CMyApp::InitInstance() but the main frame just flickers after executing the application and then goes hiiden what should I do inorder to avoid this problem and keep the showing capability of main frame when ever I want.

A: Here is the solution for SDI/MDI app: The new MFC (with VC2010) overrides the m_nCmdShow value with a setting stored in the system registry. To change this behaviour, simply override the LoadWindowPlacement virtual function in the application class.
BOOL CAdVisuoApp::LoadWindowPlacement(CRect& rectNormalPosition, int& nFflags, int& nShowCmd)
{
    BOOL b = CWinAppEx::LoadWindowPlacement(rectNormalPosition, nFflags, nShowCmd);
    nShowCmd = SW_HIDE;
    return b;
}


A: Normally if you have VC2005 or earlier the following will do:
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

m_nCmdShow = SW_HIDE;

// Dispatch commands specified on the command line.  Will return FALSE if
// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
if (!ProcessShellCommand(cmdInfo))
    return FALSE;

// The one and only window has been initialized, so show and update it
m_pMainWnd->ShowWindow( m_nCmdShow);
m_pMainWnd->UpdateWindow();

Note that m_nCmdShow should be set to SW_HIDE before ProcessShallCommand for the flicker not to occur.
It looks like there might be a bug in VC2010 though. Since I have done this before it intrigued me and tried a fresh VC2010 project but it was not working. I noticed the problem was deep in the following MFC function.
BOOL CFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,
          CWnd* pParentWnd, CCreateContext* pContext)
{
    // only do this once
    ASSERT_VALID_IDR(nIDResource);
    ASSERT(m_nIDHelp == 0 || m_nIDHelp == nIDResource);

    m_nIDHelp = nIDResource;    // ID for help context (+HID_BASE_RESOURCE)

    CString strFullString;
    if (strFullString.LoadString(nIDResource))
        AfxExtractSubString(m_strTitle, strFullString, 0);    // first sub-string

    VERIFY(AfxDeferRegisterClass(AFX_WNDFRAMEORVIEW_REG));

    // attempt to create the window
    LPCTSTR lpszClass = GetIconWndClass(dwDefaultStyle, nIDResource);
    CString strTitle = m_strTitle;
    if (!Create(lpszClass, strTitle, dwDefaultStyle, rectDefault,
      pParentWnd, ATL_MAKEINTRESOURCE(nIDResource), 0L, pContext))
    {
        return FALSE;   // will self destruct on failure normally
    }

    // save the default menu handle
    ASSERT(m_hWnd != NULL);
    m_hMenuDefault = m_dwMenuBarState == AFX_MBS_VISIBLE ? ::GetMenu(m_hWnd) : m_hMenu;

    // load accelerator resource
    LoadAccelTable(ATL_MAKEINTRESOURCE(nIDResource));

    if (pContext == NULL)   // send initial update
        SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE);

    return TRUE;
}

m_nCmdShow is still SW_HIDE when this function executes but it changes to SW_SHOWNORMAL when if (!Create(lpszClass... line executes. I don't know why this happens in VC2010 project only, sounds like a bug to me.
My sample project was SDI.

A: This comes from a dialog based application but you should be able to convert it to a Doc/View app as well.  You need to handle the OnWindowPosChanging event.  The key line is the the one inside the if statement.  This allows my application to start completely hidden from view.
void CIPViewerDlg::OnWindowPosChanging( WINDOWPOS FAR* lpWindowPosition )
{
    if( !m_bVisible ) 
    {
        lpWindowPosition->flags &= ~SWP_SHOWWINDOW;
    }

    CDialog::OnWindowPosChanging( lpWindowPosition );

}


A: Make sure that you are correctly turning off the WS_VISIBLE bit in CMainFrame::PreCreateWindow(CREATESTRUCT& cs).  Something like this should worK:
cs.style &= ~WS_VISIBLE;

We had simply been negating the bit instead of turning it off, and we got away with it in VS 6.0 because this function was called only once.  It is called twice in newer versions of Visual Studio, so in the second call we were flipping it right back on again. :-O

A: I tried all for Visual Studio 2010 and finished up with:
class CMainFrame : public CFrameWndEx
{
// ...
// Attributes
public:
   BOOL m_bForceHidden;
   // ...
// Overrides
public:
   virtual void ActivateFrame(int nCmdShow = -1);
   //...
};

CMainFrame::CMainFrame() : m_bForceHidden(TRUE)
{
   // ...
}

void CMainFrame::ActivateFrame(int nCmdShow)
{
   if(m_bForceHidden)
   {
      nCmdShow = SW_HIDE;
      m_bForceHidden = FALSE;
   }
   CFrameWndEx::ActivateFrame(nCmdShow);
}

Other tricks did not work for me.
Found solution at:
http://forums.codeguru.com/showthread.php?478882-RESOLVED-Can-a-Doc-view-be-hidden-at-startup

A: I found in VS2017 (using BCGControlBar Pro which is what MFC Feature Pack was based on) that you have to handle things in two places:
BOOL CMainFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle, CWnd* pParentWnd, CCreateContext* pContext)
{
    if (!__super::LoadFrame(nIDResource, dwDefaultStyle, pParentWnd, pContext))
    {
        return FALSE;
    }

    // undo what __super::LoadFrame() does where it will set it to SW_NORMAL if not SW_MAXIMIZED
    AfxGetApp()->m_nCmdShow = SW_HIDE;
    
}


BOOL CTheApp::LoadWindowPlacement(CRect& rectNormalPosition, int& nFflags, int& nShowCmd)
{
  BOOL b = __super::LoadWindowPlacement(rectNormalPosition, nFflags, nShowCmd);
  nShowCmd = SW_HIDE;
  return b;
}

