先获得对话框的大小,计算后设置控件的位置 void GetWindowRect( LPRECT lpRect ) const; void MoveWindow( int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE );
响应OnSize函数,然后利用MoveWindow
响应OnSize函数 控件.MoveWindow(--)
截获最大话消息,然后使用MoveWindow
void CDlg3Dlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: Add your message handler code here CRect lpRect; GetWindowRect(&lpRect); CSize szMWD = lpRect.Size(); int a = szMWD.cx; int b = szMWD.cy; GetDlgItem(IDC_LIST1)->MoveWindow( (1024 - a)/2, (768 - b)/2,a , b ,TRUE);
void GetWindowRect( LPRECT lpRect ) const;
void MoveWindow( int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE );
控件.MoveWindow(--)
{
CDialog::OnSize(nType, cx, cy); // TODO: Add your message handler code here
CRect lpRect;
GetWindowRect(&lpRect);
CSize szMWD = lpRect.Size();
int a = szMWD.cx;
int b = szMWD.cy;
GetDlgItem(IDC_LIST1)->MoveWindow( (1024 - a)/2, (768 - b)/2,a , b ,TRUE);
}出错,有没有什么好的方法把ListBox重新放在对话框中间?
但是,同样的代码放到OnInitDialog()就能成功,为什么呢?
在OnInitDialog()里写
m_hInit = true;//这个变量作为对话框的成员变量,并且在对话框构造函数里负初值false然后在OnSize()里写
void CDlg3Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if(m_hInit){
// TODO: Add your message handler code here
CRect lpRect;
GetWindowRect(&lpRect);
CSize szMWD = lpRect.Size();
int a = szMWD.cx;
int b = szMWD.cy;
GetDlgItem(IDC_LIST1)->MoveWindow( (1024 - a)/2, (768 - b)/2,a , b ,TRUE);
}
}
{
CDialog::OnSize(nType, cx, cy); if (IsWindow(GetDlgItem(IDC_LIST1))) {
CRect lpRect;
GetWindowRect(&lpRect);
CSize szMWD = lpRect.Size();
int a = szMWD.cx;
int b = szMWD.cy;
GetDlgItem(IDC_LIST1)->MoveWindow( (1024 - a)/2, (768 - b)/2,a , b ,TRUE);
}
}