第一步:OnInitDialog裏保存對話框及其全部子窗體的Rect區域spa
1 CRect rect; 2 GetWindowRect(&rect); 3 listRect.AddTail(rect);//對話框的區域 4 5 CWnd* pWnd = GetWindow(GW_CHILD);//獲取子窗體 6 while(pWnd) 7 { 8 pWnd->GetWindowRect(rect);//子窗體的區域 9 m_listRect.AddTail(rect); //CList<CRect,CRect> m_listRect成員變量 10 pWnd = pWnd->GetNextWindow();//取下一個子窗體 11 }
第二步:響應OnSize消息code
1 if (listRect.GetCount() > 0) 2 { 3 CRect dlgNow; 4 GetWindowRect(&dlgNow); 5 POSITION pos = listRect.GetHeadPosition();//第一個保存的是對話框的Rect 6 7 CRect dlgSaved; 8 dlgSaved = listRect.GetNext(pos); 9 ScreenToClient(dlgNow); 10 11 float x = dlgNow.Width() * 1.0 / dlgSaved.Width();//根據當前和以前保存的對話框的寬高求比例 12 float y = dlgNow.Height() *1.0/ dlgSaved.Height(); 13 ClientToScreen(dlgNow); 14 15 CRect childSaved; 16 17 CWnd* pWnd = GetWindow(GW_CHILD); 18 while(pWnd) 19 { 20 childSaved = listRect.GetNext(pos);//依次獲取子窗體的Rect 21 childSaved.left = dlgNow.left + (childSaved.left - dlgSaved.left)*x;//根據比例調整控件上下左右距離對話框的距離 22 childSaved.right = dlgNow.right + (childSaved.right - dlgSaved.right)*x; 23 childSaved.top = dlgNow.top + (childSaved.top - dlgSaved.top)*y; 24 childSaved.bottom = dlgNow.bottom + (childSaved.bottom - dlgSaved.bottom)*y; 25 ScreenToClient(childSaved); 26 pWnd->MoveWindow(childSaved); 27 pWnd = pWnd->GetNextWindow(); 28 } 29 } 30 31 //Invalidate(); //強制重繪窗口