两者用途:均表示改变控件的大小和位置
分别介绍:
(1)、SetWindowPos:改变一个子窗口,弹出式窗口或顶层窗口的尺寸,位置和Z序。
BOOL SetWindowPos(
const CWnd* pWndInsertAfter,
int x, //Specifies the new position of the left side of the window.
int y, //Specifies the new position of the top of the window.
int cx, //Specifies the new width of the window.
int cy, //Specifies the new height of the window.
UINT nFlags //Specifies sizing and positioning options.
);
nFlags常用取值:
SWP_NOZORDER:忽略第一个参数;
SWP_NOMOVE:忽略x、y,维持位置不变;
SWP_NOSIZE:忽略cx、cy,维持大小不变;
CWnd *pWnd;
pWnd = GetDlgItem( IDC_BUTTON_OPEN ); //获取控件指针,IDC_BUTTON1为控件ID号
pWnd->SetWindowPos( NULL,50,80,0,0,SWP_NOZORDER | SWP_NOSIZE ); //把按钮移到窗口的(50,80)处,大小不变
pWnd = GetDlgItem( IDC_EDIT_HTMLPATH );
pWnd->SetWindowPos( NULL,0,0,100,80,SWP_NOZORDER | SWP_NOMOVE ); //把编辑控件的大小设为(100,80),位置不变
pWnd = GetDlgItem( IDC_EDIT_PARSEHTML );
pWnd->SetWindowPos( NULL,50,80,100,80,SWP_NOZORDER ); //编辑控件的大小和位置都改变
(2)、MoveWindow:功能是改变指定窗口的位置和大小。对子窗口来说,位置和大小取决于父窗口客户区的左上角;对于Owned窗口,位置和大小取决于屏幕左上角。
void MoveWindow(
int x, //Specifies the new position of the left side of the CWnd.
int y, //Specifies the new position of the top of the CWnd.
int nWidth, //Specifies the new width of the CWnd.
int nHeight, //Specifies the new height of the CWnd.
BOOL bRepaint = TRUE
);
void MoveWindow(
LPCRECT lpRect, //The CRect object or RECT structure that specifies the new size and position.
BOOL bRepaint = TRUE
);
bRepaint指定了是否要重画CWnd。如果为TRUE,则CWnd象通常那样在OnPaint消息处理函数中接收到一条WM_PAINT消息。如果这个参数为FALSE,则不会发生任何类型的重画操作。这应用于客户区、非客户区(包括标题条和滚动条)和由于CWnd移动而露出的父窗口的任何部分。当这个参数为FALSE的时候,应用程序必须明确地使CWnd和父窗口中必须重画的部分无效或重画。
void CDialogQual::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
CWnd *pWnd = GetDlgItem(IDC_PROGRESS_PARSEHTML);
if(pWnd && pWnd->GetSafeHwnd())
{
pWnd->MoveWindow(5,18,cx-200,18);
}
pWnd = GetDlgItem(IDC_EDIT_HTMLPATH);
if(pWnd && pWnd->GetSafeHwnd())
{
pWnd->MoveWindow(5,40,cx-280,25);
}
pWnd = GetDlgItem(IDC_BUTTON_OPEN);
if(pWnd && pWnd->GetSafeHwnd())
{
pWnd->MoveWindow(cx-274,40,80,25);
}
pWnd = GetDlgItem(IDC_LIST_PARSE);
if(pWnd && pWnd->GetSafeHwnd())
{
pWnd->MoveWindow(5,70,cx-200,120);
}
pWnd = GetDlgItem(IDC_CHECK_SHOWLOG);
if(pWnd && pWnd->GetSafeHwnd())
{
CRect rect;
pWnd->GetClientRect(&rect);
pWnd->MoveWindow(5,192,rect.Width(),rect.Height());
}
pWnd = GetDlgItem(IDC_EDIT_PARSEHTML);
if(pWnd && pWnd->GetSafeHwnd())
{
pWnd->MoveWindow(5,205,cx-200,cy-205);
}
pWnd = GetDlgItem(IDC_STATIC_PARSE);
if(pWnd && pWnd->GetSafeHwnd())
{
pWnd->MoveWindow(0,0,cx-190,cy);
}
}
上述代码在OnSize中执行,主要用于锁定控件的位置与大小,这样当控件随着Dialog最大化变化的时候,控件相对于Dialog的位置能够保持自适应。