Visual C ++MFC Guide (1): behind the scenes

zhaozj2021-02-11  211

Visual C / MFC Tutorial, the first lesson: behind: News and handle

Although you will want to go deep into the source code, you don't do this. Windows programming is very difficult. Let's take a quick look at the program under Windows is working like this. The key to programming will be to process and send messages. What is the news? Simply put, the message is a 32-bit value to indicate an event. For example: When you move your mouse, a message (defined as WM_MOUSEMOVE) is sent to the current window. When you press the key on the keyboard, a message (defined as WM_KeyDown) is sent to the current window. When you adjust the size of the window, a message (defined as WM_SIZE) is sent to the current window. Imagine this process? So where did these messages have been sent? They will be put into a team, and the window will eventually take them out and the corresponding execution. For example, when the window gets the WM_MOVE message, it will change the window coordinates and redo the window on the screen.

Let's take a look at the handle. Windows is doing well in terms of object-oriented. You face some Window objects (such as desktop, you are now used to read programs, etc ...). How do a programmer distinguishes this type of east with a non-face-oriented way? Use the handle. The handle is used to reference different Windows objects. You can use the handle to the window, and you can use the handle to allocated memory. You can understand the handle as a pointer. You have to create a handle in some way; and destroy after using it, or you will cause resource leaks to make your system paralyzed. So to ensure that they have been destroyed at some time.

Ok, now let us put these two things together. For example, there is a window, you have a handle pointing to it (called HWnd). Name your handle Your_hwnd. When this covered other windows above this window, the system will ask you to redraw the window. Windows will send a message like this: PostMessage (Your_hWnd, WM_Paint, 0, 0); this function sends a heavy-in message to the window of your_hwnd. The last two parameters use additional information for the message, now you don't have to consider it. In this way, your program should have a function including a lot of branch judgment to handle different messages. such as:

Void HandletHeMessage (long message)

{

Switch (Message)

{

Case WM_Paint:

DrawWindow ();

Break;

Case WM_KeyDown:

Break;

// ETC ...

}

}

Ok, these are the most basic operations behind the Windows. But these are enough to let you continue to learn MFC.

转载请注明原文地址:https://www.9cbs.com/read-6021.html

New Post(0)