第一步:在类中加入成员函数和成员变量
- public:
- void SetControlInfo(WORD CtrlId);
- protected:
- int m_old_cx,m_old_cy;
- CDWordArray m_control_info;
第二步 在类的构造函数中初始化变量
- CgdiDlg::CgdiDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CgdiDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_old_cx=m_old_cy=0;
- }
注意: 是在类的构造函数中初始化,不是在类的Init函数中进行初始化
第三步 定义实现成员函数SetControlInfo(WORD CtrlId)
- void CgdiDlg::SetControlInfo(WORD CtrlId)
- {
- m_control_info.Add(CtrlId);
- }
第四步 修改OnSize消息函数
- void CgdiDlg::OnSize(UINT nType, int cx, int cy)
- {
- if (cx==0||cy==0)
- {
- cx=800;
- cy=600;
- }
- CDialog::OnSize(nType, cx, cy);
- float dx_percent = (m_old_cx ==0)? 1 : (float)((float)cx/(float)m_old_cx);
- float dy_percent = (m_old_cy ==0)? 1 : (float)((float)cy/(float)m_old_cy);
- if(m_old_cx)
- {
- CRect WndRect;
- CWnd *pWnd;
- for(int i = 0; i < m_control_info.GetSize(); i++)
- {
- pWnd = GetDlgItem(m_control_info[i]);
- if(!pWnd)
- {
- TRACE( "Control ID - %d NOT FOUND!!\n ",m_control_info[i]);
- continue;
- }
- pWnd-> GetWindowRect(&WndRect);
- ScreenToClient(&WndRect);
- WndRect.left = (int)(WndRect.left*dx_percent);
- WndRect.right = (int)(WndRect.right* dx_percent);
- WndRect.bottom = (int)(WndRect.bottom*dy_percent);
- WndRect.top = (int)(WndRect.top*dy_percent);
- pWnd-> MoveWindow(&WndRect);
- }
- }
- m_old_cx = cx;
- m_old_cy = cy;
- }
-
- <第五步 在OnInitDialog初始化函数中 加入控件ID
- SetControlInfo(IDC_BUTTON1);
- 参考资料:"http://hi.baidu.com/jues17/blog/item/61d5a7ff289ae3265c60089c.html"