SetWindowPos

2.x

  BOOL SetWindowPos(hwnd, hwndInsertAfter, x, y, cx, cy, fuFlags)    
  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 fuFlags; /* window-positioning flags */

The SetWindowPos function changes the size, position, and Z-order of child, pop-up, and top-level windows. These windows are ordered according to their appearance on the screen; the window on top receives the highest rank and is the first window in the Z-order.

Parameters

hwnd

Identifies the window to be positioned.

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 Places the window at the bottom of the Z-order. If hwnd identifies a topmost window, the window loses its topmost status; the system places the window at the bottom of all other windows.
HWND_TOP Places the window at the top of the Z-order.
HWND_TOPMOST Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.
HWND_NOTOPMOST Repositions the window to the top of all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window.

For rules about how this parameter is used, see the following Comments section.

x

Specifies the new position of the left side of the window.

y

Specifies the new position of the top of the window.

cx

Specifies the new width of the window.

cy

Specifies the new height of the window.

fuFlags

Specifies the window sizing and positioning options. This parameter can be a combination of the following values:

Value Meaning

SWP_DRAWFRAME Draws a frame (defined in the window's class description) around the window.
SWP_HIDEWINDOW Hides the window.
SWP_NOACTIVATE 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_NOMOVE Retains the current position (ignores the x and y parameters).
SWP_NOSIZE Retains the current size (ignores the cx and cy parameters).
SWP_NOREDRAW Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the non-client area (including the title and scroll bars), and any part of the parent window uncovered as a result of the moved window. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that must be redrawn.
SWP_NOZORDER Retains the current ordering (ignores the hwndInsertAfter parameter).
SWP_SHOWWINDOW Displays the window.

Return Value

The return value is nonzero if the function is successful. Otherwise, it is zero.

Comments

If the SWP_SHOWWINDOW or the SWP_HIDEWINDOW flags are 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 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 are not changed.

If neither SWP_NOACTIVATE nor SWP_NOZORDER is specified (that is, when the application requests that a window be simultaneously activated and placed in the specified Z-order), the value specified in hwndInsertAfter is used only in the following circumstances:

Neither HWND_TOPMOST or HWND_NOTOPMOST is specified in the hwndInsertAfter parameter.

The window specified in the hwnd parameter 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 the Z-order of an activated window without restrictions or activate a window and then move it to the top of the topmost or non-topmost windows.

A topmost window is no longer topmost if it is repositioned to the bottom (HWND_BOTTOM) of the Z-order or after any non-topmost window. When a topmost window is made non-topmost, all of its owners and its owned windows are also made non-topmost windows.

A non-topmost window may own a topmost window, but not vice versa. 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.

Example

The following example sets the size of a window equal to one-fourth the size of the desktop and then positions the window in the upper-left corner of the desktop:

RECT rect;

GetWindowRect(GetDesktopWindow(), &rect);
SetWindowPos(hwnd, (HWND) NULL, 0, 0,
    rect.right / 2, rect.bottom / 2,
    SWP_NOZORDER | SWP_NOACTIVATE);

See Also

BringWindowToTop, GetWindowRect, MoveWindow