System Layout API Reference
msdn.microsoft.com/windows/api/system/layout
This document provides a comprehensive reference for the System Layout API in Windows. These functions allow developers to interact with and manage the visual and structural layout of windows and controls within the Windows operating system.
Key Concepts
Understanding the following concepts is crucial when working with the System Layout API:
- Window Hierarchy: How windows are parented and childed.
- Client Area: The portion of a window available for drawing.
- Screen Coordinates: The coordinate system used to position windows on the screen.
- DPI Awareness: How applications scale with different display resolutions.
Core Functions
The System Layout API provides a variety of functions for managing window positioning, sizing, and visibility. Here are some of the most commonly used functions:
SetWindowPos
Sets the size, position, and ordering of a window. This is one of the most fundamental functions for layout manipulation.
BOOL SetWindowPos(
HWND hWnd,
HWND hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
UINT uFlags
);
Parameters:
hWnd: Handle to the window.hWndInsertAfter: Handle to the window to be inserted after.X: The new horizontal position of the window.Y: The new vertical position of the window.cx: The new width of the window.cy: The new height of the window.uFlags: Window positioning flags.
GetWindowRect
Retrieves the dimensions of the bounding rectangle of a window. The dimensions are in screen coordinates relative to the upper-left corner of the screen.
BOOL GetWindowRect(
HWND hWnd,
LPRECT lpRect
);
Parameters:
hWnd: Handle to the window.lpRect: Pointer to a RECT structure that receives the screen coordinates of the window.
MoveWindow
Changes the size and position of a given window. This function is similar to SetWindowPos but is often simpler for basic repositioning.
BOOL MoveWindow(
HWND hWnd,
int X,
int Y,
int nWidth,
int nHeight,
BOOL bRepaint
);
Parameters:
hWnd: Handle to the window.X: New horizontal position.Y: New vertical position.nWidth: New width.nHeight: New height.bRepaint: Indicates whether the window is to be repainted.
Layout Structures
The API uses several structures to define positions and dimensions:
| Structure | Description |
|---|---|
RECT |
Defines the coordinates of the upper-left and lower-right corners of a rectangle. |
POINT |
Defines a point in two-dimensional space. |
SIZE |
Defines the width and height of a rectangular area. |
DPI Scaling
Modern Windows applications must be DPI aware to ensure proper scaling on high-resolution displays. The System Layout API includes functions to help manage this:
GetDpiForWindowAdjustWindowRectExForDpi
Example Usage
Here's a simplified example of how to move a window to a specific position and size:
HWND hWnd = GetActiveWindow(); // Or GetForegroundWindow(), etc.
int newX = 100;
int newY = 100;
int newWidth = 400;
int newHeight = 300;
if (hWnd != NULL) {
SetWindowPos(hWnd, NULL, newX, newY, newWidth, newHeight, SWP_NOZORDER | SWP_SHOWWINDOW);
}