對話框中多個控件隨窗體大小變化:
在類中加成員函數和成員變量:
void SetControlInfo(WORD CtrlId);
protected:
int m_nCx, m_nCy;
CDWordArray m_ctlInfo;
構造函數中初始化:
m_nCx = m_nCy = 0;
加控件信息的函數定義:
void CTestDlg::SetControlInfo(WORD CtrlId)
{
m_ctlInfo.Add(CtrlId);
}
修改OnSize函數:
void CTestDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
float dx = (m_nCx ==0) ? 1 : (float)((float)cx / (float)m_nCx);
float dy = (m_nCy ==0) ? 1 : (float)((float)cy / (float)m_nCy);
if(m_nCx)
{
CRect WndRect;
CWnd *pWnd;
for(int i = 0; i < m_ctlInfo.GetSize(); i++)
{
pWnd = GetDlgItem(m_ctlInfo[i]);
if(!pWnd)
{
TRACE( "Control ID - %d NOT FOUND!!\n ",m_ctlInfo[i]);
continue;
}
pWnd-> GetWindowRect(&WndRect);
ScreenToClient(&WndRect);
WndRect.left = (int)(WndRect.left * dx);
WndRect.right = (int)(WndRect.right* dx);
WndRect.bottom = (int)(WndRect.bottom*dy);
WndRect.top = (int)(WndRect.top*dy);
pWnd-> MoveWindow(&WndRect);
}
}
m_nCx = cx;
m_nCy = cy;
}
在OnInitDialog函數中加入控件ID:
SetControlInfo(IDC_BUTTON);