第一步、在对话框类中(.h文件)定义如下变量和函数
定义如下几个变量:
BOOL bChangeFlag; float m_Multiple_height; float m_Multiple_width; void ReSize(int nID);
重载消息 WM_SIZE
afx_msg void OnSize(UINT nType, int cx, int cy);
//对话框初始大小 CRect rect; GetClientRect(rect); LONG m_nDlgWidth = rect.Width(); LONG m_nDlgHeight = rect.Height(); //计算最大化后的大小(即全屏大小) CRect rectWorkArea; SystemParametersInfo(SPI_GETWORKAREA, 0, &rectWorkArea, SPIF_SENDCHANGE); LONG m_nWidth = rectWorkArea.Width(); LONG m_nHeight = rectWorkArea.Height(); //计算放大倍数(要用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消息
//给对话框添加 WM_SIZE消息 void CStuDemoDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: Add your message handler code here if (bChangeFlag)//如果OninitDlg已经调用完毕 { ReSize(IDC_STATIC_1); ReSize(IDC_STATIC_2); ReSize(IDC_EDIT11);// ReSize(IDC_EDIT12);// ReSize(IDC_LIST_SHOW); 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)->GetClientRect(Rect); //计算控件左上角点 CPoint OldTLPoint,NewTLPoint; OldTLPoint = Rect.TopLeft(); NewTLPoint.x = long(OldTLPoint.x * m_Multiple_width); NewTLPoint.y = long(OldTLPoint.y * m_Multiple_height); //计算控件右下角点 CPoint OldBRPoint,NewBRPoint; OldBRPoint = Rect.BottomRight(); NewBRPoint.x = long(OldBRPoint.x * m_Multiple_width); NewBRPoint.y = long(OldBRPoint.y * m_Multiple_height); //移动控件到新矩形 Rect.SetRect(NewTLPoint, NewBRPoint); GetDlgItem(nID)->MoveWindow(Rect, TRUE); }
第一步、在对话框类中(.h文件)定义如下变量和函数
void ReSize(); LONG m_nOldWidth; LONG m_nOldHeight;
重载消息 WM_SIZE
afx_msg void OnSize(UINT nType, int cx, int cy);
//获取客户区大小 CRect rect; GetClientRect(&rect); m_nOldWidth = rect.Width(); m_nOldHeight = rect.Height()
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]; //计算当前窗口大小 CRect rect; GetClientRect(&rect); //计算当前缩放比例 fsp[0] = (float)rect.Width() / m_nOldWidth; fsp[1] = (float)rect.Height() / m_nOldHeight; int nId = 0; //控件ID CPoint OldTLPoint, NewTLPoint; //左上角 CPoint OldBRPoint, NewBRPoint; //右下角 //列出所有控件 HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD); while(hwndChild) { //取得控件ID nId = ::GetDlgCtrlID(hwndChild); //取得控件原来位置 GetDlgItem(nId)->GetClientRect(rect); //计算缩放后的位置 OldTLPoint = rect.TopLeft(); NewTLPoint.x = long(OldTLPoint.x * fsp[0]); NewTLPoint.y = long(OldTLPoint.y * fsp[1]); OldBRPoint = rect.BottomRight(); NewBRPoint.x = long(OldBRPoint.x * fsp[0]); NewBRPoint.y = long(OldBRPoint.y * fsp[1]); //重新设置控件 rect.SetRect(NewTLPoint, NewBRPoint); GetDlgItem(nId)->MoveWindow(rect,TRUE); //继续下一个控件 hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT); } //保存新的窗口大小 m_nOldWidth = rect.Width(); m_nOldHeight = rect.Height(); }