MFC让控件随窗口大小而改变
转载自http://blog.csdn.net/chw1989/article/details/7488711
大小和位置都改变(亲测可行)
1.首先为窗体类添加CRect m_rect,该成员变量用来记录窗体的当前大小。
2.在类向导里面(Ctrl+W),为窗体添加消息WM_SIZE的响应函数OnSize();
注意if(nType==1) return;这一句必须添加,否则最小化恢复的时候会出错
-
void CPaperManagementDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); if(nType==1) return;//最小化则什么都不做 // TODO: Add your message handler code here CWnd *pWnd; pWnd = GetDlgItem(IDC_STATIC); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_FILE_TREE); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_EDIT_NAME); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_EDIT_REFERENCE); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_EDIT_SUMMARY); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_EDIT_REMARK); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_BUTTON_UPDATE); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_BUTTON_SAVE); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_STATIC_1); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_STATIC_2); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_STATIC_3); ChangeSize(pWnd, cx, cy); pWnd = GetDlgItem(IDC_STATIC_4); ChangeSize(pWnd, cx, cy); GetClientRect(&m_rect);// 将变化后的对话框大小设为旧大小 }
changesize是自己添加的函数
void CPaperManagementDlg::ChangeSize(CWnd *pWnd, int cx, int cy) { if(pWnd) //判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建 { CRect rect; //获取控件变化前的大小 pWnd->GetWindowRect(&rect); ScreenToClient(&rect);//将控件大小转换为在对话框中的区域坐标 // cx/m_rect.Width()为对话框在横向的变化比例 rect.left=rect.left*cx/m_rect.Width();//调整控件大小 rect.right=rect.right*cx/m_rect.Width(); rect.top=rect.top*cy/m_rect.Height(); rect.bottom=rect.bottom*cy/m_rect.Height(); pWnd->MoveWindow(rect);//设置控件大小 } }