Window Management

This section details the core APIs for creating, managing, and interacting with windows within the Windows operating system. Understanding these functions is fundamental to developing graphical user interfaces (GUIs) on the Windows platform.

Key Concepts

Core Window Functions

CreateWindowEx

Creates an overlapping, pop-up, or child window. This is a versatile function used to create most types of windows.

HWND CreateWindowEx(
    DWORD dwExStyle,
    LPCTSTR lpClassName,
    LPCTSTR lpWindowName,
    DWORD dwStyle,
    int x,
    int y,
    int nWidth,
    int nHeight,
    HWND hWndParent,
    HMENU hMenu,
    HINSTANCE hInstance,
    LPVOID lpParam
);
dwExStyle
Extended window styles.
lpClassName
The name of the window class.
lpWindowName
The text that appears in the window's title bar.
dwStyle
Window styles.
x
The position of the window's left side, in screen coordinates.
y
The position of the window's top side, in screen coordinates.
nWidth
The width, in device units, of the window.
nHeight
The height, in device units, of the window.
hWndParent
Handle to the parent window or owner window.
hMenu
Handle to a menu, or specifies a child-window identifier.
hInstance
Handle to the application instance.
lpParam
Pointer to window creation data.
Return Value
If the function succeeds, the return value is a handle to the newly created window. Otherwise, it is NULL.

DestroyWindow

Destroys the specified window. In addition to destroying the window, this function sends a WM_DESTROY message to the window to notify it of its impending destruction.

BOOL DestroyWindow(
    HWND hWnd
);
hWnd
Handle to the window to be destroyed.
Return Value
If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE.

GetMessage

Retrieves messages from the calling thread's message queue and places the message in the specified buffer.

BOOL GetMessage(
    LPMSG lpMsg,
    HWND hWnd,
    UINT wMsgFilterMin,
    UINT wMsgFilterMax
);
lpMsg
Pointer to a MSG structure that receives message information.
hWnd
Handle to the window whose messages are to be retrieved.
wMsgFilterMin
Lowest message value to examine.
wMsgFilterMax
Highest message value to examine.
Return Value
If the function retrieves a message other than WM_QUIT, the return value is TRUE. If the function retrieves the WM_QUIT message, the return value is FALSE. If there is an error, the return value is -1.

TranslateMessage and DispatchMessage

These functions are crucial for the Windows message loop. TranslateMessage translates virtual-key messages into character messages. DispatchMessage sends the message to the appropriate window procedure for processing.


MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
            

Window Styles

Window styles determine the appearance and behavior of a window. Some common styles include:

Style Constant Description
WS_OVERLAPPED An overlapped window. Has a title bar and a border.
WS_POPUP A pop-up window. No title bar or special frame.
WS_CHILD A child window. Cannot be created alone; must belong to another window.
WS_VISIBLE The window is initially visible.
WS_SYSMENU The window has a system-apply menu.
WS_MINIMIZEBOX The window has a minimize button.
WS_MAXIMIZEBOX The window has a maximize button.

Window Messages

Windows communicate through messages, which are 32-bit values consisting of a message ID and two message parameters (wParam and lParam).

Common Messages: WM_CREATE, WM_PAINT, WM_COMMAND, WM_SIZE, WM_CLOSE, WM_DESTROY.