Skip to content

Instantly share code, notes, and snippets.

@Acebond
Created May 25, 2024 02:12

Revisions

  1. Acebond created this gist May 25, 2024.
    69 changes: 69 additions & 0 deletions WindowAppExample.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    #include <Windows.h>

    LRESULT CALLBACK WndProc(
    _In_ HWND hWnd,
    _In_ UINT msg,
    _In_ WPARAM wParam,
    _In_ LPARAM lParam) {

    switch (msg) {

    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    int APIENTRY wWinMain(
    _In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR lpCmdLine,
    _In_ int nCmdShow) {

    const TCHAR szTitle[] = TEXT("Example Windows App");
    const TCHAR szWindowClass[] = TEXT("Example Windows App");

    WNDCLASSEX wc = {
    .cbSize = sizeof(wc),
    .lpfnWndProc = WndProc,
    .hInstance = hInstance,
    .lpszClassName = szWindowClass,
    };

    if (!RegisterClassEx(&wc)) {
    return 1;
    }

    HWND hWnd = CreateWindowEx(
    0, // No extended style
    szWindowClass, // Class name
    szTitle, // Window title
    WS_OVERLAPPEDWINDOW, // Window style
    CW_USEDEFAULT, // Initial x position
    CW_USEDEFAULT, // Initial y position
    1280, // Width
    720, // Height
    nullptr, // No parent window
    nullptr, // No menu
    wc.hInstance, // Instance handle
    nullptr); // No additional data

    if (!hWnd) {
    return 1;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

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

    DestroyWindow(hWnd);
    UnregisterClass(wc.lpszClassName, wc.hInstance);
    return 0;
    }