SetWindowPos详解

其内部定义:
BOOL SetWindowPos(
HWND hWnd, // handle of window
HWND hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
UINT uFlags // window-positioning flags
);
参数解释:
hWnd
Identifies the window. 窗口句柄
hWndInsertAfter 窗口叠层位置
Identifies the window to precede the positioned window in the Z order. This parameter must be a window handle or one of the following values:
Value Meaning
设置值:
HWND_BOTTOM =1在底层的"普通层"
Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows.
HWND_NOTOPMOST= -2 在所有非"普通层"之上的"普通层"
Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window.
HWND_TOP =0在顶层的"普通层"
Places the window at the top of the Z order.
HWND_TOPMOST = -1在所用"普通层"之上的"最顶层"
Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.
X 窗口左坐标
Specifies the new position of the left side of the window.
Y 窗口上坐标
Specifies the new position of the top of the window.
cx 窗口宽度(单位为pixels)
Specifies the new width of the window, in pixels.
cy 窗口高度(单位为pixels)
Specifies the new height of the window, in pixels.
uFlags 附加参数
Specifies the window sizing and positioning flags. This parameter can be a combination of the following values:
Value Meaning 参数值:
SWP_DRAWFRAME =0x0020窗口带边框
Draws a frame (defined in the window’s class description) around the window.
SWP_FRAMECHANGED =0x0020发送边框改变消息
Sends a WM_NCCALCSIZE message to the window, even if the window’s size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window’s size is being changed.
SWP_HIDEWINDOW =0x0080窗口隐藏
Hides the window.
SWP_NOACTIVATE =0x0010不激活窗口
Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter).
SWP_NOCOPYBITS =0x0100不保留显示缓存的拷贝
Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned.
SWP_NOMOVE =0x0002不可移动窗口(忽略X,Y参数)
Retains the current position (ignores the X and Y parameters).
SWP_NOOWNERZORDER =0x0200不改变父窗口叠层顺序
Does not change the owner window’s position in the Z order.
SWP_NOREDRAW =0x0008不重画窗口
Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing.
SWP_NOREPOSITION =0x0200
Same as the SWP_NOOWNERZORDER flag.
SWP_NOSENDCHANGING =0x0400不接受窗口位置改变的消息
Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
SWP_NOSIZE =0x0001窗口大小不变(忽略CX,CY参数)
Retains the current size (ignores the cx and cy parameters).
SWP_NOZORDER =0x0004不改变叠层顺序(忽略hWndInsertAfter参数)
Retains the current Z order (ignores the hWndInsertAfter parameter).
SWP_SHOWWINDOW =0x0040显示窗口
Displays the window.
注意:如果要使用上述多个uFlags参数, 只要将几个参数相加的和赋给uFlags即可.
Return Values返回值
函数执行成功返回非0值, 不成功返回0.
Remarks 特别说明(不翻译了)
If the SWP_SHOWWINDOW or SWP_HIDEWINDOW flag is set, the window cannot be moved or sized.
All coordinates for child windows are client coordinates (relative to the upper-left corner of the parent window’s client area).
A window can be made a topmost window either by setting the hWndInsertAfter parameter to HWND_TOPMOST and ensuring that the SWP_NOZORDER flag is not set, or by setting a window’s position in the Z order so that it is above any existing topmost windows. When a non-topmost window is made topmost, its owned windows are also made topmost. Its owners, however, are not changed.
If neither the SWP_NOACTIVATE nor SWP_NOZORDER flag is specified (that is, when the application requests that a window be simultaneously activated and its position in the Z order changed), the value specified in hWndInsertAfter is used only in the following circumstances:
· Neither the HWND_TOPMOST nor HWND_NOTOPMOST flag is specified in hWndInsertAfter.
· The window identified by hWnd is not the active window.
An application cannot activate an inactive window without also bringing it to the top of the Z order. Applications can change an activated window’s position in the Z order without restrictions, or it can activate a window and then move it to the top of the topmost or non-topmost windows.
If a topmost window is repositioned to the bottom (HWND_BOTTOM) of the Z order or after any non-topmost window, it is no longer topmost. When a topmost window is made non-topmost, its owners and its owned windows are also made non-topmost windows.
A non-topmost window can own a topmost window, but the reverse cannot occur. Any window (for example, a dialog box) owned by a topmost window is itself made a topmost window, to ensure that all owned windows stay above their owner.
If an application is not in the foreground, and should be in the foreground, it must call the SetForegroundWindow function.
以上来源于MSDN.
BOOL SetWindowPos(
HWND hWnd, // handle of window
HWND hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
UINT uFlags // window-positioning flags
);
参数解释:
hWnd
Identifies the window. 窗口句柄
hWndInsertAfter 窗口叠层位置
Identifies the window to precede the positioned window in the Z order. This parameter must be a window handle or one of the following values:
Value Meaning
设置值:
HWND_BOTTOM =1在底层的"普通层"
Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows.
HWND_NOTOPMOST= -2 在所有非"普通层"之上的"普通层"
Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window.
HWND_TOP =0在顶层的"普通层"
Places the window at the top of the Z order.
HWND_TOPMOST = -1在所用"普通层"之上的"最顶层"
Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.
X 窗口左坐标
Specifies the new position of the left side of the window.
Y 窗口上坐标
Specifies the new position of the top of the window.
cx 窗口宽度(单位为pixels)
Specifies the new width of the window, in pixels.
cy 窗口高度(单位为pixels)
Specifies the new height of the window, in pixels.
uFlags 附加参数
Specifies the window sizing and positioning flags. This parameter can be a combination of the following values:
Value Meaning 参数值:
SWP_DRAWFRAME =0x0020窗口带边框
Draws a frame (defined in the window’s class description) around the window.
SWP_FRAMECHANGED =0x0020发送边框改变消息
Sends a WM_NCCALCSIZE message to the window, even if the window’s size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window’s size is being changed.
SWP_HIDEWINDOW =0x0080窗口隐藏
Hides the window.
SWP_NOACTIVATE =0x0010不激活窗口
Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter).
SWP_NOCOPYBITS =0x0100不保留显示缓存的拷贝
Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned.
SWP_NOMOVE =0x0002不可移动窗口(忽略X,Y参数)
Retains the current position (ignores the X and Y parameters).
SWP_NOOWNERZORDER =0x0200不改变父窗口叠层顺序
Does not change the owner window’s position in the Z order.
SWP_NOREDRAW =0x0008不重画窗口
Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing.
SWP_NOREPOSITION =0x0200
Same as the SWP_NOOWNERZORDER flag.
SWP_NOSENDCHANGING =0x0400不接受窗口位置改变的消息
Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
SWP_NOSIZE =0x0001窗口大小不变(忽略CX,CY参数)
Retains the current size (ignores the cx and cy parameters).
SWP_NOZORDER =0x0004不改变叠层顺序(忽略hWndInsertAfter参数)
Retains the current Z order (ignores the hWndInsertAfter parameter).
SWP_SHOWWINDOW =0x0040显示窗口
Displays the window.
注意:如果要使用上述多个uFlags参数, 只要将几个参数相加的和赋给uFlags即可.
Return Values返回值
函数执行成功返回非0值, 不成功返回0.
Remarks 特别说明(不翻译了)
If the SWP_SHOWWINDOW or SWP_HIDEWINDOW flag is set, the window cannot be moved or sized.
All coordinates for child windows are client coordinates (relative to the upper-left corner of the parent window’s client area).
A window can be made a topmost window either by setting the hWndInsertAfter parameter to HWND_TOPMOST and ensuring that the SWP_NOZORDER flag is not set, or by setting a window’s position in the Z order so that it is above any existing topmost windows. When a non-topmost window is made topmost, its owned windows are also made topmost. Its owners, however, are not changed.
If neither the SWP_NOACTIVATE nor SWP_NOZORDER flag is specified (that is, when the application requests that a window be simultaneously activated and its position in the Z order changed), the value specified in hWndInsertAfter is used only in the following circumstances:
· Neither the HWND_TOPMOST nor HWND_NOTOPMOST flag is specified in hWndInsertAfter.
· The window identified by hWnd is not the active window.
An application cannot activate an inactive window without also bringing it to the top of the Z order. Applications can change an activated window’s position in the Z order without restrictions, or it can activate a window and then move it to the top of the topmost or non-topmost windows.
If a topmost window is repositioned to the bottom (HWND_BOTTOM) of the Z order or after any non-topmost window, it is no longer topmost. When a topmost window is made non-topmost, its owners and its owned windows are also made non-topmost windows.
A non-topmost window can own a topmost window, but the reverse cannot occur. Any window (for example, a dialog box) owned by a topmost window is itself made a topmost window, to ensure that all owned windows stay above their owner.
If an application is not in the foreground, and should be in the foreground, it must call the SetForegroundWindow function.
以上来源于MSDN.
事究竟、度无极、到彼岸 http://bbs.openlab.net.cn/blogs/ayd/default.aspx

