WinForm 2nd three things (1) Addendum

Source: Internet
Author: User

In WinForm 2, 3 things (1), we talked about events on WinForm (such as clicking and double-clicking) through message loops and message distribution mechanisms. But that article is just a reference. Then someone asked me what is the specific relationship between them? Now let's talk in detail about the Event from Win32 Message to WinForm.

Hello world in Win32
To learn more about this problem, let's first look at how to make a simple Hello World form using Native API (or Native API) in Win32:

1: # include <windows. h>
2:
3: // This is where the message is received and then processed.
4: lresult callback WndProc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
5 :{
6: switch (msg)
7 :{
8: case WM_DESTROY: PostQuitMessage (0); return 0;
9: default: return DefWindowProc (hwnd, msg, wparam, lparam );
10 :}
11 :}
12:
13: int WINAPI WinMain (HINSTANCE instance, HINSTANCE prev, LPSTR using line, int cmdShow)
14 :{
15: // a form class, used to set various properties of the form, such as the size and background color.
16: WNDCLASSEX windowClass;
17: MSG msg;
18: HWND window;
19: memset (& windowClass, 0, sizeof (WNDCLASSEX ));
20: windowClass. cbSize = sizeof (WNDCLASSEX );
21: windowClass. hInstance = instance;
22: windowClass. lpszClassName = "HelloWorld ";
23: // note that when a message is sent to this form, it is processed by the WndProc function.
24: windowClass. lpfnWndProc = WndProc;
25: windowClass. hbrBackground = (HBRUSH) COLOR_WINDOW + 1;
26:
27: if (! RegisterClassEx (& windowClass ))
28 :{
29: MessageBox (NULL, "the Form class cannot be registered", "", MB_ OK | MB_ICONERROR );
30: return 1;
31 :}
32: window = createdomainwex (0, "MainWnd", "Hello World", WS_OVERLAPPEDWINDOW &~ WS_MAXIMIZEBOX,
33: CW_USEDEFAULT, CW_USEDEFAULT, 200,150, NULL, NULL, instance, NULL );
34: ShowWindow (window, cmbShow );
35:
36: // message loop. Obtain the message from the message queue and send messages separately.
37: // In fact, The GetMessage method will return-1 (indicating an error), so the following statements are not always correct.
38: while (GetMessage (& msg, NULL, 0, 0 ))
39 :{
40: TranslateMessage (& msg );
41: DispatchMessage (& msg );
42 :}
43:
44: return 0;
45 :}

Ah, so many things are required to bring up a blank window in Win32. Take a closer look at the above Code and do not go into the details of the Win32 underlying layer. We can understand this: GetMessage gets a message from the message queue of the current thread (what is a message? Click the mouse on the form, press the case on the keyboard, create the form, draw the form, and destroy the form. All the messages are sent to a message queue one by one ), then, TranslateMessage is responsible for converting the message (that is, converting the native message into a "understandable" message. For example, if you press F1, it will convert it to a WM_HELP message ), then, DispatchMessage is used to distribute the message. Where can it be sent? The preceding WndProc function. After the WndProc function receives a message, there is a switch in it. The switch determines what to do based on the message type, for example, you can receive a call function that is clicked by a button and then called by the button. The whole process is like a pipeline, which is executed step by step.

OK. So far, we will go back to. Net. Let's see how to compile a form in. Net?

Hello world in WinForm
1: public class HelloWorld: Form
2 :{
3: public HelloWorld ()
4 :{
5: InitializeComponent ();
6 :}
7:
8: private void InitializeComponent ()
9 :{
10: // is it similar to WNDCLASSEX settings in Win32?
11: this. Size = new Size (300,200 );
12: this. Text = "Hello World ";
13: this. Name = "HelloWorld ";
14:
15: this. Click + = new System. EventHandler (helloworld_Click );
16 :}
17:
18: private void helloworld_Click (object sender, EventArgs e)
19 :{
20: MessageBox. Show ("the form is clicked ");
21 :}
22 :}

Connection between Message and Event
Oh, for. Net code, we not only implement a blank form, but also add an event when you click the form. The code is concise and has no details. It seems that this is a little related to Win32, and there is no connection. How did the click form event trigger? We keep tracing the inheritance level of Form and find that Form is indirectly derived from Control, while the Control class finds a WndProc method:

1: protected virtual void WndProc (ref Message m)
2 :{
3: if (this. controlStyle & ControlStyles. EnableNotifyMessage) = ControlStyles. EnableNotifyMessage)
4 :{
5: this. onpolicymessage (m );
6 :}
7: switch (m. Msg)
8 :{
9: case 1:
10: this. WmCreate (ref m );
11: return;
12 ://............
13:
14: case 8:
15: this. WmKillFocus (ref m );
16: return;
17: case 0x200:
18: this. WmMouseMove (ref m );
19: return;
20:
21: case 0x201:
22: this. WmMouseDown (ref m, MouseButtons. Left, 1 );
23: return;
24:
25: case 0x202:
26: this. WmMouseUp (ref m, MouseButtons. Left, 1 );
27:

Related Article

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.