MFC bottom window implementation

Brief description

MFC is a basic class library of Microsoft. If GUI is developed on Windows platform, it is a good choice. Simply record the knowledge points that need to be mastered or viewed later in the learning process of MFC.

Windows Messaging

  1. First, the operating system captures the messages from the keyboard or mouse input system, and stores the acquired messages in the message queue.
  2. The application always gets messages from the message queue through GetMessage().
  3. The application then dispatches the acquired message to the operating system through DispatchMessage().
  4. Operating system executes window procedure again

Windows programming model

  1. Definition of WinMain function (WinMain function is the entry of Windows program)
  2. Create a window
  3. Loop messages
  4. Write window procedure function

Overall frame structure

Implementation steps

Create win32 project

  1. Create a new win32 project, select "empty project" and complete the creation.
  2. Add the source file with the filename ending with ". c".
  3. Add header file "windows.h"
  4. Add program entry function "WindowMain"
//WINAPI represents the order in which the \\
int WINAPI WinMain(
    HINSTANCE hInstance,      // Application Instance Handle 
    HINSTANCE hPrevInstance,  // The last application handle. In win32 environment, the parameter is generally NULL and does not work.
    LPSTR lpCmdLine,          // char * argv[]
    int nShowCmd)             // Display commands maximize and minimize normal
{
    ...
        return 0;
}

Application process

  1. Design window
  • First, instantiate a window class, and then set its parameters in turn
WNDCLASS wc;
wc.cbClsExtra = 0; //Extra memory for class
wc.cbWndExtra = 0; //Window extra memory
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // Setting background
wc.hCursor = LoadCursor(NULL, IDC_HAND);  //Set cursor if the first parameter is NULL, the cursor provided by the system is used.
wc.hIcon = LoadIcon(NULL, IDI_ERROR);
wc.hInstance = hInstance; //The application instance handle can be passed into the parameter in WinMain.
wc.lpfnWndProc = WindowProc;// Callback function window procedure
wc.lpszClassName = TEXT("WIN");//Specify window class name
wc.lpszMenuName = NULL; //NULL means no menu
wc.style = 0; //Display style 0 is the default
  1. Registration window
  • Register the window class instantiated above
RegisterClass(&wc);
  1. create a window
/*
lpClassName,  Class name
lpWindowName, Title name
dwStyle,      Style WS? Overlappedwindow
x,            Display coordinate CW? Usedefault
y,
nWidth,       wide
nHeight,      high
hWndParent,   parent window
hMenu,        menu
hInstance,    Instance handle
lpParam       The added value lp is generally the mouse added value NULL.
*/
HWND hwnd = CreateWindow(wc.lpszClassName, TEXT("WINDOWS"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, wc.hInstance, NULL);
  1. Display and update
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
  1. Get message through loop
/*
HWND        hwnd;    Main window handle
UINT        message; Specific message name
WPARAM      wParam;  Additional message keyboard message
LPARAM      lParam;  Attach message mouse message
DWORD       time;    Message generation time
POINT       pt;      Additional message mouse message x y
*/
MSG msg;

while (1)
{
    /*
    LPMSG lpMsg,        news
    HWND hWnd,          NULL in capture window means capture all windows
    UINT wMsgFilterMin, The minimum and maximum filtered messages are generally filled with 0.
    UINT wMsgFilterMax  Fill in 0 to capture all messages
    */
    if (GetMessage(&msg, NULL, 0, 0) == FALSE)
    {
        break;
    }

    // Translation message
    TranslateMessage(&msg);
    // Distribution message
    DispatchMessage(&msg);
}
  1. Message processing (window procedure)
//Callpack represents the order in which the arguments of "stdcall" are passed: right to left to stack and clear the stack before the function returns
LRESULT CALLBACK WindowProc(
    HWND hwnd,       // Window handle to which the message belongs
    UINT uMsg,       // Specific message name WM xxxxxxxxmessage name
    WPARAM wParam,   // Keyboard additional messages
    LPARAM lParam    // Mouse attached message
)
{
    switch (uMsg)
    {
    case WM_CLOSE:
        //All methods ending in xxxWindow will not enter the message queue, but will be executed directly.
        DestroyWindow(hwnd);  //DestroyWindow sends another message WM? Destroy
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_LBUTTONDOWN:
    {
        int xPos = LOWORD(lParam);
        int yPos = HIWORD(lParam);

        char buf[1024];
        wsprintf(buf, TEXT("x = %d, y = %d"), xPos, yPos);

        MessageBox(hwnd, buf, TEXT("Press the left mouse button"), MB_OK);
        break;
    }
    case WM_KEYDOWN:
        MessageBox(hwnd, TEXT("Keyboard pressed"), TEXT("Keyboard pressed"), MB_OK);
        break;
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        TextOut(hdc, 100, 100, TEXT("HELLO"), strlen("HELLO"));
        EndPaint(hwnd, &ps);
        break;
    }
    default:
        break;
}

The general process is as follows

Summary

  1. First of all, we should have an overall grasp of the whole creation process.
  2. Learn to view msdn documents. Unknown parameters and methods can be obtained from them.
  3. It's important to study hard

Keywords: C++ Windows Programming

Added by Copyright on Fri, 18 Oct 2019 09:40:57 +0300