About Windows Messages

zhaozj2021-02-16  42

About Windows Messages

Text / Bogdan Rechi

introduction

Everyone knows that MFC is the most useful tool for developing a Windows platform. Its class is an expert level when developing the entire difficult task, and how important you can imagine that the adaptability of the selected solution is. In developing large software, this is the best way to allow programmers away from the problems such as detail. It is the greatest advantage when processing a Windows-based system, but ...

Thread

We don't talk about Windows-based systems. "Entity in the internal work of a program is thread", which is said in the MSDN. In many background details like "Stack" and "Performing Path", it also covers a message queue implementation. This is not a provision, but you will find it on the thread of each support window. This is a general existence: the window has a message column. wrong! The message queue only exists on the thread, and the message sent to the window is sent to it by a loop in the thread. I will explain this question in this article.

program

This is an application-based console; the modal dialog manages its message through the console's message queue, so it will not have two colors at the same time. The code provides one and the main function.

CconsoleQueue

This class implements a message loop and an original system for processing messages. It has the following methods:

Runqueue - message loop running

While (GetMessage (& MSG, NULL, 0, 0) // Extract the message from the queue

{

IF

(fctman = (* this) [msg.Message])

FCTMAN ((void *) msg.wparam);

Else

{

TranslateMessage (& MSG); // Necessary to Listen the Keyboard

DispatchMessage (& MSG); // Message Goes to the Default Procedure

}

}

Endqueue - end message loop through the WM_QUIT

Postmessage (NULL, WM_QUIT, 0, 0); // Message to Interrupt the loop by return False // on getMessage

RegisterMessageHandler - Register a handle function to a specific message. The handle function is called when the message is received in the message loop. To pass the value of a handle, you must turn WPARAM when calling PostMessage.

Note: This can be considered as a backbone that is proposed by "Adaptive Communication Environment Ace".

RemoveAll - Delete all the union (from the reflection)

Main function

The program is planning step by step, like below:

1. Establish a modal dialog to send a message to the column:

HWndDialog = Createdialog (NULL, MakeintResource (IDD_DIALOG_TEST),

Null, dialogprocedure;

ShowWindow (hwnddialog, sw_show);

2. Set a timer on the thread

Ntimer = setTimer (NULL, 0, NDELAY * 1000, TimerProcedure);

3. Register a handle for user-defined messages

Queue.registerMessageHandler (WM_USER_DEfined, UserDefined);

4. Run column

Queue.runqueue ();

behavior

The purpose is to end the WM_TIMER message in the message queue. Each WM_TIMER will be printed into a normal buffer of the console. If the buffer does not modify the NKEEPQUEUESTEPS WM_TIMER, the last message will terminate the timer and end the message column. The modified buffer is completed by writing and pressing the "send buffer" within the modal dialog. Pressing the "Send User-Defined" message will increase the WM_USER_DEFINED message to the column. At last

Note Call PostMessage Use null as the first parameter ends the message in the current thread message queue.

Another note is the setting of the timer. This exists only in the queue event and it is not a window resource. It belongs to a thread, just if it is set and the window can be affected when it is passed through the DispatchMessage.

The last sentence of the message loop. This is a source of defects to programmers. In fact, you can have more message loops in the execution of a single thread. In order to test, simply increase the actual content of Main, you can be like this in the function body:

HWndDialog = Createdialog (NULL, MakeintResource (IDD_DIALOG_TEST),

Null, dialogprocedure; // ...

Cout << Endl << "Program Ends Here ..." << Endl;

HWndDialog = Createdialog (NULL, MakeintResource (IDD_DIALOG_TEST),

Null, dialogprocedure;

// ...

Cout << Endl << "Program Ends Here ..." << Endl;

// ...

HWndDialog = Createdialog (NULL, MakeintResource (IDD_DIALOG_TEST),

Null, dialogprocedure;

// ...

Cout << Endl << "Program Ends Here ..." << Endl;

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

New Post(0)