In-depth light-out incident (below)

xiaoxiao2021-04-02  206

In-depth light-out incident (below)

two. Event origin

In the traditional concept of object-oriented concept, there is no "event" concept. Traditional object-oriented concepts only data (DATA, also known as Field, domain, member variable), and method (Method, is also member functions, function). If I have not remembered, then this concept appears in Microsoft's COM technology, but because VB is based on ActiveX (one of COM), the concept of "incident" has passed through VB, for many Programmers are well known and used - I am one of them.

.NET Framework is actually a higher level package for COM - to know, before the name .NET Framework, it is called "COM

3

"

Come - naturally retained support for events.

three. Event meaning

"Evolution Theory" said: "The product is brought, reasonable,"

Microsoft said: "I am a boss, there is reasonable!"

No matter whether Microsoft is playing big cards, the presence of merits - the existence of an event has brought a lot of convenience to the development of the procedure. From the design level, it makes the procedure concise and clear, easy to maintain; from the technical level, it is a beautifully unknown Windows message delivery mechanism, which greatly reduces programmers. The entrance to the job.

From a software engineering perspective, an event is a notification mechanism that is a way to maintain synchronization between classes and classes.

Ask: What is synchronization?

Answer: Message synchronization!

four. Essence of an event - package delivery

The event can be excited (fire, "also known as" trigger "), and a member event contained in a class can be excited in a variety of situations. The most typical: a button Click event, can be stimulated by the user to use the mouse, or by testing another software of this software through the Win32 API function to stimulate it.

Let's briefly discuss this CLICK event:

In fact, if you understand the nature of Win32, you should understand that users can't get directly to a control. On the surface, it is indeed that the user clicks on the button with the mouse. In fact, when the user presses the left mouse button, it is sent by the mouse to the Windows operating system "Left-click [X, Y]" message, and then Windows will take this according to the position of [X, Y]. Message assignment (routing) gives controls that should receive it - this is the message delivery / routing mechanism of Windows.

Similarly, when you move the mouse, it seems that the pointer is moving with your will, and it is actually your mouse to report the current location to the Windows operating system with a few hundred times per second, and then Windows will Painting a beautiful pointer "painting" to you on the screen - haha, we are all cheated!

However, these contents are invisible for C # programmers - all packaged as "events". Therefore, from the mechanism of Windows systems, the event mechanism is a package for Windows message delivery mechanism.

The following code is an simulation of the WinForm program automatically generated by Visual Studio 2005. After reading, you can write a WinForm yourself and analyze the mechanism.

Code:

/ / ============ 水 = ============ //// //// http://blog.9cbs.net/fantasiax // // / / / ======== On the water, the moisturizer is silent ========== // use system; use system.collections.Generic; use system.text; using system.windows. Forms; // Add a reference to System.Windows.Forms and System.Drawing assemblies! Using system.drawing; namespace emulatewinform {// Custom EmulateForm class, derived from the Form class. Class emulateform: form {// Two control private button mytextbox; // Initialize the various controls and form itself and adds controls to the form of the control.

private void InitializeComponent () {myButton = new Button (); myTextBox = new TextBox (); myButton.Location = new System.Drawing.Point (195, 38); myButton.Size = new System.Drawing.Size (75, 23 ); Mybutton.text = "Click Me"; MyButton.Click = new eventHandler (MyButton_Click); // Mounting Event Processing Function MyTextBox.location = New System.drawing.point (12, 12); mytextbox.size = New System.drawing.size (258, 20); controls.add (mybutton); text = "emulateform";} // myButton's Click event occurs, EmulateForm class is given to event response functions (Event Handler) Void MyButton_Click (Object Sender, Eventargs E) {MyTextBox.text = "Hello, Event World!";} // Explore the initialization method in the constructor of the EmulateForm class public emulateform ();}} Class Program {static voidmain

(String [] args) {emulateform myform = new emulateform (); Application.run (myForm);}}} code analyzer:

1. To reference using system.drawing; using system.windows.forms; these two Namespace, first manually add a reference to System.drawing and System.Windows.Forms.

2. The EmulateForm class is customized, note that it is derived from the FORM class. For the sake of clarity, I have simplified the code to almost the simplest ... only two member variables. MyButton is an instance of the Button class; MyTextBox is an instance of the TextBox class. The members of the EmulateForm class private void initializeComponent () is exactly the imitation of the real WinForm program. In its function body, instantiate, initialize the member variable, initialize (such as determining size and location), and add them to the form A Controls array. This function will be executed in the constructor of the emulateform.

