3-windows programming – Windows and messages

Time:2022-1-6

Windows and messages

   1. Windows programming model.

               

               

 

Procedure 1-1

#include 

//Window procedure callback function H is the abbreviation of handler, handle
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//The second way to write
//LONG WINAPI  WndProc(HWND, UINT, WPARAM, LPARAM);



//Main function H handle, instance 
Int WinAPI WinMain (hinstance hinstance, // current instance handle
                    Hinstance hprevinstance, // previous instance handle
                    LPSTR lpszCmdLine,
                    int nCmdShow)
{
    HWND hwnd; // Window handle
    MSG msg;   // news

    WNDCLASS wc; // Window class
    //1. Design a window class
    wc. style = 0;  // window style
    wc. lpfnWndProc = (WNDPROC)WndProc; // Window process
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc. hInstance = hInstance;  // The current instance handle is assigned to WC
    wc. hIcon = LoadIcon(NULL, IDI_WINLOGO); // Handle Icon
    wc. hCursor = LoadCursor(NULL, IDC_ARROW);// mouse
    wc. hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // Window background
    wc. lpszMenuName = NULL; // Window menu
    wc. lpszClassName = TEXT("MyWndClass");  // Window class name

    //2. Register window class
    RegisterClass(&wc);

    //3. create a window
    hwnd = CreateWindow(
        Text ("mywndclass"), // window class name
        Text ("Hello SDK application"), // window title
        WS_ Overlappedwindow, // window style
        CW_ Usedefault, // window position x
        CW_ Usedefault, // window position Y
        CW_ Usedefault, // window size
        CW_ Usedefault, // window size
        Null, // parent window handle
        Null, // window menu handle
        hInstance,
        NULL
        );

    //4. Display and update windows
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    //5. Message loop
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);  // Translation message
        DispatchMessage(&msg); // Forward to window procedure
    }

    return msg.wParam;
}


//Window process
LRESULT  CALLBACK WndProc(HWND hwnd,
                            UINT message,
                            WPARAM wParam,
                            LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;  // Device handle of DC handle
    RECT rect; // Box
    //Process various messages
    switch (message)
    {
    case WM_ Lbuttondown: // left mouse button
        //PostQuitMessage(0);  
        return 0;
    case WM_ Paint: / painting
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd,&rect);
        Ellipse(hdc,0,0,200,100);
        DrawText (HDC, text ("windows programming model"), - 1, & rect,
            DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        EndPaint(hwnd, &ps);
        return 0;
    case WM_ Destroy: // close window message
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);  // Process default messages
}

               

2. Use the vs2017 wizard to quickly create a desktop application

Realize simple functions:

① Mask menu bar

② Draw text

③ Play a song when creating a window (song format: wav# song path: as shown in the file directory similar to the following figure).

                       

Procedure 2-1

// 3-2_ Create code using the windows wizard CPP: defines the entry point for the application.
//

#include "pch.h"
#include "framework.h"
#Include "3-2# use the windows wizard to create code. H"

//#include screen //#define Win32 in header file here_ LEAN_ AND_ Mean is enough
//#Pragma comment (LIB, "winmm. Lib") // add in attribute - > linker - > Input

#define MAX_LOADSTRING 100

//Global variables:
HINSTANCE hInst;                                //  Current instance
WCHAR szTitle[MAX_LOADSTRING];                  //  Title Block text
WCHAR szWindowClass[MAX_LOADSTRING];            //  Main window class name

//Forward declaration of functions contained in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

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

    //Todo: place code here.

    //Initialize global string
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY32WINDOWS, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    //Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY32WINDOWS));

    MSG msg;

    //Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//Function: myregisterclass()
//
//Target: register window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY32WINDOWS));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    //wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_MY32WINDOWS);
    wcex. lpszMenuName   =  NULL;   // Mask menu window
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//Function: InitInstance (hint, int)
//
//Target: save the instance handle and create the main window
//
//Note:
//
//In this function, we save the instance handle in the global variable and
//Create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; //  Store the instance handle in a global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

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

   return TRUE;
}

//
//Function: wndproc (hWnd, uint, wParam, lParam)
//
//Objective: to process messages from the main window.
//
//  WM_ Command - process application menu
//  WM_ Paint - draw main window
//  WM_ Destroy - send exit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_ Create: / newly added
        //Play sound when creating a window
        Playsound (text ("our dream. Wav"), null, snd_ FILENAME | SND_ ASYNC);
        break;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            //Analysis menu selection:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            RECT rect;
            HDC hdc = BeginPaint(hWnd, &ps);
            //Todo: add any drawing code using HDC here
            GetClientRect(hWnd, &rect);

            //Add new: draw text
            DrawText (HDC, text ("use the wizard to quickly create code!")- 1, &rect,
                DT_SINGLELINE | DT_CENTER | DT_VCENTER);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

