- /*重写Onsize
- */
- void CVehiclesim::OnSize(UINT nType, int cx, int cy)
- {
- CDialog::OnSize(nType, cx, cy);//注意这里的范围
- if (cx <= 1 || cy <= 1 )
- return;
- #ifdef _WIN32_WCE
- m_Grid.MoveWindow(0,0, cx,cy, FALSE);
- #else
- CSize Translate(cx - m_OldSize.cx, cy - m_OldSize.cy);
- ::EnumChildWindows(GetSafeHwnd(), EnumProc1, (LPARAM)&Translate);
- m_OldSize = CSize(cx,cy);
- #endif
- CWnd *pWnd = GetDlgItem(IDC_SIZEBOXX);//窗体右下脚的拖动模块
- if (pWnd)
- pWnd->ShowWindow( (nType == SIZE_MAXIMIZED)? SW_HIDE : SW_SHOW);
- }
- /*对每一个控件进行调整,通常分类实现,如:按比例放大,只是位置发生变化,位置不变
- */
- BOOL CALLBACK EnumProc1(HWND hwnd, LPARAM lParam)
- {
- CWnd* pWnd = CWnd::FromHandle(hwnd);
- CSize* pTranslate = (CSize*) lParam;
- CVehiclesim* pDlg = (CVehiclesim*) pWnd->GetParent();
- if (!pDlg) return FALSE;
- CRect rect;
- pWnd->GetWindowRect(rect);
- pDlg->ScreenToClient(rect);
- if (hwnd == pDlg->s_pGrid.GetSafeHwnd() || hwnd == pDlg->s_static2.GetSafeHwnd())
- {//X,Y,不变 H,W变化
- pWnd->MoveWindow(rect.left, rect.top, rect.Width()+pTranslate->cx, rect.Height()+pTranslate->cy, FALSE);
- }
- else if (hwnd == pDlg->s_SendDatas.GetSafeHwnd() ||hwnd == pDlg->s_static1.GetSafeHwnd())
- {//X,W,Y不变 H变
- pWnd->MoveWindow(rect.left, rect.top, rect.Width(), rect.Height()+pTranslate->cy, FALSE);
- }
- else if(hwnd == pDlg->s_qingkong.GetSafeHwnd() || hwnd == pDlg->s_qingchu.GetSafeHwnd()
- ||hwnd == pDlg->s_shoudong.GetSafeHwnd() || hwnd == pDlg->s_zidong.GetSafeHwnd())
- {//X,W,H不变,Y变化
- pWnd->MoveWindow(rect.left, rect.top+pTranslate->cy,
- rect.Width(), rect.Height(), FALSE);
- }
- else if(hwnd == pDlg->s_jisuan1.GetSafeHwnd() || hwnd == pDlg->s_jisuan2.GetSafeHwnd())
- {//X,W,H不变,Y变化
- pWnd->MoveWindow(rect.left, rect.top+pTranslate->cy,
- rect.Width(), rect.Height(), FALSE);
- }
- else if(hwnd == pDlg->s_fanhui.GetSafeHwnd())
- {//X,Y变化 W,H不变
- pWnd->MoveWindow(rect.left + +pTranslate->cx, rect.top+pTranslate->cy,
- rect.Width(), rect.Height(), FALSE);
- }
- else
- {
- if (pWnd->GetDlgCtrlID() == IDC_SIZEBOXX)//X,Y变化 W,H不变
- pWnd->MoveWindow(rect.left+pTranslate->cx, rect.top+pTranslate->cy,
- rect.Width(), rect.Height(), FALSE);
- else//不变
- pWnd->MoveWindow(rect.left, rect.top, rect.Width(), rect.Height(), FALSE);
- }
- pDlg->Invalidate();
- return TRUE;
- }
- 其他操作:
- - 申明变量 CSize m_OldSize;
- - 消息映射 ON_WM_SIZE()
- - 在初始化函数中运行
- CRect rect;
- GetClientRect(rect);
- m_OldSize = CSize(rect.Width(), rect.Height());
- //该片段来自于http://www.codesnippet.cn/detail/160120148558.html
来源: http://www.codesnippet.cn/detail/160120148558.html