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

来源:互联网 发布:香港网络专线 编辑:程序博客网 时间:2024/05/21 18:38

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

CRect rect;GetWindowRect(&rect);m_listRect.AddTail(rect);//对话框的区域CWnd* pWnd = GetWindow(GW_CHILD);//获取子窗体while(pWnd){pWnd->GetWindowRect(rect);//子窗体的区域m_listRect.AddTail(rect);           //CList<CRect,CRect> m_listRect成员变量pWnd = pWnd->GetNextWindow();//取下一个子窗体}

第二步:响应OnSize消息

if (listRect.GetCount() > 0) {  CRect dlgNow;  GetWindowRect(&dlgNow);  POSITION pos = listRect.GetHeadPosition();//第一个保存的是对话框的Rect  CRect dlgSaved;  dlgSaved = listRect.GetNext(pos);  ScreenToClient(dlgNow);  float x = dlgNow.Width() * 1.0 / dlgSaved.Width();//根据当前和之前保存的对话框的宽高求比例  float y = dlgNow.Height()  *1.0/ dlgSaved.Height();  ClientToScreen(dlgNow);  CRect childSaved;  CWnd* pWnd = GetWindow(GW_CHILD);  while(pWnd)  {   childSaved = listRect.GetNext(pos);//依次获取子窗体的Rect   childSaved.left = dlgNow.left + (childSaved.left - dlgSaved.left)*x;//根据比例调整控件上下左右距离对话框的距离   childSaved.right = dlgNow.right + (childSaved.right - dlgSaved.right)*x;   childSaved.top = dlgNow.top + (childSaved.top - dlgSaved.top)*y;   childSaved.bottom = dlgNow.bottom + (childSaved.bottom - dlgSaved.bottom)*y;   ScreenToClient(childSaved);   pWnd->MoveWindow(childSaved);   pWnd = pWnd->GetNextWindow();  } } //Invalidate(); //强制重绘窗口

来自:http://www.cnblogs.com/wind-net/p/3159810.html

0 0
原创粉丝点击