Compartilhar via


Sizing and Positioning a Window

Other versions of this page are also available for the following:

Windows Mobile Not SupportedWindows Embedded CE Supported

8/28/2008

The size and position of a window are expressed as a bounding rectangle that is specified in coordinates that are 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 the value CW_USEDEFAULT for the nWidth and nHeight parameters in the CreateWindow or CreateWindowEx function. After creating a window, set the window size or position by calling the MoveWindow or SetWindowPos function. For Windows CE 2.12 and earlier, changing the size of a window invalidates the entire window, which causes the OS to send a WM_PAINT message to your application. For Windows CE 3.0, changing the size of a window does not invalidate the entire window.

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.

If you want to override the default minimum resize dimensions, change the values (in pixels) for the following registry entries:

HKEY_LOCAL_MACHINE\System\GWE\cxMinSize
HKEY_LOCAL_MACHINE\System\GWE\cyMinSize

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, respectively.

You can call the GetWindowRect function to retrieve the coordinates of the bounding rectangle of a window. GetWindowRect fills a RECT structure with the coordinates of the upper-left and lower-right corners of the window. 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 the bounding rectangle of a child window to coordinates that are relative to the client area of the parent window.

The GetClientRect function retrieves the position and size of the client area of the window. Because coordinates are relative to the client area itself, the upper-left corner of the client area 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 Embedded CE, it is included in the dimensions that are returned by GetClientRect.

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 client area of the parent window. To convert the client coordinates of a specified point into screen coordinates, call the ClientToScreen function. To convert the screen coordinates of a specified point into client coordinates, call the ScreenToClient function.

To change the position of a window 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 window, whereas topmost refers to a specific style that controls the z-order for the window. Topmost windows are located 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 z-order of the window.

The defer window API, which consists of the DeferWindowPos, BeginDeferWindowPos, and EndDeferWindowPos functions, is an alternative to making repeated calls to SetWindowPos. These functions enable you to queue up several changes in window position and to 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:

  • When creating an owned window, if the owner of the window is designated WS_EX_TOPMOST, the owned window must be a topmost window, too.
  • When calling SetWindowPos to change the topmost attribute of a window, you must change the topmost attribute of all of its owned windows.
  • When changing the topmost attribute of a window, do not call SetWindowLong.
  • When changing the z-order of a member of an owned group of windows, you must respect the topmost attribute of other windows.

See Also

Concepts

Working with Windows and Messages

Other Resources

GWES Application Development