MSDN Community

Window Functions (Win32 API)

Overview

The Win32 Window functions enable creation, management, and interaction with top‑level and child windows. These functions are fundamental for building graphical user interfaces on Windows.

Key Functions

FunctionHeaderPurpose
CreateWindowExwinuser.hCreates an overlapped, pop‑up, or child window with extended styles.
DestroyWindowwinuser.hDestroys the specified window.
ShowWindowwinuser.hSets the window's show state.
UpdateWindowwinuser.hUpdates the client area of the specified window.
SetWindowPoswinuser.hChanges the size, position, and Z order of a window.
GetWindowLongPtrwinuser.hRetrieves information about the specified window.
SetWindowLongPtrwinuser.hChanges an attribute of the specified window.
DefWindowProcwinuser.hDefault processing for window messages.

Example: Creating a Simple Window

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
    const wchar_t CLASS_NAME[]  = L"SampleWindowClass";

    WNDCLASS wc = {0};
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInst;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0,
        CLASS_NAME,
        L"Hello, Win32!",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
        NULL,
        NULL,
        hInst,
        NULL
    );

    if (hwnd == NULL) return 0;

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

Related Topics