Modifies the size, position, and Z order of a window.
Applies to:
BOOL SetWindowPos(
HWND hWnd,
HWND hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
UINT uFlags
);
hWndHWND [in]
Handle to the window.
hWndInsertAfterHWND [in, optional]
Handle to the window that is to be placed immediately before the window specified by the hWnd parameter in the window--order. This parameter must be a window handle or one of the following values:
HWND_BOTTOM: Places the window at the bottom of the Z order. If the calling thread and the thread that owns the window are attached to different input queues, the window is placed at the bottom of the list of top-most windows.HWND_NOTOPMOST: Places the window above all nontopmost windows. The window remains at the top of all the other nontopmost windows.HWND_TOP: Places the window at the top of the Z order.HWND_TOPMOST: Places the window above all nontopmost windows. The window floats above all nontopmost windows and remains at the top of all the other nontopmost windows.This parameter can be NULL to indicate that no wrap-around is desired for the HWND_TOP or HWND_BOTTOM values.
Xint [in]
The new x-coordinate of the left side of the window.
Yint [in]
The new y-coordinate of the top of the window.
cxint [in]
The new width of the window, in pixels.
cyint [in]
The new height of the window, in pixels.
uFlagsUINT [in]
Window status. This parameter can be a combination of the following values:
SWP_ASYNCWINDOWPOS: Don't wait for all mirrored or collapsed windows to finish the whole operation before returning.SWP_DEFERERASE: Prevent generation of the WM_ERASEBKGND and WM_PAINT messages. This can be used to optimize animation or drag operations.SWP_DRAWFRAME: Apply the frame change.SWP_FRAMECHANGED: Send a WM_NCCALCSIZE message to the window, even if the size is not changing.SWP_HIDEWINDOW: Hide the window.SWP_NOACTIVATE: Does not activate the window.SWP_NOCOPYBITS: Discards the contents of the client area. If this flag is not specified, the system will copy the contents of the client area to the window that is being placed SWP_SHOWWINDOW in the Z order.SWP_NOMOVE: Retains the current position (X and Y parameters are ignored).SWP_NOSENDCHANGING: Prevents the system from sending the WM_WINDOWPOSCHANGING message.SWP_NOREPOSITION: Same as SWP_NOMOVE.SWP_NOZORDER: Retains the current Z order (hWndInsertAfter is ignored).SWP_SHOWWINDOW: Displays the window.TRUE if the function succeeds, FALSE otherwise.If the calling thread and the thread that owns the window are attached to different input queues, the function posts the request to the calling thread's input queue and returns immediately.
A window can be resized and moved only if it has the WS_OVERLAPPED style. A window with the WS_CHILD style can be resized and moved by its parent window.
The SetWindowPos function sends a WM_WINDOWPOSCHANGING message to the window, followed by a WM_WINDOWPOSCHANGED message, whether the window's size, position, or Z-order has changed or not. The SWP_NOSENDCHANGING flag prevents these messages from being sent.
If the SWP_NOZORDER flag is set, the hWndInsertAfter parameter is ignored. If the SWP_NOMOVE flag is set, the X and Y parameters are ignored.
If the SWP_HIDEWINDOW flag is set, the window is hidden. If the SWP_SHOWWINDOW flag is set, the window is displayed.
When calling SetWindowPos, ensure that the uFlags parameter is set correctly to avoid unintended consequences. For example, if you intend to move the window but forget to set SWP_NOMOVE, the window may not move as expected.
Always use the correct window handle (HWND) when calling this function. Passing an invalid handle can lead to unexpected behavior or application crashes.