Sunteți pe pagina 1din 4

Programarea în Windows

Home / My courses / PWINDOWS / Capitolul 1. Concepte fundamentale. Partea 2 / HelloWin modificat (de la Charles Petzold)

HelloWin modificat (de la Charles Petzold)

Pentru a vedea ce înseamnă cu adevărat această paradigmă bazată pe evenimente și cum funcționează, vom considera o aplicație GUI simplă
„HelloWin” care rulează pe Windows. Aplicația este scrisă în C, folosind antica Interfață pentru Programarea Aplicațiilor Windows (API), pe care
Microsoft a dezvoltat-o încă din anii '80. Spre deosebire de alte API-uri Windows, mai moderne și alte limbaje de programare, această API Win32 de
nivel scăzut în C demonstrează conceptele principale ale programării bazate pe evenimente în forma lor cea mai simplă și directă.

Așadar, haideți să parcurgem această aplicație Windows simplă, care este adaptarea programului „Hello, Windows 95” din cartea „Programarea în
Windows” a lui Charles Petzold. Această carte, publicată pentru prima dată în 1988, a devenit biblia pentru programarea Windows în acea vreme.

/*******************************************************************

* HelloWin.c -- Displays "Hello, Windows" in client area

*               (c) Charles Petzold, 1996

* ©Miro Samek, 2020


* Adaptat pentru cursul "Programarea pilotata de evenimente"

*******************************************************************/

#include <windows.h> // Windows API

LRESULT CALLBACK WndProc(HWND me, UINT sig, WPARAM wParam, LPARAM lParam);

#define WIN_HANDLED   ((LRESULT)0)

/*----------------------------------------------------------------

* The main entry point to the Windows apllication

*/

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PTSTR szCmdLine, int iShowCmd)

    WNDCLASSEX wnd; // instance of the Window class ('wnd' object)

    HWND hwnd;  // "handle" to the window object

    int status; // status of the processing to report back to Windows

    UNREFERENCED_PARAMETER(hPrevInstance);

    UNREFERENCED_PARAMETER(szCmdLine);

    // set attributes of the 'wnd' object

    wnd.cbSize = sizeof(wnd);

    wnd.style = CS_HREDRAW | CS_VREDRAW;

    wnd.cbClsExtra = 0;

    wnd.cbWndExtra = 0;

    wnd.hInstance = hInstance;

    wnd.hIcon = NULL; // LoadIcon(NULL, IDI_APPLICATION);


    wnd.hbrBackground = GetStockObject(WHITE_BRUSH);

    wnd.lpszMenuName = NULL;

    wnd.hIconSm = NULL;

    wnd.lpszClassName = "HelloWin";

    // set "virtual" function of the 'wnd' object

    wnd.lpfnWndProc = &WndProc; // attach the "window proc"

    // register the Window Class with Windows

    RegisterClassEx(&wnd);

    // constructor of the 'wnd' class

    hwnd = CreateWindow(

        wnd.lpszClassName,    // window class name

        "Hello, Windows!",    // window caption

        WS_OVERLAPPEDWINDOW,  // window style

        CW_USEDEFAULT,        // initial x position

        CW_USEDEFAULT,        // initial y position

        300,                  // initial x size

        200,                  // initial y size

        NULL,                 // parent window handle

        NULL,                 // window menu handle

        hInstance,            // program instance handle

        NULL);                // creation parameters

    ShowWindow(hwnd, iShowCmd);

    UpdateWindow(hwnd);

    // event loop ("message pump")

    while (1) {

        MSG msg; // message object to receive

        // generically WAIT for any message to arrive in the queue

        status = GetMessage(&msg, NULL, 0, 0);

        if (status == 0) { // message NOT to be processed?

            status = msg.wParam;

            break; // terminate the event loop

    }

        TranslateMessage(&msg);

        // dispatch to the appropriate "window proc"

        DispatchMessage(&msg);
    return status; // return to Windows with the status of processing

/*----------------------------------------------------------------

* The "windows proc" registered for the main window

* of this application

*/

LRESULT CALLBACK WndProc(HWND me, UINT sig, WPARAM wParam, LPARAM lParam)

    static int wm_keydown_ctr   = 0; // counter incremented in WM_KEYDOWN

    static int wm_mousemove_ctr = 0; // counter incremented in WM_MOUSEMOVE

    static char const* led_text = "OFF";

    LRESULT status; // status of the processing to report back to Windows

    switch (sig) {       // signal of the message

        case WM_CREATE: { // window was created

            status = WIN_HANDLED; // report event handled

            break;

    }

        case WM_DESTROY: { // windows is about to be destroyed

            PostQuitMessage(0);

            status = WIN_HANDLED; // report event handled

            break;

    }

        case WM_PAINT: { // window needs to be repainted

            PAINTSTRUCT ps;

            HDC hdc;

            RECT rect;

            char cBuffer[100];

            wsprintf(cBuffer, "KEYBOARD=%3d, MOUSE=%3d, LED=%s ",

                (wm_keydown_ctr % 1000),

                (wm_mousemove_ctr % 1000),

                led_text);

            // painting performed between BeginPain() and EndPaint()...

            hdc = BeginPaint(me, &ps);

            GetClientRect(me, &rect);

            DrawText(hdc, cBuffer, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);


            status = WIN_HANDLED; // report event handled

            break;

    }

                case WM_KEYDOWN: {  // keyboard key has been pressed

            ++wm_keydown_ctr;

            status = WIN_HANDLED; // report event handled

            break;

    }

        case WM_MOUSEMOVE: { // mouse has been moved over the window

            ++wm_mousemove_ctr;

            InvalidateRect(me, NULL, FALSE); // force re-paining of the window

            status = WIN_HANDLED; // report event handled

            break;

    }

        default: { // default behavior (characteristic "look and feel")

            // report the status of default processing

            status = DefWindowProc(me, sig, wParam, lParam);

            break;

    }

  }

    return status; // return to Windows with the status of processing

Explicațiile vin în pagina următoare.

Last modified: Sunday, 3 May 2020, 11:58 PM

Punctul de intrare în program -


◄ Evenimentele conduc programul Jump to...
inițializarea ►

You are logged in as Mihail Curchi (Log out)


PWINDOWS
Data retention summary
Get the mobile app

S-ar putea să vă placă și