MSDN - Windows API Reference

Win32 API | Window Management

Win32 Window Management API Reference

This section provides detailed documentation on the Windows API functions and concepts related to creating, managing, and interacting with windows within the Windows operating system.

Creating Windows

Learn how to create top-level windows, child windows, and pop-up windows using functions like CreateWindowEx and RegisterClassEx.

Function Description
CreateWindowEx Creates an overlapped, pop-up, or child window.
RegisterClassEx Registers a window class for subsequent use in calls to the CreateWindowEx function.
DestroyWindow Destroys the specified window. The parent window of a destroyed window automatically destroys any child windows that the window has.

Managing Window State

Control the visibility, enabled state, and focus of windows.

Function Description
ShowWindow Sets the show state of a window.
EnableWindow Enables or disables the specified window.
SetForegroundWindow Brings the thread that created the specified window into the foreground and activates the window.
IsWindowVisible Determines whether a window is visible.

Window Messages

Understand the message-driven architecture of Windows, including message loops, message dispatching, and common message types.

The core of window management in Win32 is the message loop. Applications retrieve messages from a message queue and dispatch them to the appropriate window procedure.


// Example of a basic message loop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
                
Function/Concept Description
GetMessage Retrieves messages from the calling thread's message queue.
TranslateMessage Translates virtual-key messages into character messages.
DispatchMessage Sends the message to the appropriate window procedure.
WNDPROC The callback function that processes messages sent to a window.
Common Messages Information about standard Windows messages (e.g., WM_PAINT, WM_COMMAND, WM_SIZE).

Window Drawing and Painting

Learn how to handle painting requests using the WM_PAINT message and the Graphics Device Interface (GDI).

When a window needs to be redrawn, the system sends a WM_PAINT message. Your application's window procedure should handle this message to update the window's content.


LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_PAINT: {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            // Drawing code goes here...
            RECT rect;
            GetClientRect(hwnd, &rect);
            DrawText(hdc, "Hello, Windows!", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            EndPaint(hwnd, &ps);
            return 0;
        }
        // ... other messages
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
                

Positioning and Sizing

Functions to get and set the position, size, and Z-order of windows.

Function Description
SetWindowPos Changes the size, position, and Z order of a child, pop-up, or top-level window.
GetWindowRect Gets the dimensions of the specified window. The dimensions are given in screen coordinates.
MoveWindow Changes the position and size of the specified window.

Dialog Boxes

Information on creating and managing modal and modeless dialog boxes, which provide interactive user interfaces.

Function Description
CreateDialog Creates a modeless dialog box from a dialog box template.
DialogBox Creates a modal dialog box from a dialog box template.

Standard Controls

Reference for common Windows controls such as buttons, edit boxes, list boxes, and static text.

These controls are typically created as child windows and can be managed using the general window management functions.

Control Type Description
Buttons BS_PUSHBUTTON, BS_CHECKBOX, etc.
Edit Boxes ES_MULTILINE, ES_PASSWORD, etc.
List Boxes LBS_OWNERDRAW, etc.
Static Text For displaying text or images.
Scroll Bars SBS_HORIZONTALSCRLL, SBS_VERTICALLSCRLL.