天天看點

mfc應用視窗最大化顯示

來自:http://hi.baidu.com/lz3272/blog/item/236932126131f70b5aaf538a.html

1.一般的做法是在 C**App::InitInstance()中,修改成這樣:

{

//...

m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);

m_pMainWnd->UpdateWindow();

//...

}

或者,還在 CMainFrame::PreCreateWindow(CREATESTRUCT& cs)中,添加:

{

//...

cs.style |= WS_MAXIMIZE;

//...

}

這種做法能産生視窗最大化,但效果是顯示的時候視窗從普通大小"閃"到最大化。還有的做法,是先将視窗隐藏,然後再最大化。那麼怎樣使視窗正常一開始出現就最大化?看看下面的流程,從 C**App::InitInstance()中的ProcessShellCommand(...)開始:

{

//...

//ProcessShellCommand中第一次顯示了視窗

if (!ProcessShellCommand(cmdInfo))

   return FALSE;

m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);

m_pMainWnd->UpdateWindow();

//...

}

->CWinApp::ProcessShellCommand 

->AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL) 

//如果你自己處理了ID_FILE_NEW要調用CWinApp::OnFileNew()

->CWinApp::OnFileNew()

->CDocManager::OnFileNew() 

->CSingleDocTemplate::OpenDocumentFile //目前文檔模闆初始化

->CSingleDocTemplate::CreateNewDocument //建立文檔

//加載資源并建立主視窗(順便建立視圖),但沒顯示

->CSingleDocTemplate::CreateNewFrame 

->CFrameWnd::InitialUpdateFrame

{

//...

int nCmdShow = -1;      // default

CWinApp* pApp = AfxGetApp();

if (pApp != NULL && pApp->m_pMainWnd == this)

{

   nCmdShow = pApp->m_nCmdShow; // use the parameter from WinMain

   pApp->m_nCmdShow = -1; // set to default after first time

}

ActivateFrame(nCmdShow); //在這裡第一次顯示視窗

//...

}

->CFrameWnd::ActivateFrame(int nCmdShow)

// nCmdShow is the normal show mode this frame should be in

{

// translate default nCmdShow (-1)

if (nCmdShow == -1)

{

   if (!IsWindowVisible())

    nCmdShow = SW_SHOWNORMAL;

   else if (IsIconic())

    nCmdShow = SW_RESTORE;

}

// bring to top before showing

BringToTop(nCmdShow);

if (nCmdShow != -1)

{

  // show the window as specified

   ShowWindow(nCmdShow); //第一次顯示視窗

  // and finally, bring to top after showing

   BringToTop(nCmdShow);

}

}

->***

從上面可以看出,CWinApp::ProcessShellCommand函數建立了視窗并顯示,這是視窗第一次顯示,先于:

m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);

m_pMainWnd->UpdateWindow();

怎麼解決問題? 然視窗第一次顯示就最大化?

CCommandLineInfo cmdInfo;

ParseCommandLine(cmdInfo);

// Dispatch commands specified on the command line

//在ParseCommandLine之後,ProcessShellCommand之前,添加這句!!!

m_nCmdShow = SW_SHOWMAXIMIZED; 

if (!ProcessShellCommand(cmdInfo))

   return FALSE;

// The one and only window has been initialized, so show and update it.

m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);

m_pMainWnd->UpdateWindow();

問題解決。

2.在MainFram裡面的PreCreateWindow裡面直接最大化

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)

{

if( !CFrameWnd::PreCreateWindow(cs) )

   return FALSE;

// TODO: Modify the Window class or styles here by modifying

// the CREATESTRUCT cs

cs.style|=WS_MAXIMIZEBOX|WS_MAXIMIZE;

int nWidth;          // window width

int nHeight;          // window height

nWidth=GetSystemMetrics(SM_CXSCREEN);//獲得系統的分辨率

nHeight=GetSystemMetrics(SM_CYSCREEN);//獲得系統的分辨率

cs.cx=nWidth;

cs.cy=nHeight;

return TRUE;

}

在CFabricIRTesterApp.cpp中修改:BOOL CFabricIRTesterApp::InitInstance()

m_pMainWnd->ShowWindow(SW_SHOW);

為m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);

繼續閱讀