Skip to content

Instantly share code, notes, and snippets.

@SolindekDev
Created December 17, 2021 14:23
Show Gist options
  • Select an option

  • Save SolindekDev/78ee7bcf800dae7d18704158b39d62ea to your computer and use it in GitHub Desktop.

Select an option

Save SolindekDev/78ee7bcf800dae7d18704158b39d62ea to your computer and use it in GitHub Desktop.

Revisions

  1. SolindekDev created this gist Dec 17, 2021.
    72 changes: 72 additions & 0 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #include <windows.h>

    #define TITLE L"Example Window Title"
    #define WIDTH 800
    #define HEIGHT 600

    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {

    MSG msg;
    HWND hwnd;
    WNDCLASSW wc;

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.lpszClassName = L"Window";
    wc.hInstance = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpszMenuName = NULL;
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassW(&wc);
    CreateWindowW(wc.lpszClassName, TITLE,
    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    100, 100, WIDTH, HEIGHT, 0, 0, hInstance, 0);

    while (GetMessage(&msg, NULL, 0, 0)) {

    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }

    return (int) msg.wParam;
    }

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

    switch(msg) {

    case WM_CREATE:

    CenterWindow(hwnd);
    break;

    case WM_DESTROY:

    PostQuitMessage(0);
    break;
    }

    return DefWindowProcW(hwnd, msg, wParam, lParam);
    }

    void CenterWindow(HWND hwnd) {

    RECT rc = {0};

    GetWindowRect(hwnd, &rc);
    int win_w = rc.right - rc.left;
    int win_h = rc.bottom - rc.top;

    int screen_w = GetSystemMetrics(SM_CXSCREEN);
    int screen_h = GetSystemMetrics(SM_CYSCREEN);

    SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w)/2,
    (screen_h - win_h)/2, 0, 0, SWP_NOSIZE);
    }