Why the flash occurs
In order to understand why the application does not minimize properly, we need to look into the MFC code that constructs and displays the main window. By placing a breakpoint in the ProcesShellCommand
function, we see that, by default, the AppWnd OnFileNew
handler is called. OnFileNew
calls the CDocument* CDocument* CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,BOOL bMakeVisible) OpenDocument
, which creates a new document instance, creates a new frame for the document, and finally displays the window by calling InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
, and displays the view and the mainframe window. The reason why the application does not display correctly when a different SW parameter is chosen instead of SW_SHOW
, is because InitialUpdateFrame
CFrameWnd::ActivateFrame()
calls ShowWindow
during the initialization of the window in the function. This implies that the call to ShowWindow
in InitInstance()
is redundant and not needed.
The solution
There are two solutions that can be used to solve the flashing problem. The first solution is to make a subclass of the SingleDocumentTemplate
and call our derived version of OpenDocument
with bMakeVisible = false
for the minimized case. This, however, does not solve the case of using SW_MAXMIMIZE
. Another solution, which is far more simpler and can be used for any ShowWIndow
mode, is to set the application ShowWindow
property prior to initializing the window, as shown below:
Collapse | Copy Code
CSingleDocTemplate * pDocTemplate;
pDocTemplate = new CSingleDocTemplate (
IDR_MAINFRAME,
RUNTIME_CLASS(CMyMFCProgramDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CMyMFCProgramView));
AddDocTemplate(pDocTemplate);
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
this->m_nCmdShow = SW_MAXIMIZE;
if (!ProcessShellCommand(cmdInfo))
return FALSE;
m_pMainWnd->UpdateWindow();
return TRUE;
One may ask if the ShowWindow()
line in the InitInstance
has no purpose, and why Microsoft put that line there in the first place. The answer is, if one decides to construct an SDI application using the MFC application wizard and checks the option not to use MFC, this line is required to show the window. However, Microsoft should have deleted this line if MFC is used. However, since the vast majority of applications initially display ShowWindow
with the SW_SHOW
parameter, calling ShowWindow
twice (the first time in ActivateFrame
, as described above) will not influence the display of the application.