3. The most important part of this example is to initialize myButton. Note this sentence: MyButton.Click = New EventHandler (MyButton_Click); MyButton.click is MyButton's Click event, you may be strange: How can you declare an incident this time? Oh, because .NET Framework has been prepared for us, you will use it directly. However, we are in order to explore the bottom, so I have to take care of this incident.

4. Detailed analysis of the Button.Click event: First, the event is based on the delegate, then which delegate is based on the MyButton.click event? By looking MSDN, you can find myButton.Click inherited from the Control class, and the EventHandler delegate based - The following is a statement EventHandler delegate [SerializableAttribute] [ComVisibleAttribute (true)] public delegate void EventHandler (Object sender, EventArgs e) If you don't know how the event is declared, go back to the top of "In-depth". " In this statement, in square brackets is Attribute, you don't have to pay attention to it. The key is to see EventHandler's delegate: This delegate parameter list requires the function it histed (for an event is hidden event handler) should have two parameters - Object type Sender and Eventargs type E. What role does this two parameters? Oh, in fact, it is very fun - I have said, the event mechanism is the package of the message mechanism. You can understand the news into a shell, Sender is "Who is a shell", e is "What is the shelling" Of course, the target of the shell is of course the reception of the message. We carefully review the FireEventArgs class written by the last article: Is there two member variables in this class? One is a FLOOR that represents the fire floor, one is representative fire level FireElevel, with the FireAlarmRing event of the Building class instance, the instance E of the FireEventargs class is transmitted to an instance of the Employee class and the Fireman class, and the two instances open "Cannonball" gives the corresponding processing based on the launched content. Just like the shells in the real war, there are regular bombs, wear a bombs, burner, etc., our "news cannonball" is not only one, believe in a few people with everyone: 1 Eventargs class: This is used in the CLICK event The one. It is a regular bomb. Because the user clicking the button is a very simple event, it is not required to carry more information. 2 MouseEventArgs class: is fired by Mousemove, Mouseup, MouseDown event. Its instance carries a lot of other information, the most commonly used one is a x and one y - I can think about it with leg stomach, it is the current position of the mouse. In the following example we show it. 3 Painteventarg Class: Send it from the Paint event. This shell is not simple, and those very beautiful custom controls are inseparable from it! In its belly, there is a Graphics that represents a "canvas" you can paint on the top of the "canvas" ... OK, first list 3 MSDN has their family portraits, the location is System.Eventargs Derived Classes tree. Microsoft can be described as a good job in .NET Framework, Microsoft, from these EVENT ARGS (event parameters), to a variety of entrustments, and then to the five-flowers, all have made good packages, we only need to take it out, it is . 5. Void MyButton_Click (Object Sender, Eventargs E) is the response function of the EmulateForm class for MyButton.Click events (also known as event processors, EventHandler). Pay attention to its parameter list, is it consistent with the EventHandler: P

6. The main program has nothing to say - anew an instance of an emulateform, and use the Application.Run method to execute the program. 7. By the way, it is a correction: What has been explained above - it is the sender of the message. I have repeatedly found this kind of "incident sender" in some books, this is wrong! You think, the incident can only be triggered, excited, how can it be "send"? Not logical ...

Job 1:

Create a WinForm program, as shown. Contains 1 PANEL, 3 Textbox, 1 Button.

Claim:

1. When the mouse slides in Panel, TextBox1 and TextBox2 displays the current X and Y mouse.

2. When the mouse click on the button, TextBox3 wants to display Hello Events World!

prompt:

1. Pay your heart Mousemove event E

2. Trying to use the Visual Studio 2005 use C # 2.0, and use the Partial keyword to store the FORM1 class code in Form1.cs and Form1.Designer.cs, respectively.

Job 2:

Upgrade the program of "in-depth" in-depth event (on) "嘎子 使用 to the version of the event. (I will give it in the next day).

Over

Legal Notice: This article is protected by intellectual property law. If any unit or individual needs to reprint this article, it is necessary to ensure the integrity of the article (any of the authorized or change will be considered infringement). If you need to reprint, be sure to indicate 9CBS in the article to protect the rights of the website; be sure to indicate that the author is Liu Tie, and send mail to Blade @tom.com, indicating the location and use of article. When reprint, please document this legal statement, thank you!

转载请注明原文地址:https://www.9cbs.com/read-131313.html

New Post(0)