实现对话框伸缩功能的通用代码:
void CCpuUsageDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if (listRect.GetCount()>0)//看链表是否为空
{
CRect rectDlgNow;
GetWindowRect(&rectDlgNow);//得到当前对话框的坐标
POSITION mp=listRect.GetHeadPosition();取得存储在链表中的头元素,其实就是前边的对话框坐标
CRect rectDlgSaved;
rectDlgSaved=listRect.GetNext(mp);
ScreenToClient(rectDlgNow);
float fRateScaleX=(float)(rectDlgNow.right-rectDlgNow.left)/(rectDlgSaved.right-rectDlgSaved.left);//拖拉后的窗口大小与原来窗口大小的比例
float fRateScaleY=(float)(rectDlgNow.bottom-rectDlgNow.top)/(rectDlgSaved.bottom-rectDlgSaved.top);
ClientToScreen(rectDlgNow);
CRect rectChildSaved;
CWnd *pWndChild=GetWindow(GW_CHILD);
while (pWndChild)
{
rectChildSaved=listRect.GetNext(mp);
rectChildSaved.left=rectDlgNow.left+(int)((rectChildSaved.left-rectDlgSaved.left)*fRateScaleX);
rectChildSaved.top=rectDlgNow.top+(int)((rectChildSaved.top-rectDlgSaved.top)*fRateScaleY);
rectChildSaved.right=rectDlgNow.right+(int)((rectChildSaved.right-rectDlgSaved.right)*fRateScaleX);
rectChildSaved.bottom=rectDlgNow.bottom+(int)((rectChildSaved.bottom-rectDlgSaved.bottom)*fRateScaleY);
ScreenToClient(rectChildSaved);
pWndChild->MoveWindow(rectChildSaved);
pWndChild = pWndChild->GetNextWindow();
}
}
Invalidate(); //强制重绘窗口
}
同时在OnInitDialog()代码末尾添加:
CRect rectWnd;
GetWindowRect(&rectWnd);//得到当前窗口(对话框)的坐标
listRect.AddTail(&rectWnd);//将坐标添加到链表listRect的末尾(注意 CList <CRect,CRect> listRect;是类的成员变量)
CWnd *pWndChild=GetWindow(GW_CHILD);//
while (pWndChild)//依次得到对话框上控件的坐标,并将所有的控件坐标存储在链表中
{
pWndChild->GetWindowRect(&rectWnd);
listRect.AddTail(&rectWnd);//由于依次将控件坐标添加到链表末尾,所以开头的坐标是对话框的坐标
pWndChild=pWndChild->GetNextWindow();
}
同时注意在控件“边框”要选择“调整大小”