Unveil the mystery of. Net message loop (2)

Source: Internet
Author: User

This article from: http://www.cnblogs.com/sakiwer/archive/2009/12/15/1619281.html

 

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------

 

What is a message?

Messages are generated when you move the mouse, press the button, and close the window. In Windows, messages exist in the following data structure (defined in the winuser. h file ):..

Typedef struct tagmsg {
Hwnd;
Uint message;
Wparam;
Lparam;
DWORD time;
Point pt;
} MSG;

There are six messages in the message:

. Hwnd: the unique hwnd Number of the window/control. The message loop sends messages to the correct target based on this information.

. Message: the pre-defined message type ID in windows.

. Wparam and lparam: some messages must carry more information, which is stored in wparam and lparam.

. Time and PT: the time when the message occurred and the mouse position.

How does. NET Framework encapsulate message loops?

Windows Forms of. NET Framework encapsulates messages cyclically for our convenience. The classes mentioned in this section all belong to the system. Windows. Forms namespace ).

The message loop is encapsulated into the run () Static Method of the application class; Windows procedure is encapsulated into the nativewindow and control classes; individual message processing actions are encapsulated into the onxyz () (for example, onpaint () of the control class ()). We can override onxyz () to provide our own programs. You can also use the. NET event mechanism to add our event handler to the XYZ event ). The onxyz () of the control class actively calls the processing function of the XYZ event.

Note that because the XYZ event processing function is called by the onxyz () method of the control class, when you override the onxyz () method, do not forget to call onxyz () of the control class (unless you have special requirements). Otherwise, the XYZ event processing function will be ineffective. You can call the onxyz () method of the control class by calling base. onxyz (), as shown below:

Protected override void onpaint (painteventargs e ){
Base. onpaint (E); // todo: Add form1.onpaint to implement
}

We can override the onxyz () of the control class to determine what to do when the message occurs. Similarly, we can even override wndproc () of the Control and nativewindow classes to define windows procedure.

Remind you again, because the onxyz () series of methods are called by the wndproc () of the control class, when you override wndproc, do not forget to call the wndproc () of the control class (unless you have special requirements). Otherwise, the onxyz () series of methods (and XYZ event processing functions) will not work. You only need to call base. wndproc () to call wndproc () of the control class, as shown below:

Protected override void wndproc (ref message m ){
Base. wndproc (ref m); // todo: Add form1.wndproc to implement
}

You may also notice that wndproc () requires a message class parameter, which is exactly the result of MSG being encapsulated into. net.

An example of Windows Forms

To help readers better understand the actual situation, I will use the following example:

Namespace windowsapplication1 {
/// Summary of form1.
Public class form1: FORM {
/// Variables required for the design tool.
Private container components = NULL;
Public form1 (){
Autoscalebasesize = new size (5, 15 );
Clientsize = new size (292,266 );
Name = "form1 ";
TEXT = "form1 ";
Paint + = new painteventhandler (this. form1_paint );
Paint + = new painteventhandler (this. form1_paint2 );
}
/// Main entry point of the application.
[Stathread]
Static void main (){
Application. Run (New form1 ());
}
Protected override void onpaint (painteventargs e ){
Base. onpaint (E); // 2
}
Private void form1_paint (Object sender, painteventargs e ){
// 3
}
Private void form1_paint2 (Object sender, painteventargs e ){
// 4
}
Protected override void wndproc (ref message m ){
Base. wndproc (ref m); // 1
}
}
}

1. In Main (), use application. Run () to display the form1 window and enter the message loop. Application. Run () has not ended.

2. OS puts a wm_paint message in the message queue of this process so that the window is displayed.

3. wm_paint is cyclically obtained by the message in application. Run () and distributed to wndproc (). Due to the polymorphism (polymorphism), The wndproc () obtained from the call (invoke) belongs to the wndproc () of form1, that is, the place where comment 1 is annotated in the above program, instead of calling control. wndproc ().

4. At the end of form1.wndproc (), base. wndproc () is called, which actually calls control. wndproc ().

5. Control. wndproc () indicates that the message is wm_paint from the message parameter, so onpaint () is called (). Due to polymorphism, The onpaint () called this time belongs to the onpaint () of form1, that is, the annotation 2 in the above program, rather than the control. onpaint () call ().

6. At the end of form1.onpaint (), base. onpaint () is called, which actually calls control. onpaint ().

7. We used to register form1_paint () and form1_paint2 () as the paint event handler in the constructor of form1.
(Event handler ). Control. onpaint () calls these two functions in sequence, that is, to annotate 3 and 4 in the above program.

Why do you know so much? Thanks to tools, today's programmers are very happy. They can write programs in the context of the paste. However, such programmers may not be very competitive. After all, it is difficult to drag and drop components to the screen and set component attributes. Only by gaining an in-depth understanding of the internal principles can you integrate your own technologies and make the programmer's path more stable and longer.

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.