NO MFC Programming 06 - Detecting Messages from System

zhaozj2021-02-16  50

(Previous tutorial If you don't understand anything, don't matter, please continue to go down, I will naturally won't be confused!)

Windows is used by an event-driven programming method, so it is important to detect and process messages. Let us continue to study the message queue (Queue):

A more vivid example! The system is between the system, the system is upstream, I am in the downstream and set a water gate. What is the system to inform me to write into the bottle and throw it into the river. Finally, these bottles came in front of the front of the watergate. (This example is good) I will pick up a bottle when I have time, read the inside, then I will know what the next step is to do.

What if I don't have time? Then what you deal with may have missed the best time. There is a way to let your messages, that is to use postmessage ().

Ok, let's move your hands, imitate the message queue for the system to send me the message.

The function to be used: Send a message to the queue - postthreadMessage (), search queue message - PeekMessage (), there is a structure of loading messages and related data MSG (that is, the bottle of instructions), as follows:

TypeDef struct tagmsg {hwnd hwnd; // The window handle related to the message is 0, there is no relevant window uint message; // message specific value, which is equivalent to an int (16 bits) that is not symbol, range is 0 - 65535 WPARAM WPARAM; / / The first correlation parameter, the type is the same. LPARAM LPARAM; // Second correlation parameters, equivalent to the long definition, range (omitted) DWORD TIME; // system timing, equivalent to unsigned long, using gettickcount () can achieve the same value POINT PT; // mouse Coordinate, equivalent to (long x; long y;) coordinate} MSG;

See the details of the specific source program:

// file name: WinMain.cpp

#define Win32_Lean_and_mean // Say No To Mfc !!

#include

CHAR TEMP [177] = "Hello World"; // Temporary String Variable

Char Result [512] = ""; // is used to save, output results

Char title [] = "Sample for message Details __copyright -` sea breeze `";

// name: winmain () // Main program entrance // ------------------------------------------------- Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NCMDSHOW) {

Msg msg; / / define a structure of load messages

DWORD CURTHREADID = GetCurrentThreadId (); // Get the ID of the current thread (flag number)

MessageBox (NULL, "Please press OK to start testing!", Title, mb_ok | mb_topmost); BOOL DONE = false; // Used to save the send message is successful

/ / Send a message You need to use the thread to indicate which message queue is sent, the ID is obtained by getCurrentThreadID ()

Done = PostthreadMessage (CurthreadID, 123, 1, 22); if (! DONE) MessageBox (NULL, "Send Message Failed _1!", Title, MB_ok | MB_TOPMOST);

Done = PostthreadMessage (CurthreadID, 345, 2, 22); if (! DONE) MessageBox (NULL, "Send Message Failed _2!", Title, MB_ok | MB_TOPMOST);

Done = PostthreadMessage (CurthreadID, 4321, 3, 22); if (! done) MessageBox (NULL, "Send Message Failed _3!", Title, MB_ok | MB_TOPMOST);

// The following is equivalent to postquitMessage (12); done = postthreadMessage (CurthreadID, WM_QUIT, 12, 0); if (! DONE) MessageBox (Null, "Send message failed _4!", Title, mb_ok | MB_TOPMOST);

PostquitMessage (12); // Send Exit Message

// is equivalent to sending a WM_QUIT message, but only getMessage () will detect it, peekMessage () should be handled. / / WM_QUIT is equal to 18 (ie 0x0012), click on the right mouse button to see "Go to Definition of WM_QUIT"

Long i = 24, k = 0; // i is the initial value of the circulation coefficient, K is used to record how many messages have been accepted

While (i) // Cycle Coefficient I exits {IF (PEEKMESG, NULL, 0, 0, PM_REMOVE)) {

WSPrintf (TEMP, "detected the% LD number message / n" "first parameter wparam =% ld;" "Second parameter lparam =% ld; / n" (system time) msg.Time =% ld; " "The handle involved) msg.hwnd =% ld / n / n", msg.message, msg.wparam, msg.lparam, msg.time, msg.hwnd; lstrcat (result, temp); k ; // Added to the result string back}

I - // The cycle factor is reduced by 1, with 0 exits} WSPrintf (TEMP, "Treading a total of% LD strips / n", k); // Subtrate the number LSTRCAT (RESULT, TEMP);

MessageBox (NULL, RESULT, TITLE, MB_OK | MB_TOPMOST); / / Shows Details of the detected message

EXITPROCESS (0); return null;}

The above source program should pay attention to two places:

One is the last parameter pm_remove of PeekMessage (). If it is changed to PM_NOREMOVE, although the message is made, there is still a message in the message queue (MSDN original: Messages Are Not Removed from the queue inster processing by peekmessage.), What effect will there be? You will try it yourself.

Its second, if you use the PostquitMessage () function, the actual effect is equivalent to sending a wm_quit message to your thread with postthreadMessage (), you can determine if (msg.Message == WM_QUIT) is really exit message (handle) ) Cycle (using Break).

In addition, in the above example, use PM_NOREMOVE to test. If the expected result is not expected, I don't think it is strange, it is recommended to use the following method to display the results.

While (True) / / Clear all excent messages {if (! PeekMessage (& MSG, NULL, 0, 0, PM_REMOVE)) Break;}

