第一步、在对话框类中(.h文件)定义如下变量和函数
定义如下几个变量:
void ReSize(int nID);
BOOL change_flag;
float m_Multiple_height;
float m_Multiple_width;
afx_msg void OnSize(UINT nType, int cx, int cy);
第二步、在OnInitDialog()中 计算出当前对话框的大小与最大化后大小
CRect rect;
::GetWindowRect(m_hWnd,rect);//这里m_hWnd为窗口句柄,如果不存在此变量则在该行代码前加一句:HWND h_Wnd=GetSafeHwnd( );
ScreenToClient(rect);
LONG m_nDlgWidth = rect.right - rect.left;
LONG m_nDlgHeight = rect.bottom - rect.top;
//Calc 分辨率
LONG m_nWidth = GetSystemMetrics(SM_CXSCREEN);
LONG m_nHeight = GetSystemMetrics(SM_CYSCREEN);
//计算放大倍数(要用float值,否则误差很大)
m_Multiple_width = float(m_nWidth)/float(m_nDlgWidth);
m_Multiple_height = float(m_nHeight)/float(m_nDlgHeight);
change_flag = TRUE;//用来判断OnSize执行时,OninitDialg是否已经执行了
第三步、给对话框添加 WM_SIZE消息
//给对话框添加 VM_SIZE消息
void CStuDemoDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if (change_flag)//如果OninitDlg已经调用完毕
{
ReSize(IDC_STATIC_1);
ReSize(IDC_STATIC_2);
ReSize(IDC_EDIT11);//
ReSize(IDC_EDIT12);//
ReSize(IDC_LIST_SHOW);//LIST
ReSize(IDC_BUTTON_ADD);
ReSize(IDC_BUTTON_DEL);
ReSize(IDOK);
ReSize(IDCANCEL);
//恢复放大倍数,并保存 (确保还原时候能够还原到原来的大小)
m_Multiple_width = float(1)/m_Multiple_width;
m_Multiple_height = float(1)/m_Multiple_height;
}
}
第四步、刷新控件:根据比例计算控件缩放的大小,然后movewindow 到新矩形上
void CStuDemoDlg::ReSize(int nID)
{
CRect Rect;
GetDlgItem(nID)->GetWindowRect(Rect);
ScreenToClient(Rect);
//计算控件左上角点
CPoint OldTLPoint,TLPoint;
OldTLPoint = Rect.TopLeft();
TLPoint.x = long(OldTLPoint.x *m_Multiple_width);
TLPoint.y = long(OldTLPoint.y * m_Multiple_height );
//计算控件右下角点
CPoint OldBRPoint,BRPoint; OldBRPoint = Rect.BottomRight();
BRPoint.x = long(OldBRPoint.x *m_Multiple_width);
BRPoint.y = long(OldBRPoint.y * m_Multiple_height );
//移动控件到新矩形
Rect.SetRect(TLPoint,BRPoint);
GetDlgItem(nID)->MoveWindow(Rect,TRUE);
}
第一步、在对话框类中(.h文件)定义如下变量和函数
void ReSize();
POINT old;
afx_msg void OnSize(UINT nType, int cx, int cy);
第二步、在OnInitDialog()中 计算出原始对话框的大小
CRect rect;
GetClientRect(&rect); //取客户区大小
old.x=rect.right-rect.left;
old.y=rect.bottom-rect.top;
第三步、添加 WM_SIZE消息
void CStuDemoDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if (nType==SIZE_RESTORED||nType==SIZE_MAXIMIZED)
{
ReSize();
}
}
第四步、刷新控件函数
void CStuDemoDlg::ReSize()
{
float fsp[2];
POINT Newp; //获取现在对话框的大小
CRect recta;
GetClientRect(&recta); //取客户区大小
Newp.x=recta.right-recta.left;
Newp.y=recta.bottom-recta.top;
fsp[0]=(float)Newp.x/old.x;
fsp[1]=(float)Newp.y/old.y;
CRect Rect;
int woc;
CPoint OldTLPoint,TLPoint; //左上角
CPoint OldBRPoint,BRPoint; //右下角
HWND hwndChild=::GetWindow(m_hWnd,GW_CHILD); //列出所有控件
while(hwndChild)
{
woc=::GetDlgCtrlID(hwndChild);//取得ID
GetDlgItem(woc)->GetWindowRect(Rect);
ScreenToClient(Rect);
OldTLPoint = Rect.TopLeft();
TLPoint.x = long(OldTLPoint.x*fsp[0]);
TLPoint.y = long(OldTLPoint.y*fsp[1]);
OldBRPoint = Rect.BottomRight();
BRPoint.x = long(OldBRPoint.x *fsp[0]);
BRPoint.y = long(OldBRPoint.y *fsp[1]);
Rect.SetRect(TLPoint,BRPoint);
GetDlgItem(woc)->MoveWindow(Rect,TRUE);
hwndChild=::GetWindow(hwndChild, GW_HWNDNEXT);
}
old=Newp;
}
就个人而言,本人还是比较倾向第二种方法,毕竟可以少操作控件ID,否则少了一个布局都会发生变化。