SetWindowPos function

Modifies the size, position, and Z order of a window.

Applies to:

C++
BOOL SetWindowPos(
  HWND hWnd,
  HWND hWndInsertAfter,
  int X,
  int Y,
  int cx,
  int cy,
  UINT uFlags
);

Parameters

Return value

Remarks

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.

Note

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.

Important

Always use the correct window handle (HWND) when calling this function. Passing an invalid handle can lead to unexpected behavior or application crashes.

See also