让阿伊达抢先了,我在加些自己的理解吧。
在各位高手的悉心指点下,我终于比较全面的了解了SetWindowPos 函数,学会了在VB中创建浮动面板等窗体的技术。我在MSDN中查到一些 SetWindowPos 的英文说明资料,现把其大意翻译如下,并加入一些自已的理解。同时还有些不大明白的地方,请大家共同讨论。下列说明信息中是英文的都是我不清楚而没有翻译的,没有说明值为多少的是因为没有查到有关说明。
SetWindowPos
SetWindowPos 函数用于改变子窗体,弹出式窗体或浮动面板窗体的大小,位置和Z向顺序(即前后顺序)。它是Windows API 函数,在VB中定义如下:
Public Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, _
ByVal wFlags As Long)
As Long
参数说明:
hWnd :窗体句柄,说明对哪个窗体进行操作。值可取为 frmName.hWnd 或 me.hWnd。
hWndInsertAfter Z向(前后)顺序,取下列值:
:
Value 常量(值)
Meaning含义
HWND_BOTTOM (1)
将窗体置于所有窗体的后面,包括主窗口。
HWND_NOTOPMOST(-2)
取消窗体的总在最前状态(HWND_TOPMOST),使之位于所有非总在最前窗体的前面和总在最前窗体的后面。如果窗体原来就为非总在最前窗体,则本设置不产生操作。
HWND_TOP(0)
将窗体置于最前面
HWND_TOPMOST(-1)
设置窗体为总在最前状态。总在最前窗体在任何时候总位于一般窗体的前面,但不一定总在所有窗体的最前面。
附:在API浏览器中,我还查到以下两个此处没有使用的常量:
Public Const HWND_BROADCAST = &HFFFF&
Public Const HWND_DESKTOP = 0
X :窗体X向位置,根据用户设定方式赋值
Y :窗体Y向位置,根据用户设定方式赋值
cx :窗体宽度,单位像素(pixels )
cy :窗体高度,单位像素(pixels )
uFlags
指定引用本函数的方式及窗体的一些状态,取值如下:
Value常量(值)
Meaning含义
SWP_ASYNCWINDOWPOS ( ? )
If the calling thread does not own the window, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
SWP_DEFERERASE ( ?)
Prevents generation of the WM_SYNCPAINT message.
SWP_DRAWFRAME (SWP_FRAMECHANGED)
在窗体周围加边框
SWP_FRAMECHANGED (&H20 )
Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed.
SWP_HIDEWINDOW (&H80 )
隐藏窗体
SWP_NOACTIVATE (&H10 )
不激活窗体。如果不设本值,则默认使用该函数时将激活窗体并置于所有窗体的前面(同时受 hWndInsertAfter 影响)
SWP_NOCOPYBITS (&H100 )
清空窗体的所有用户输入信息。如不设本值,则默认保留所有用户输入信息。
SWP_NOMOVE (&H2 )
保持窗体位置(忽略X,Y 参数)
SWP_NOOWNERZORDER (&H200)
不改变窗体的Zorder 值。 ???我不知道有什么影响。
SWP_NOREDRAW (&H8 )
不刷新窗体内容
SWP_NOREPOSITION (SWP_NOOWNERZORDER )
Same as the SWP_NOOWNERZORDER flag.
SWP_NOSENDCHANGING ( ? )
Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
SWP_NOSIZE (&H1)
保持窗体大小(忽略 cx,cy 参数)
SWP_NOZORDER (&H4 )
保留窗体前后顺序(忽略 hWndInsertAfter 参数)
SWP_SHOWWINDOW (&H40)
显示窗体
返回值
成功调用返回非零值。不成功返回0。
【在 阿伊达 (阿伊达) 的大作中提到:】
其内部定义:
BOOL SetWindowPos(
HWN...
在各位高手的悉心指点下,我终于比较全面的了解了SetWindowPos 函数,学会了在VB中创建浮动面板等窗体的技术。我在MSDN中查到一些 SetWindowPos 的英文说明资料,现把其大意翻译如下,并加入一些自已的理解。同时还有些不大明白的地方,请大家共同讨论。下列说明信息中是英文的都是我不清楚而没有翻译的,没有说明值为多少的是因为没有查到有关说明。
SetWindowPos
SetWindowPos 函数用于改变子窗体,弹出式窗体或浮动面板窗体的大小,位置和Z向顺序(即前后顺序)。它是Windows API 函数,在VB中定义如下:
Public Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, _
ByVal wFlags As Long)
As Long
参数说明:
hWnd :窗体句柄,说明对哪个窗体进行操作。值可取为 frmName.hWnd 或 me.hWnd。
hWndInsertAfter Z向(前后)顺序,取下列值:
:
Value 常量(值)
Meaning含义
HWND_BOTTOM (1)
将窗体置于所有窗体的后面,包括主窗口。
HWND_NOTOPMOST(-2)
取消窗体的总在最前状态(HWND_TOPMOST),使之位于所有非总在最前窗体的前面和总在最前窗体的后面。如果窗体原来就为非总在最前窗体,则本设置不产生操作。
HWND_TOP(0)
将窗体置于最前面
HWND_TOPMOST(-1)
设置窗体为总在最前状态。总在最前窗体在任何时候总位于一般窗体的前面,但不一定总在所有窗体的最前面。
附:在API浏览器中,我还查到以下两个此处没有使用的常量:
Public Const HWND_BROADCAST = &HFFFF&
Public Const HWND_DESKTOP = 0
X :窗体X向位置,根据用户设定方式赋值
Y :窗体Y向位置,根据用户设定方式赋值
cx :窗体宽度,单位像素(pixels )
cy :窗体高度,单位像素(pixels )
uFlags
指定引用本函数的方式及窗体的一些状态,取值如下:
Value常量(值)
Meaning含义
SWP_ASYNCWINDOWPOS ( ? )
If the calling thread does not own the window, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
SWP_DEFERERASE ( ?)
Prevents generation of the WM_SYNCPAINT message.
SWP_DRAWFRAME (SWP_FRAMECHANGED)
在窗体周围加边框
SWP_FRAMECHANGED (&H20 )
Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed.
SWP_HIDEWINDOW (&H80 )
隐藏窗体
SWP_NOACTIVATE (&H10 )
不激活窗体。如果不设本值,则默认使用该函数时将激活窗体并置于所有窗体的前面(同时受 hWndInsertAfter 影响)
SWP_NOCOPYBITS (&H100 )
清空窗体的所有用户输入信息。如不设本值,则默认保留所有用户输入信息。
SWP_NOMOVE (&H2 )
保持窗体位置(忽略X,Y 参数)
SWP_NOOWNERZORDER (&H200)
不改变窗体的Zorder 值。 ???我不知道有什么影响。
SWP_NOREDRAW (&H8 )
不刷新窗体内容
SWP_NOREPOSITION (SWP_NOOWNERZORDER )
Same as the SWP_NOOWNERZORDER flag.
SWP_NOSENDCHANGING ( ? )
Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
SWP_NOSIZE (&H1)
保持窗体大小(忽略 cx,cy 参数)
SWP_NOZORDER (&H4 )
保留窗体前后顺序(忽略 hWndInsertAfter 参数)
SWP_SHOWWINDOW (&H40)
显示窗体
返回值
成功调用返回非零值。不成功返回0。
【在 阿伊达 (阿伊达) 的大作中提到:】
其内部定义:
BOOL SetWindowPos(
HWN...

.
得失由人论,冷暖自心知。