Windows Messaging mechanisms and instances

Source: Internet
Author: User
Tags message queue

Windows Send window messages

Tools Required: Spy++,visual Studio 2017,c# language

Technical route: First get the handle of the window to be manipulated by Spy + +, the prototype declaration of the function is:

[DllImport ("User32.dll")]
public static extern IntPtr FindWindow (string lpclassname, string lpwindowname);

This function obtains a handle to the target window, and if you want to get a handle to a child window, you can get it by using the following function:

[DllImport ("User32.dll")]
public static extern IntPtr FindWindowEx (IntPtr parent, IntPtr Childe, String strclass, string frmtext);

For the operation of the target window (sending instructions), the function prototype used is as follows:

[DllImport ("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage (IntPtr hWnd, int msg, int wParam, int lParam);

I send a drawing action through the SendMessage function, specifically including the mouse down,move,up

Another function that sends a message, the prototype declares:

[DllImport ("User32.dll", EntryPoint = "PostMessage")]
private static extern int PostMessage (IntPtr hWnd, int Msg, int wParam, int lParam);

I send by PostMessage function is the action of click button, specifically including the mouse down,up

The difference between SendMessage and PostMessage function

SendMessage invokes the window handler for the specified message until the window finishes processing the program, and the PostMessage function sends a message to the thread message to the column and returns immediately.

Failure section: 1. During the drawing process, I considered sending a drawing message with WM_PAINT, but after viewing the MSDN manual, I found that the WM_PAINT message was generated by the system and should not be sent by an application. I've thought about it, sir. into a graphic, and then send it to the window, which violates the purpose of the original test.

Failure Experience Summary: First of all to determine the technical route, can not blindly disorderly try to see the Official document function introduction.

Message mechanism in Windows: messages are instructions. If you want the window or control (in essence, the control to be a special window) to perform what action, you should send a message to it, and if another window wants you to do anything, it can send a message to you. If an event, such as tapping the keyboard, moving the mouse, clicking the button, etc., the system sends the message to the window, and if you are one of those windows, you will receive the message to perform the appropriate action.

Message Queuing: When a message is sent, a message is added to the message queue, and when a message is processed, it is removed from the message queue. This ensures that the message is not lost and that when you are processing a message, other incoming messages can be added to the message queue until they are processed.

Message loop:

while (GetMessage ( &msg, NULL, 00) > 0)
{
     translatemessage (&msg);//optional, some will use
    dispatchmessage (&msg );
} /p>

< Span style= "COLOR: #800080" > < Span style= "COLOR: #000000" > 1. The message loop calls GetMessage () to look up messages from the message queue for processing if the message queue is empty. The program stops executing and waits (the program is blocked).

< Span style= "COLOR: #800080" > < Span style= "COLOR: #000000" > 2. event occurs when a message is added to the message queue (for example, the system registers a mouse click event), GetMessage () A positive value is returned, indicating that a message needs to be processed and that the message has been populated into the incoming MSG parameter, and returns 0 when the Wm_quit message is passed in, or if the return value negative indicates an error occurred.

< Span style= "COLOR: #800080" > < Span style= "COLOR: #000000" > 3. Take out the message (in the MSG variable) and pass it to the TranslateMessage () function, This function does some extra processing: Converts the Virtual key value information to character information.

< Span style= "COLOR: #800080" > < Span style= "COLOR: #000000" > 4. After the above steps have been performed, the message is passed to the DispatchMessage () function. The DispatchMessage () function distributes the message to the target window of the message, and finds the target window procedure function, passing the window handle, message, WParam, lparam, and so on to the window procedure function and calling the function.

5. In the window procedure function, check the message and other parameters that you can use to implement the action you want. If you do not want to handle some special messages, you should always call the DefWindowProc () function, and the system will handle these messages in the default manner (generally, do not take any action).
6. Once a message processing is complete, the window procedure function returns, and the DispatchMessage () function returns, continuing to loop through the next message.

Windows Messaging mechanisms and instances

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.