/**************************************************************************************
hellowin.cpp
    displays "Hello, Windows" in a fully responsive GUI window
***************************************************************************************/

#include <windows.h> //provided by the compiler

//------- Callback function that "runs" the program
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

//------- OS point of entry. Replaces main()
int WINAPI WinMain (HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nCmdShow) {
    static char szAppName[] = "HelloWin";
    HWND        hwnd;
    MSG         msg;
    WNDCLASS    wcl;

    if (!hPrevInst) { //this is the first running copy of the program
        //setup the window class for this program
        wcl.hInstance = hThisInst;                       //handle to this copy
        wcl.lpszClassName = szAppName;                   //window class name
        wcl.lpfnWndProc = WndProc;                       //name of function that processes messages
        wcl.style = CS_HREDRAW | CS_VREDRAW;             //window display style

        wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);     //default icon
        wcl.hCursor = LoadCursor(NULL, IDC_ARROW);       //default cursor
        wcl.lpszMenuName = NULL;                         //no menu

        wcl.cbClsExtra = 0;                              //no additional class data
        wcl.cbWndExtra = 0;                              //no additional window data

        wcl.hbrBackground = GetStockObject(WHITE_BRUSH); //window background fill color

        if(!RegisterClass (&wcl))
            return 0;
    }

    //after verifying that the window class exists, create a window based on it
    //this is actually a function call, but because it has so many parameters, we
    //usually format it as if it were a statement, including indents
    hwnd = CreateWindow (
        szAppName,           // Window class
        "The Hello Program", // string for the Title Bar
        WS_OVERLAPPEDWINDOW, // Window style
        CW_USEDEFAULT,       // initial x position
        CW_USEDEFAULT,       // initial y position
        CW_USEDEFAULT,       // initial x size
        CW_USEDEFAULT,       // initial y size
        HWND_DESKTOP,        // parent window handle
        NULL,                // window menu handle
        hThisInst,           // program instance handle
        NULL                 // additional creation parameters
    );

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

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

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    HDC hdc;        //device context handle
    PAINTSTRUCT ps; //paint structure
    RECT rect;      //rectangle parameters

    switch (message) {
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rect);
            DrawText(hdc, "Hello, Windows!", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
            EndPaint(hwnd, &ps);
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0 ;
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
}