Delphi processes Windows messages

Source: Internet
Author: User

1. peekmessage () function. The following figure shows its prototype: bool peekmessage (lpmsg, // pointer to structure for message hwnd, // handle to window uint wmsgfiltermin, // first message uint wmsgfiltermax, // last message uint wremovemsg // removal flags); this is a boolean type, that is, an int type, but there are only two values, true and false, if a message is waiting in the queue, the function returns true. Otherwise, false is returned. Its parameter is simple: lpmsg: This is a pointer variable of the MSG type. If a message is waiting, the message information is filled in with this variable. Hwnd: handle of the Message Queue window to be checked. Uint wmsgfiltermin, wmsgfiltermax: index the first and last messages. Generally, you can retrieve them from the first message, so set them to 0. Uint wremovemsg: Generally, it has two values: pm_remove or pm_noremove. The former will be removed from the queue after the message is read, and the latter will continue to be retained. Generally, select the former pm_remove. 2. Differences between peekmessage and getmessage are similar to those between sendmessage and postmessage. They are both waiting and not waiting. If there is no message in the message queue, peekmessage will not wait, return 0, while getmessage will wait for a message in the message queue to return... getmessage is blocked. After getmessage is called, the entire Program Will be in sleep state. 3. when processing a message, you need to do two things: translatemessage () and dispatchmessage (). Their prototype is very similar: bool translatemessage (const MSG * lpmsg); is to convert a virtual key message to a character message and add it to the queue of the calling thread. (It should be to convert the wm_keydown message into a suitable wm_char message and put it in the message queue of this thread.) Long dispatchmessage (const MSG * lpmsg); call the corresponding information from the MSG structure. Dispatchmessage sends the processed message back to Windows and is handled by Windows itself. The work it completes is to distribute the current message to the relevant window process. Then, the window process processes different messages based on their types. 4. postmessage () and sendmessage () postmessage () are often used to add messages to the Message Queue. if the message is successful, true is returned. Otherwise, false is returned. It simply adds the message to the queue and returns it. In most cases, calling it returns true. Sendmessage () is somewhat different. Instead of adding a message to a queue, it directly translates the message and calls the message for processing until the message is processed. 5. A Message Queue can be divided into a system message queue and a thread message queue. System message queues are maintained by windows, and thread message queues are maintained by each GUI thread. To avoid creating message queues for non-Gui threads, no message queues are generated by all threads, only when the thread calls the GDI function for the first time, the system creates a message queue for the thread. The queue message is sent to the system message queue and then to the thread message queue. For queue messages, the most common is the mouse and keyboard-triggered messages, such as wm_mousermove, wm_char, and other messages, such as wm_paint, wm_timer, and wm_quit. When a mouse or keyboard event is triggered, the corresponding mouse or keyboard driver converts the event to a message and then delivers it to the system message queue, it is handled by the Windows system. In Windows, a message is retrieved from the system message queue at an appropriate time. Based on the MSG message structure we mentioned earlier, the message is sent to that window, then, the retrieved message is sent to the corresponding queue of the thread that creates the window. The following is the concern of the thread message queue. Windows is busy with its own tasks. When a thread sees a message in its own message queue, it extracts it from the queue and sends it to a suitable window through the operating system for processing. Generally, the system always posts messages to the end of the message queue. This ensures that the window receives messages in the FIFO order. However, wm_paint is an exception. Multiple wm_paint messages in the same window are merged into one wm_paint message, and all invalid areas are merged into one invalid area. The purpose of merging wm_pain is to reduce the number of refresh windows. Example 1: // This function enables the program to enter a message loop. The loop is exited only when the left mouse button is left in the form client area, you cannot change the form size and move the form procedure tform1.button1click (Sender: tobject); var themessage: tmsg; // keep the message information mouseclicked: Boolean; // cyclic control variable begin {initialize cyclic control variable} mouseclicked: = false; {clear the message queue before entering, there is a clean environment} while peekmessage (themessage, handle, 0, 0, pm_remove) Do; {This function places the program in a message loop, wait until you click the mouse to exit} while not mouseclicked do begin {suspend the thread until a new message is put into the Message Queue} waitmessage (); {Get the message into the Message Queue} while peekmessage (themessage, handle, 0, 0, pm_remove) Do begin if themessage. message = wm_paint then dispatchmessage (themessage) else if themessage. message = wm_lbuttondown then begin mouseclicked: = true; showmessage ('A message was received, resume execution '); end; Example 2: function tapplication. processmessage (var msg: tmsg): Boolean; var handled: Boolean; begin result: = False; If peekmessage (MSG, 0, 0, 0, pm_remove) then begin result: = true; If MSG. message <> wm_quit then begin handled: = false; if assigned (fonmessage) Then fonmessage (MSG, handled); if not ishintmsg (MSG) and not handled and not ismdimsg (MSG) and not iskeymsg (MSG) and not isdlgmsg (MSG) then begin translatemessage (MSG); dispatchmessage (MSG); end else fterminate: = true; end; Example 3: // wait In response to all messages of the current process, the message specifies the message void processmessages (INT message) {MSG; bool result; do {result = false; If (:: peekmessage (& MSG, 0, 0, 0, pm_remove) {result = true; If (MSG. message! = Wm_quit | msg. message! = Message) {: translatemessage (& MSG);: dispatchmessage (& MSG) ;}} while (result );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.