轉載自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);//設置控件大小 } }