MFC窗口和控件大小等比例变化

第一步:OnInitDialog里保存对话框及其所有子窗体的Rect区域

 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 m_listRect成员变量
10      pWnd = pWnd->GetNextWindow();//取下一个子窗体
11  }

 


第二步:响应OnSize消息

 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(); //强制重绘窗口

 

 

你可能感兴趣的:(MFC)