Sizing and Positioning a Window

A window's size and position are expressed as a bounding rectangle specified in coordinates relative to the screen or to the parent window. Typically, window dimensions and coordinates are measured in pixels.

When you create a window, you can set the initial size and position of the window directly or instruct the system to calculate the initial size and position by specifying CW_USEDEFAULT in the CreateWindow or CreateWindowEx function. After creating a window, set the window size or position by calling the MoveWindow or SetWindowPos function.

If you need to create a window with a client area of a specified size, call the AdjustWindowRectEx function to calculate the required size of a window based on the preferred size of the client area. Pass the resulting size values to the CreateWindowEx function.

Although you can create a window of any size, it should not exceed the screen size of the target device. Before setting a window size, check the width and height of the screen by using the GetSystemMetrics function with the SM_CXSCREEN and SM_CYSCREEN flags.

You can call the GetWindowRect function to retrieve the coordinates of a window's bounding rectangle. GetWindowRect fills a RECT structure with the coordinates of the window's upper-left and lower-right corners. The coordinates are relative to the upper-left corner of the screen, even for a child window. The ScreenToClient or MapWindowPoints function maps the screen coordinates of a child window's bounding rectangle to coordinates relative to the parent window's client area.

The GetClientRect function retrieves the position and size of a window client area; because coordinates are relative to the client area itself, the client area's upper-left corner is always at coordinates (0, 0) and the coordinates of the lower-right corner are the width and height of the client area. Furthermore, because the command bar is part of the client area in Windows CE, it is included in the dimensions that are returned by the GetClientRect function.

To retrieve the handle to the window that occupies a particular point on the screen, call the WindowFromPoint function. Call the ChildWindowFromPoint function to retrieve the handle to the child window that occupies a specified point in the parent window client area. To convert the client coordinates of a specified point to screen coordinates, call the ClientToScreen function. To convert the screen coordinates of a specified point into client coordinates, call the ScreentoClient function.

To change a window's position in the z-order, call the SetWindowPos function. This function is used to create a topmost window. A topmost window is a window that has the WS_EX_TOPMOST style. Do not confuse topmost with top-level. Top-level refers to whether or not a window has a parent, whereas topmost refers to a specific style that controls the z-order for the window. Topmost windows are above all non-topmost sibling windows in the z-order. To create a topmost window, specify the WS_EX_TOPMOST style when you create the window, or call SetWindowPos and set the hWndInsertAfter parameter to HWND_TOPMOST.

A window might lose its topmost style by calling SetWindowPos and setting the hWndInsertAfter parameter to HWND_NOTOPMOST. If a window is positioned directly after a non-topmost window, that window loses its WS_EX_TOPMOST style. You can set the SetWindowLong function to give a window the WS_EX_TOPMOST style; however, this function does not change the window's z-order.

The defer window API, which consists of DeferWindowPos, BeginDeferWindowPos, and EndDeferWindowPos, is an alternative to making repeated calls to SetWindowPos. These functions enable you to queue up several window position changes and execute them simultaneously.

Occasionally, topmost windows can disappear behind the desktop. This typically occurs when one of the following rules governing the use of topmost windows is broken: