1.對話框頭文件中定義:
CRect m_DlgRect;//存儲對話框改變前大小搏嗡,以供計算控件相應(yīng)位置及大小時使用
void repaint(UINT id, int last_Width, int now_Width, int last_Height, int now_Height);
2.對話框源文件中實現(xiàn):
void TestDlg::repaint(UINT id, int last_Width, int now_Width, int last_Height, int now_Height)//更新控件位置和大小函數(shù)套硼,可以根據(jù)需要自行修改
{
CRect rect;
CWnd *wnd = NULL;
wnd = GetDlgItem(id);
if (NULL == wnd)
{
MessageBox(_T("相應(yīng)控件不存在"));
}
wnd->GetWindowRect(&rect);
ScreenToClient(&rect);
rect.left = (long)((double)rect.left / (double)last_Width*(double)now_Width);
rect.right = (long)((double)rect.right / (double)last_Width*(double)now_Width);
rect.top = (long)((double)rect.top / (double)last_Height*(double)now_Height);
rect.bottom = (long)((double)rect.bottom / (double)last_Height*(double)now_Height);
wnd->MoveWindow(&rect);
}
3.Ctrl+Shift+x打開類向?qū)Ю缘樱瑢崿F(xiàn)WM_SIZE消息
4.在OnSize里調(diào)用repait函數(shù)
void TestDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
if (0 == m_DlgRect.left && 0 == m_DlgRect.right
&& 0 == m_DlgRect.top && 0 == m_DlgRect.bottom)//第一次啟動對話框時的大小變化不做處理
{
}
else
{
if (0 == cx && 0 == cy)//如果是按下了最小化忘渔,則觸發(fā)條件婚陪,這時不保存對話框數(shù)據(jù)
{
return;
}
BOOL bWindowMaxFlag = FALSE;
CRect rectDlgChangeSize;
GetClientRect(&rectDlgChangeSize);//存儲對話框大小改變后對話框大小數(shù)據(jù)
repaint(IDC_RICHEDIT21, m_DlgRect.Width(), rectDlgChangeSize.Width(), m_DlgRect.Height(), rectDlgChangeSize.Height());//重繪函數(shù)棍丐,用以更新對話框上控件的位置和大小
}
GetClientRect(&m_DlgRect); //save size of dialog
Invalidate();//更新窗口
}
···