////////////////////////////////////////////////////////////////////////////// // MFC Tip #2 -- Vision X Software -- http://www.visionx.com/mfcpro // Centering a CView-derived window // // This source code is provided for instructional purposes, and is released // to the public domain. No claim of ownership is made. Use it at your own // risk. No warranties are expressed or implied. The resulting program may // not be fully functional. No animals were used to test this code. ////////////////////////////////////////////////////////////////////////////// void CMyView::CenterWindow() { // Get dimensions of main client window. CRect rectMain; (CLtApp*)AfxGetApp()->m_pMainWnd->GetClientRect(&rectMain); int xMain = rectMain.Width(); int yMain = rectMain.Height(); // Get dimensions of view window. CRect rectView; GetParent()->GetWindowRect(&rectView); int xView = rectView.Width(); int yView = rectView.Height(); // Calculate the (x,y) coordinate for the view window. int x = (xMain - xView) / 2; int y = (yMain - yView) / 2; // Move the view window. GetParent()->MoveWindow(x, y, xView, yView, TRUE); } void CMainFrame::OnSize(UINT nType, int cx, int cy) { CMDIFrameWnd::OnSize(nType, cx, cy); SendMessageToDescendants(WM_SIZE); } void CMyView::OnSize(UINT nType, int cx, int cy) { CFormView::OnSize(nType, cx, cy); CenterWindow(); }