關於MFC中窗口最大化

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

作者:enoloo

一般的做法是在 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();

問題解決。

 

文二:轉載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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章