//Message handler for the about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

                

   3. Handle

Baidu Encyclopedia:
A handle is an identifier used to identify an object or item. It can be used to describe forms, files, etc. it is worth noting that a handle cannot be a constant 。
The reason why windows wants to set up a handle is fundamentally due to the problem of memory management mechanism, that is, virtual address. In short, the address of the data needs to be changed. After the change, someone needs to record and manage the change. Therefore, the system uses a handle to record the change of the data address. In programming, handle is a special intelligent pointer. When an application wants to refer to memory blocks or objects managed by other systems (such as database and operating system), it needs to use handle.
I understand:
Similar to the frying pan, the handle is needed to operate the frying pan*_*
               

 

 

Procedure 3-1
// 3-3_ Handle CPP: defines the entry point for the application.
//

#include "pch.h"
#include "framework.h"
#Include "3-3# handle. H"

#define MAX_LOADSTRING 100

//Global variables:
HINSTANCE hInst;                                //  Current instance
WCHAR szTitle[MAX_LOADSTRING];                  //  Title Block text
WCHAR szWindowClass[MAX_LOADSTRING];            //  Main window class name

//Forward declaration of functions contained in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

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

    //Todo: place code here.

    //Initialize global string
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY33, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    //Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY33));

    MSG msg;

    //Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//Function: myregisterclass()
//
//Target: register window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY33));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    //wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_MY33);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//Function: InitInstance (hint, int)
//
//Target: save the instance handle and create the main window
//
//Note:
//
//In this function, we save the instance handle in the global variable and
//Create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; //  Store the instance handle in a global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

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

   return TRUE;
}

//
//Function: wndproc (hWnd, uint, wParam, lParam)
//
//Objective: to process messages from the main window.
//
//  WM_ Command - process application menu
//  WM_ Paint - draw main window
//  WM_ Destroy - send exit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            //Analysis menu selection:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            RECT rect;
            HDC hdc = BeginPaint(hWnd, &ps);
            //Todo: add any drawing code using HDC here
            GetClientRect(hWnd,&rect);
            Ellipse(hdc, 0, 0, rect.right, rect.bottom);
            DrawText (HDC, text ("handle example"), - 1, & rect, DT_ SINGLELINE | DT_ CENTER | DT_ VCENTER);

            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

//Message handler for the about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

                

 

 

  4. Overall consideration

               

                

                

               

 

Procedure 4-1

// 3-4_ Overall consideration CPP: defines the entry point for the application.
//

#include "pch.h"
#include "framework.h"
#Include "3-4# overall consideration. H"

#define MAX_LOADSTRING 100

//Global variables:
HINSTANCE hInst;                                //  Current instance
WCHAR szTitle[MAX_LOADSTRING];                  //  Title Block text
WCHAR szWindowClass[MAX_LOADSTRING];            //  Main window class name

int iDistance;
short nLength;
TCHAR szFileName[100];
int cxWidth;
int cyHeight;

UINT a;
PSTR str;
CHAR *str1;





//Forward declaration of functions contained in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

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

    //Todo: place code here.

    //Initialize global string
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY34, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    //Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY34));

    MSG msg;

    //Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//Function: myregisterclass()
//
//Target: register window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY34));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    //wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
    //wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_MY34);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//Function: InitInstance (hint, int)
//
//Target: save the instance handle and create the main window
//
//Note:
//
//In this function, we save the instance handle in the global variable and
//Create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; //  Store the instance handle in a global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

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

   return TRUE;
}

//
//Function: wndproc (hWnd, uint, wParam, lParam)
//
//Objective: to process messages from the main window.
//
//  WM_ Command - process application menu
//  WM_ Paint - draw main window
//  WM_ Destroy - send exit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    //Define variables
    int cxClient, cyClient;

    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            //Analysis menu selection:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            RECT rect;
            HDC hdc = BeginPaint(hWnd, &ps);
            //Todo: add any drawing code using HDC here
            GetClientRect(hWnd, &rect);

            cxClient = rect.right - rect.left;
            cyClient = rect.bottom - rect.top;

            Ellipse(hdc, cxClient/8, cyClient/8,cxClient*7/8, cyClient*7/8);

            DrawText (HDC, text), - 1, & rect, DT_ SINGLELINE | DT_ CENTER | DT_ VCENTER);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

//Message handler for the about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}