On the fundamentals of Windows message-driven (. NET platform)

Source: Internet
Author: User

The most recent lesson setting involves invoking system API functions and sending form messages, overloading receiving, and so on. Therefore, to publish a text, I hope to work with you to explore, learning.

First, Windows is a Windows-based message-driven system. This sentence is very interesting. The focus is on the two words "windows" and "message driven", two words containing the processing principle of the Windows interface. The following explains what is message driven. The so-called "message", or "event". has been exposed to C #. NET friends know that you drag a control, double-click, add code, this paragraph can be called "event". Seriously, the so-called "event" refers to what happens when people use a computer, such as pressing a key on the keyboard, moving the mouse, clicking the left mouse button, moving the window and selecting the Program menu, and so on.

So what is message-driven? Message-driven, also called event-driven. In layman's terms, you do an action on the form, Windows responds appropriately, and implements the function. This is called message-driven. Seriously, whenever an event occurs, that is, we do an action on the program, the program will produce a corresponding specific message, the message will be captured by Windows, and then this message will be invoked to execute the corresponding program code to implement the function. Windows creates a number of message queues for each generated message. Messages generated when the window is manipulated are placed in the message queue. Each application should have a message loop that repeatedly looks at its own message queue and, if there is a message, extracts it from the message queue using the GetMessage () function and executes the corresponding message-handling code. If there is no message in the queue to wait, when there is a message in the queue to deal with it, and so on, until the end of the program or encounter Win_quit;

Here's a look at this picture:

When the Run program-I event action raises a message, the message first exists in the system message queue, and then into the application message queue, and then the message queue is returned with the message loop extraction message, processing message, ....

The above explanation doesn't matter if you don't understand. Let's continue to explain slowly.

First, leave the message behind to drive the original question. Let's introduce a concept called "Dynamic link library". What is a dynamic link library? A friend who has just been in touch with C # may have heard it, but he hasn't learned much about it. No hurry, let's take it slow. Dynamic link library, this name is also very interesting. Divided into three sections, dynamics, links and libraries. First it is a library, what library? It is generally understood as a warehouse. A warehouse with a lot of functions on it. Then it is dynamic, what is dynamic? Dynamic is the beginning of the program is not, and so run to, need, go to the library to find the call. So why is it dynamic? Because if it is static, the program will always take a lot of code, it is not necessarily used, but the program space will become larger, efficiency and so will be affected. I do not know that the friend who read this article has written the program of C/s + +, there is a #include<xxx.h> header file in C/s, yes, this is "dynamic link". How to use the dynamic link library in C #, yes, [Dllimprot ("Xxx.dll")] class. In C #, use this class to declare an instance, declared. DLL file. You can call the API function (the API function is encapsulated in the. dll file). However, there are several requirements, that is, the calling API function name must be the same as the original system API function name, parameters are consistent, and must be declared as extern ... ; Sample:

1 int 0x520 ; 2 int 0x521; User-defined messages.
1[DllImport ("User32.dll", entrypoint="SendMessage"]//entrypoint indicates that the function entry point to be called is the SendMessage () function in the DLL file. 2 Private Static extern intSendMessage (3 INTPTR hwnd,//Declare a window handle, the so-called handle, is the "pointer" in C #, a reference to the window, but this "pointer" is not available for other special operations. IntPtr is a platform-specific type int. is 32-bit and 64-bit on 32-and 64-bit systems respectively. Typically used to declare a handle. 4    intmsg,//Indicates the message to be sent. 5    intWParam,//32-bit additional information. 6    intIprarm//32-bit additional information. (change with the change of message)7);

The function of the SendMessage () function is to send a message to one or more forms, and the corresponding function to receive information from the form. They are a natural pair. The receive function overload declaration is written to the program.

1 protected Override voidDefwndproc (refMessage m)//overloaded Receive handler functions2 { 3Switch(m.msg)4{ 5 CaseXxx1:label1. Text ="there?"; Break; 6 CaseXxx2:label1. Text ="Not available"; Break;//xxx1/xxx2 user-defined messages. 7default:Base. Defwndproc (refm); Break; 8}9}

The purpose of the Defwndproc () function is to allow the form to receive the corresponding "message" and make the processing, function implementation.

That's all you have to say, send a form message, and then let the form receive the message and process the message. So what does this have to do with our original purpose?

Windows applications actually have the same program structure and execution control flow. For example, the establishment of an API call program, the implementation process is as follows:

Program Entry point (DOS is main (), Windows is _tWinMain ()), Registration window class (registerclass), create window (CreateWindow ()), display window (ShowWindow ( Hwnd,ncmdshow) (UpdateWindow (HWND)), message loop (Wait for User Action window to generate messages), put messages in Message Queuing, and then read and execute the corresponding Code, window functions ( Decide what to display in the window, or how to respond to user input (such as the SendMessage () function above)), message processing (to determine what message the window function receives and how to handle it (such as the Defwndproc () function above).

The author of poor writing, knowledge is not solid, write just let everyone advice, but also hope to severely criticize, enhance each other.

Off topic: why learn. NET also learn the message-driven principle these are closer to the bottom of things? According to our teacher, you are in the school before graduation there are 20,000 lines of code, even this do not know, even if you have tens of thousands of lines, you can never make out how much good, more useful program to.

On the fundamentals of Windows message-driven (. NET platform)

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.