1.创建List以存储Dialog上的控件
CList<CRect,CRect&> m_listRect;//定义一成员变量
2.初始化时将控件存储在Clist中
OnInitDialog()函数中
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();//取下一个子窗体
}
//自适应缩放
3.在OnSize函数中计算当前窗口及控件的合适尺寸
if (m_listRect.GetCount() > 0)
{
CRect dlgNow;
GetWindowRect(&dlgNow);
POSITION pos = m_listRect.GetHeadPosition();//第一个保存的是对话框的Rect
CRect dlgSaved;
dlgSaved = m_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 = m_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();
}
}