關於MFC中窗口最大化的兩篇文章

文一:轉載(http://dev.csdn.net/htmls/28/28675.htmlhtml

doc_view結構中讓窗口一開始就最大化探討web

做者:enoloo

通常的作法是在 C**App::InitInstance()中,修改爲這樣:
{
 //...
 m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
 m_pMainWnd->UpdateWindow();
 //...
}
或者,還在 CMainFrame::PreCreateWindow(CREATESTRUCT& cs)中,添加:
{
 //...
 cs.style |= WS_MAXIMIZE;
 //...
}ide

這種作法能產生窗口最大化,但效果是顯示的時候窗口從普通大小"閃"到最大化。還有的作法,是先將窗口隱藏,而後再最大化。那麼怎樣使窗口正常一開始出現就最大化?看看下面的流程,從 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;
 }
this

 // bring to top before showing
 BringToTop(nCmdShow);
spa

 if (nCmdShow != -1)
 {
  // show the window as specified
  ShowWindow(nCmdShow); 
 //第一次顯示窗口.net

  // and finally, bring to top after showing
  BringToTop(nCmdShow);
 }
  }
->***rest

從上面能夠看出,CWinApp::ProcessShellCommand函數建立了窗口並顯示,這是窗口第一次顯示,先於:
 m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
 m_pMainWnd->UpdateWindow();code


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

 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();

問題解決。

文二:轉載http://blog.csdn.net/ziren235/archive/2006/05/19/745651.aspx

如何讓MFC窗口啓動時最大化 收藏

這兩天在網上搜了好多,都不行,由於都是同一篇文章的轉載,因而只好本身慢慢摸索。終於,黃天不負苦心人。

只需將App類InitInstance()函數中m_pMainWnd->ShowWindow()的參數改成SW_SHOWMAXIMIZED便可。

微軟MSDN網頁中有相關介紹:

MFC Library Reference  

CWnd::ShowWindow

Sets the visibility state of the window.

BOOL ShowWindow(  int nCmdShow );

Parameters

nCmdShow

Specifies how the CWnd is to be shown. It must be one of the following values:

  • SW_HIDE   Hides this window and passes activation to another window.
  • SW_MINIMIZE   Minimizes the window and activates the top-level window in the system's list.
  • SW_RESTORE   Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.
  • SW_SHOW   Activates the window and displays it in its current size and position.
  • SW_SHOWMAXIMIZED   Activates the window and displays it as a maximized window.
  • SW_SHOWMINIMIZED   Activates the window and displays it as an icon.
  • SW_SHOWMINNOACTIVE   Displays the window as an icon. The window that is currently active remains active.
  • SW_SHOWNA   Displays the window in its current state. The window that is currently active remains active.
  • SW_SHOWNOACTIVATE   Displays the window in its most recent size and position. The window that is currently active remains active.
  • SW_SHOWNORMAL   Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.

Return Value

Nonzero if the window was previously visible; 0 if the CWnd was previously hidden.

相關文章
相關標籤/搜索