Win32 API Examples

Explore practical code snippets and complete examples demonstrating common Win32 API functionalities. These examples are designed to be illustrative and serve as a starting point for your Windows application development.

Creating a Simple Window

Learn the fundamental steps to register a window class, create a window, and implement a basic message loop.


#include <windows.h>

// Window procedure function
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wc = {0};
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = "MyWindowClass";

    // Register the window class
    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    HWND hWnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        "MyWindowClass",
        "Hello, Win32!",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
        NULL, NULL, hInstance, NULL);

    if (hWnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Show the window
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    // Message loop
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}
            

Handling User Input (Mouse Click)

This example shows how to detect and respond to mouse click events within your application window.


// ... (previous code for window creation and message loop)

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_LBUTTONDOWN: {
            // Get mouse coordinates
            int xPos = LOWORD(lParam);
            int yPos = HIWORD(lParam);
            MessageBox(hWnd,
                       ("Mouse clicked at: " + std::to_string(xPos) + ", " + std::to_string(yPos)).c_str(),
                       "Click Detected",
                       MB_OK);
            break;
        }
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
            

Displaying a Message Box

A straightforward example of how to use the `MessageBox` function to display information or prompts to the user.


// Example usage within a button click handler or other event
if (MessageBox(hWnd,
               "Do you want to proceed?",
               "Confirmation",
               MB_YESNO | MB_ICONQUESTION) == IDYES) {
    // User clicked Yes
    // Perform action...
} else {
    // User clicked No
    // Cancel action...
}
            

Working with GDI (Drawing a Line)

Demonstrates basic Graphics Device Interface (GDI) operations, such as obtaining a device context and drawing simple shapes.


// ... (inside your message loop and window procedure)

case WM_PAINT: {
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hWnd, &ps);

    // Create a pen for drawing
    HPEN hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255)); // Blue, 2 pixels wide
    HGDIOBJ hOldPen = SelectObject(hdc, hPen); // Select the new pen

    // Draw a line
    MoveToEx(hdc, 50, 50, NULL);
    LineTo(hdc, 200, 150);

    // Clean up
    SelectObject(hdc, hOldPen); // Restore the old pen
    DeleteObject(hPen);
    EndPaint(hWnd, &ps);
    break;
}
            

Further Exploration

These are just a few basic examples. The Win32 API is vast and capable of handling everything from low-level system interactions to complex graphical user interfaces. Consider exploring: