MFC / QT study notes (a) - understanding basic library windows

MFC (Microsoft Foundation Class), Windows platform GUI yet do OK, but does not support cross-platform.

 

// Windows message mechanism:

API 与 //1.SDK

Software Development Kit, creating a collection of software development tools, application specific software framework, hardware platform, operating system, and so on;

Application Programming Interface, available to the operating system application programming interface;

 

// 2. Window and handle

Window, a Windows program has at least one main window;

Handle the Windows application window by window handle (HWND) be identified. We want to manipulate a window, you must first get a handle to the window;

// ps: system created in a variety of resources (windows, icons ...) will allocate memory for them, and returns the identification number to identify those resources that handle. eg: icon handle (HICON), the cursor handle (HCURSOR), brush handles (HBRUSH)

 

3 // Message Message Queuing

Windows is an event-driven programming model, mainly based on the message.

 

--Windows entry function //4.WinMain function program, the main program corresponding to DOS 

A complete Win32 program implementation steps:

  · WinMain function definitions

  · Create a window

  · A message loop

  · Writing process window

 

// environment: VS2013, observed: VS2017, TranslateMessage function not found.

#include <WINDOWS.H> // underlying implementation header file window

// callback function
 // CALLBACK parameter representing _stdcall arguments passed this order: from right to left on the stack, the stack before the function returns empty 
LRESULT CALLBACK WindowProc (
    HWND the HWND, // Message View window handle 
    UINT UNSG, // specific message format name WM_XXX 
    WPARAM the wParam, // keyboard additional message 
    LPARAM the lParam) { // attach the mouse message

    switch (uNsg) {

        // All methods xxxWindow to end, does not enter the message queue, direct execution 
    Case the WM_CLOSE:
        The DestroyWindow (HWND); // the DestroyWindow send another message the WM_DESTROY 
        BREAK ;
     Case the WM_DESTROY:
        PostQuitMessage ( 0 ); // call the break while loop 
        BREAK ;

    Case the WM_LBUTTONDOWN: // mouse button pressed 
    {
         int xPos = the LOWORD (the lParam);
         int yPos = HIWORD of (the lParam);

        char buf [ 1024 ]; // displayed in the window 
        wsprintf (buf, the TEXT ( " X% = D, Y = D% " ), xPos, yPos); // achieve character splicing 
        the MessageBox (HWND, buf, the TEXT ( " press the left mouse button " ), MB_OK);
         BREAK ;
    }

    Case the WM_KEYDOWN: // keyboard press 
        the MessageBox (HWND, the TEXT ( " keyboard is pressed " ), the TEXT ( " keyboard press " ), MB_OK);
         BREAK ;

    Case the WM_PAINT: // Drawing 
    {
        PS the PAINTSTRUCT; // Drawing structure 
        the HDC HDC = the BeginPaint (HWND, & PS); // start 
        the TextOut (HDC, 100 , 100 , the TEXT ( " HELLO " ), strlen ( " HELLO " ));
        EndPaint (hwnd, & PS); // End 
        BREAK ;
    }
    }

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


// program entry function
 // WINAPI _stdcall parameter representative of the delivery order parameter: Drawing from right to left, before the function returns empty stack 
int WINAPI the WinMain (
    HInstance the HINSTANCE _In_,         // application instance handle 
    _In_opt_ the HINSTANCE hPrevInstance, // the application handle, usually NULL 
    _In_ LPSTR lpCmdLine,             // char * the argv [] 
    _In_ int nShowCmd) {             // display command, the maximum / minimize

    // 1. Design window 
    WNDCLASS wc;

    wc.cbClsExtra = 0 ;     // class additional memory 
    wc.cbWndExtra = 0 ;     // extra memory window 
    wc.hbrBackground = (HBRUSH) the GetStockObject (WHITE_BRUSH); // set the background color 
    wc.hCursor = LoadCursor (NULL, IDC_HAND ); // set the cursor hand, null default style 
    wc.hIcon = the LoadIcon (nULL, IDI_ERROR); // set icon, null supra 
    wc.hInstance = hInstance; // passed in parameter WinMain 
    wc.lpfnWndProc = WindowProc; // callback function, the window procedure 
    wc.lpszClassName = the TEXT ( " WIN " ); // specify a window class name 
    wc.lpszMenuName = NULL;// menu name 
    wc.style = 0 ; // default style

    // 2. Registration window 
    the RegisterClass (& WC);

    // Create the window 
    HWND hwnd = CreateWindow (
        wc.lpszClassName, // lpClassName class name 
        TEXT ( " Windows " ), // lpWindowName title name 
        WS_OVERLAPPEDWINDOW, // dwStyle default style mixed mode 
        CW_USEDEFAULT, // the X-coordinate 
        CW_USEDEFAULT, // the y- 
        CW_USEDEFAULT, // nWidth width and height 
        CW_USEDEFAULT, // nHeight 
        NULL, // hWndParent parent window 
        NULL, // hMenu menu 
        hInstance, // hInstance instance handle 
        NULL); // lpParam value mouse buttons

    // 4. display and update 
    the ShowWindow (HWND, SW_SHOWNORMAL); // normal size

    UpdateWindow(hwnd);

    // The messages take through the circulation 
    / *
     *typedef struct tagMSG {
     * HWND hwnd; // main window handle
     * UINT message; // specific message name
     * WPARAM wParam; // keyboard messages additional message
     * LPARAM lParam; // mouse messages additional message
     * DWORD time; // message generation time, from the system operator
     * POINT pt; // mouse position
     *}
    **/
    MSG msg;

    while (1) {
        /*
        GetMessageW(
            _Out_ LPMSG lpMsg, news
            _In_opt_ HWND hWnd, window capture, capture all windows on behalf of NULL
            _In_ UINT wMsgFilterMin,
            _In_ UINT wMsgFilterMax); (0,0) to capture all messages on behalf of
        */
        if (GetMessage(&msg, NULL, 0, 0) == FALSE) {
            break;
        }

        // translate the message eg: type Ctrl + C can not be directly printed, the need to translate the message into the queue again 
        TranslateMessage (& msg);

        DispatchMessage ( & msg); // distributing news

    }

    // 6. Treatment process up window

    return 0;

}

 

I refer to the video link:

https://www.bilibili.com/video/av20005978/?p=8&t=328

Guess you like

Origin www.cnblogs.com/CowryGao/p/12335788.html