Windows -- common messages and additional parameters of messages

Win32: the principle of Windows window, which can be compiled into 32 bits or 64 bits

WM: Windows Message

The window type name of the window to be created should be written in the type name of the window registered in step 1 and step 2, otherwise the window creation will fail

Here are the messages commonly used in Windows:

The window processing function will handle many messages. Which messages will it handle? Let's monitor what these messages are

//Window processing function
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    //Define a string
    wchar_t szMsg[100];
    //Multi byte sprintf
    //Format - > wsprintf for wide characters
    //Store the formatted string in szMsg. The format window handle hWnd is displayed in hexadecimal # and will be prefixed automatically
    wsprintf(szMsg,L"hWnd:%#x\tuMsg:%#x\twParm:%#x\tlParam:%#x\n",hWnd,uMsg,wParam,lParam);
    //Used to output debugging information
    OutputDebugString(szMsg);

    switch(uMsg)
    { 
    //The message created in the window client area is an earlier message. At this time, the main window has been created, but it is not displayed
    case WM_CREATE:
        //Parent window is hWnd: the currently created window
        //The call of CreateWindow function or CreateWindowEx function will issue WM_CREATE message
        MessageBox(hWnd,"This is WM_CREATE news",L"Tips",MB_OK);
        //Set the title of the window the title of the window to be set
        SetWindowText(hWnd,L"windows programming");
        break;
   }
}

WINBASEAPI
VOID
WINAPI
OutputDebugStringW(
    _In_opt_ LPCWSTR lpOutputString
    );

#ifdef UNICODE
#define OutputDebugString  OutputDebugStringW

There will be a message before the window comes out. It will call WindowProc function many times and print debugging information many times

The following problems caused by debugging can be removed directly, and the following information will not be output:

Window handle: all are the same, which is the same window

This message will be sent when the window size changes, and the application will use this message to change the default size value and position

WM_GETMINMAXINFO message (Winuser.h) - Win32 apps | Microsoft Docs

NC: non customer area

C: customer area

Creation of non customer area

Automatically calculate the size of non client area

 

Window creation message: creation of customer area

 

Messages are divided into queued messages and non queued messages

Queued messages

Messages not in the queue: GetMessage is not required to get messages, such as WM_CREATE

GetMessage: get message from message queue

WPARAM wParam, LPARAM lParam are additional information

The value of the created window information will be transferred to lParam

Forward indent tab, reverse indent shift + tab

LPARAM pointer to structure CREATESTRUCT

typedef struct tagCREATESTRUCTW {
    LPVOID      lpCreateParams;
    HINSTANCE   hInstance;        //Application Instance Handle 
    HMENU       hMenu;            //Menu handle
    HWND        hwndParent;       //Parent window handle
    int         cy;               //Height of window
    int         cx;               //Width of the window
    int         y;                //Vertical coordinate of the upper left corner of the window
    int         x;                //Abscissa of the upper left corner of the window
    LONG        style;            //style
    LPCWSTR     lpszName;         //Window name
    LPCWSTR     lpszClass;        //Window type name
    DWORD       dwExStyle;        //Window expansion style
} CREATESTRUCTW, *LPCREATESTRUCTW;
#ifdef UNICODE
typedef CREATESTRUCTW CREATESTRUCT;
//Window processing function
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    wchar_t szMsg[100];
    wsprintf(szMsg,L"hWnd:%#x\tuMsg:%#x\twParm:%#x\tlParam:%#x\n",hWnd,uMsg,wParam,lParam);
    OutputDebugString(szMsg);

    switch(uMsg)
    { 
    case WM_CREATE:
        //Pointer to structure - > cast to LPCREATESTRUCT. Any pointer points to four bytes
        LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;     
   }
}

You must add {} to define variables in case, which means it is a statement block. If you don't add {}, there will be problems

At the same time of debugging, open the monitoring window

Select the variable and drag it to the monitoring window

 

Or input directly

After single step debugging according to F10

Ipcreateparames: the information to be passed when creating a window is NULL: there is nothing to be passed. If a value is passed in, the value will be passed to ipcreateparames. Note that the pointer is passed

 

Check the window type name and note that each application has a window type name

Why do you want a window type name?

Any window has a window type name → check the window type name

spy + +: one is 32-bit and the other is 64 bit

 

Click the search window, press and hold the mouse to move to the window you want to check

The last parameter in CreateWindow, lParam, is cast from int type to pointer type

HWND hWnd = CreateWindow(
    szAppClassName,                                                   //Type name of the registration window
    szWindowName,                                                     //Title of the window
    WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINMIZEBOX,         //Window style title bar | system menu
    200,                                                              //Abscissa of the window from the upper left corner of the screen
    200,                                                              //The vertical coordinate of the window from the upper left corner of the screen
    800,                                                              //Width of the window
    600,                                                              //Height of window
    NULL,                                                             //Parent window handle
    NULL,                                                             //Menu handle
    hInstance,                                                        //Application Instance Handle 
    (LPVOID)10                                                        //Information to pass when creating window NULL: no information to pass 
);

typedef void far            *LPVOID;
#define far

 

Many things that need initialization can be placed in WM_ In create

 else  
        { 
            break;  

        }

//If you don't want to handle it by yourself, you can't handle it Call this function and leave it to the operating system
Whether you click exit or not, this statement will be executed in the end: return DefWindowProc(hWnd, uMsg, wParam, lParam);

Click Yes

 

 

Click No to close the window

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    { 
    //Window close message
    case WM_CLOSE:
        if(IDYES == MessageBox(NULL,L"Are you sure you want to exit?",L"Tips",MB_YESNO))
        {
            //Destroy the currently processed window handle - > issue WM_DESTROY information
            DestroyWindow(hWnd);
        }
        else  
        {   //If you do not execute break
            break;
        }
        break;
    //Window destroy message
    case WM_DESTROY:
        //Send an exit message WM_ Quit - > Enter 0 and exit normally
        PostQuitMessage(0);
        break;
    }
    //Call the default window processing function of the operating system
    //If you don't want to deal with it by yourself, you can't deal with it Call this function and leave it to the operating system
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

Can you let it not process and don't want it to call DefWindowProc?

WM_CLOSE message (Winuser.h) - Win32 apps | Microsoft Docs

When you close a window, you do not need to pass any values

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    { 
    //Window close message
    case WM_CLOSE:
        if(IDYES == MessageBox(NULL,L"Are you sure you want to exit?",L"Tips",MB_YESNO))
        {
            //Destroy the currently processed window handle - > issue WM_DESTROY information
            DestroyWindow(hWnd);
        }
        else  
        {
            //Click No to minimize
            ShowWindow(hWnd,SW_MINIMIZE);
            //Click No to hide the running tray
            ShowWindow(hWnd,SW_HIDE);
            return 1;                 //I've dealt with it. I don't need you to deal with it anymore
            //return 0;               // I didn't handle it. You can handle it again, but I didn't give it to the default processing function of the operating system
        }
        break;
    //Window destroy message
    case WM_DESTROY: 
        //Mainly do some cleaning work and save data - > when the program goes to this place, the program cannot be restored and the window can no longer be displayed
        ShowWindow(hWnd,SW_MINIMIZE);
        return 1;
        //Send an exit message WM_ Quit - > Enter 0 and exit normally
        PostQuitMessage(0);
        break;
    }
    //Call the default window processing function of the operating system
    //If you don't want to deal with it by yourself, you can't deal with it Call this function and leave it to the operating system
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

Tags: Windows MFC microsoft

Posted by bogins on Wed, 04 May 2022 12:51:11 +0300