![]() 图5.3 MDI Cascade的效果 |
![]() 图5.4 MDI Tile的效果 |
| void CMdiView::OnReplaceMenu() { // Load a new menu resource named IDR_SHORT_MENU CMdiDoc* pdoc = GetDocument(); pdoc->m_DefaultMenu = ::LoadMenu(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_SHORT_MENU)); if (pdoc->m_DefaultMenu == NULL) return; // Get the parent window of this view window. The parent window is // a CMDIChildWnd-derived class. We can then obtain the MDI parent // frame window using the CMDIChildWnd*. Then, replace the current // menu bar with the new loaded menu resource. CMDIFrameWnd* frame = ((CMDIChildWnd *) GetParent())->GetMDIFrame(); frame->MDISetMenu(CMenu::FromHandle(pdoc->m_DefaultMenu), NULL); frame->DrawMenuBar(); } |
| BOOL CMDIFrameWnd::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext*) { CMenu* pMenu = NULL; if (m_hMenuDefault == NULL) { // default implementation for MFC V1 backward compatibility pMenu = GetMenu(); ASSERT(pMenu != NULL); // This is attempting to guess which sub-menu is the Window menu. // The Windows user interface guidelines say that the right-most // menu on the menu bar should be Help and Window should be one // to the left of that. int iMenu = pMenu->GetMenuItemCount() - 2; // If this assertion fails, your menu bar does not follow the guidelines // so you will have to override this function and call CreateClient // appropriately or use the MFC V2 MDI functionality. ASSERT(iMenu >= 0); pMenu = pMenu->GetSubMenu(iMenu); ASSERT(pMenu != NULL); } return CreateClient(lpcs, pMenu); } |
| BOOL CMDIFrameWnd::CreateClient(LPCREATESTRUCT lpCreateStruct, CMenu* pWindowMenu) { ASSERT(m_hWnd != NULL); ASSERT(m_hWndMDIClient == NULL); DWORD dwStyle = WS_VISIBLE | WS_CHILD | WS_BORDER | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | MDIS_ALLCHILDSTYLES; // allow children to be created invisible DWORD dwExStyle = 0; // will be inset by the frame if (afxData.bWin4) { // special styles for 3d effect on Win4 dwStyle &= ~WS_BORDER; dwExStyle = WS_EX_CLIENTEDGE; } CLIENTCREATESTRUCT ccs; ccs.hWindowMenu = pWindowMenu->GetSafeHmenu(); // set hWindowMenu for MFC V1 backward compatibility // for MFC V2, window menu will be set in OnMDIActivate ccs.idFirstChild = AFX_IDM_FIRST_MDICHILD; if (lpCreateStruct->style & (WS_HSCROLL|WS_VSCROLL)) { // parent MDIFrame's scroll styles move to the MDICLIENT dwStyle |= (lpCreateStruct->style & (WS_HSCROLL|WS_VSCROLL)); // fast way to turn off the scrollbar bits (without a resize) ModifyStyle(WS_HSCROLL|WS_VSCROLL, 0, SWP_NOREDRAW|SWP_FRAMECHANGED); } // Create MDICLIENT control with special IDC if ((m_hWndMDIClient = ::CreateWindowEx(dwExStyle, _T("mdiclient"), NULL, dwStyle, 0, 0, 0, 0, m_hWnd, (HMENU)AFX_IDW_PANE_FIRST, AfxGetInstanceHandle(), (LPVOID)&ccs)) == NULL) { TRACE(_T("Warning: CMDIFrameWnd::OnCreateClient: failed to create MDICLIENT.") _T(" GetLastError returns 0x%8.8X\n"), ::GetLastError()); return FALSE; } // Move it to the top of z-order ::BringWindowToTop(m_hWndMDIClient); return TRUE; } |
| CMDIFrameWnd *CMDIChildWnd::GetMDIFrame() { HWND hWndMDIClient = ::GetParent(m_hWnd); CMDIFrameWnd *pMDIFrame; pMDIFrame = (CMDIFrameWnd*)CWnd::FromHandle(::GetParent(hWndMDIClient)); return pMDIFrame; } |
![]() 图5.5 一个MDI工程包含的类 |
| BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext *pContext) { … if (!m_wndSplitter.Create(this, 2, 2, // 分割的行、列数 CSize(10, 10), // 最小化尺寸 pContext)) { TRACE0("创建分割失败"); return FALSE; } … return TRUE; } |