MessageBox (NULL, RESULT, TITLE, MB_OK | MB_TOPMOST); / / Shows Details of the detected message

Below we will continue to try to detect the news that truly comes from the system!

However, before this, we need to introduce a handle to you, because there is one in a new window to create.

The handle is the term translated (English is Handle), but it is not good to help understand!

One of my friends published his view "The handle seems to be an apple's handle, if you want to eat Apple ..." Another friend interrupted him ", it should be this! You did a loss, let me Hold the handle, hey, I want you to do what you have to listen to me ... "

Correct! I think so, I have created a window, you have to know its status, size, location, etc. You have to have a handle; you have to change its properties, change its style, you have to have a handle Even if you have to ruin it, you have to have its handle. Since the handle is so important, when can you get a handle?

In general, when you create a new window using the CREATEWINDOWEX () function, the function will return one, remember to save it!

(A tip: The handle is not only the window, and many of the objects inside the Windows are accessed by handle. For example, if you apply for memory to the system, there must be a heap of Handle of HEAP and so on, related to handle The content will be mentioned later.)

Then we have the subject of this article - detect messages from the system.

(The following example uses the CREATEWIONDOWEX () function to create a new window, and will make a detailed description after the specific usage) Before we start, we must prior to an impression, what happens to us? When will I send us?

First answer a question later, in general, when our program gets the input focus, that is, the program's window is on top, and the title turns blue background, and the system will send our input information to our program.

What kind of input information will it be? Probably these, for example, we press the keyboard, when we press the keyboard on the keyboard, the system will send a WM_keyDown information to the program (current input focus), (wm_keydown == 256), and let's release key When the system will issue WM_KEYUP (WM_KEYUP == 257, you can distinguish which key is pressed by viewing the 16-bit msg.wParam), similar to WM_MOUSEMOVE and WM_LBUTTONDOWN, etc. related to the mouse input.

In the following example, you have three seconds to send the system to your program in the following examples!

// file name: WinMain.cpp

#define Win32_Lean_and_mean // Say No To Mfc !!

#include

CHAR TEMP [177] = "Hello World";

Char Result [1024] = ""; // is used to save, output results

Char title [] = "Sample for Message Details __copyright -` sea breeze ............. ";

// name: winmain () // Main program entrance // ------------------------------------------------- Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NCMDSHOW) {

Msg msg; / / define a structure of load messages

// *

// create the following using CreateWindowEx () function is a temporary window, use the hWnd temporarily stored handle HWND hWnd = CreateWindowEx (WS_EX_TOPMOST | WS_EX_TOOLWINDOW, "Edit", "1231", WS_OVERLAPPEDWINDOW, 120,120, 280,180, NULL, NULL, hInstance, NULL);

ShowWindow (hwnd, sw_show); // Let this window can be seen

MessageBox (NULL, "created a test window, press OK to continue testing!", Title, mb_ok | mb_topmost); // * /

Sleep (3000); // Use this time to trigger some events such as mouse movement, press the button and other events on a new window.

// Send three custom messages

BOOL DONE = false; // Used to save whether the transmission is successful

// The ID of the current thread is obtained by getCurrentThreadId (), Done = PostThreadId (), 123, 1, 22); if (! Done) MessageBox (NULL, "Send Message Failed _1!", Title, MB_ok | MB_TOPMOST) // postMessage () Send a message related to the window DONE = PostMessage (hwnd, wm_keydown, 55, 4321); // The fake WM_KEYDOWN message if (! Done) MessageBox (Null, "Send message failed _2! ", Title, mb_ok | mb_topmost);

PostquitMessage (TRUE); // Send Exit Message // Only getMessage () can detect it, peekmessage () should be judged

// The following start detection system messages

Long i = 24, k = 0; // i is the initial value of the circulation coefficient, K is used to record how many messages have been accepted

While (i) // Cycle Coefficient I exits {IF (PEEKMESG, NULL, 0, 0, PM_REMOVE)) {

WSPrintf (TEMP, "detected the% LD number message / n" "first parameter wparam =% ld;" "Second parameter lparam =% ld; / n" (system time) msg.Time =% ld; " "The handle involved) msg.hwnd =% ld / n / n", msg.message, msg.wparam, msg.lparam, msg.time, msg.hwnd; lstrcat (result, temp); k ; // Added to the result string back}

I - // The cycle factor is reduced by 1, with 0 exit}

WSPRINTF (Temp, "Total% LD Bar Messages / N", K); // Say the report digital lstrcat (Result, TEMP);

DestroyWindow (hwnd); // To destroy the window before exiting the program

MessageBox (NULL, RESULT, TITLE, MB_OK | MB_TOPMOST); / / Display the details of the intercept message // Please carefully read the system time of the message, is there a case where there is a group?

EXITPROCESS (0); return null;}

In the above experiment, if you have pressed the button, you should receive WM_KeyDown, and WM_KEYUP (Messages 257), which will be paired.

However, there is a message No. 280 (msg.Message == 0x0118), I don't understand what news is coming.

... `Sea Breeze October 17, 2002 AM 2:01

-------------------------- The secret of learning is to know why, not just how ...

Currently like songs: Power Train - Zhongxiao East Road takes nine times

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

New Post(0)