對于MDI的MFC程序,在向導中可以設置子窗口啟動時最大化,生成的代碼如下:
BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs)

{
// TODO: Modify the Window class or styles here by modifying the CREATESTRUCT cs
if( !CMDIChildWnd::PreCreateWindow(cs) )
return FALSE;
cs.style = WS_CHILD | WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
| FWS_ADDTOTITLE | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_MAXIMIZE;
return TRUE;
}
但是,在運行至 CChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)和CYourView::OnInitialUpdate()的時候并不能得到子窗口最大時的rect大小。可以通過GetClientRect檢驗。
此時可以用如下代碼解決:
WINDOWPLACEMENT winplacement;
memset(&winplacement, 0, sizeof(winplacement));
winplacement.length = sizeof(WINDOWPLACEMENT);
winplacement.showCmd = SW_SHOWMAXIMIZED;
SetWindowPlacement(&winplacement);MSDN:
Sets the show state and the normal (restored), minimized, and maximized positions for a window.
BOOL SetWindowPlacement(
const WINDOWPLACEMENT*lpwndpl
);


Parameters
lpwndpl
Points to a WINDOWPLACEMENT structure that specifies the new show state and positions.
Return Value

