In the operation, there are many mains that generate events, especially in keyboards and mice. This article explores the processing of events related to these two subjects in C #. One. Software Environment for programming and running this article: (1). Microsoft Window 2000 Server Edition (2) .. Net Framework SDK Beta 2. C # Processing mouse related events: Six species related to mouse-related events: "Mousehover", "MouseEleAve", "MouseEnter", "MouseMove", "MouseDown", "MouseUseup". (1). How to define these events in the C # program: In C #, the above event is described in different delegate, where "Mousehover", "MouseEleAve", "mouseEnter" event is "EventHandler", and Description Delegate of the three events later is "mouseeventhandler" to describe it. These two delegate are packaged in different namespaces, where "EventHandler" is encapsulated in the "system" namespace; "MouseEventHandler" is encapsulated in "SyeTem.Windows.froms" namespace. In the "Mousehover", "MouseEleAve", "MouseEnter" event is "Eventargs", and he is also encapsulated in the "system" namespace; and the class that provides data is "MouseEventArgs" for the three events behind. He was encapsulated in the "Syetem.Windows.froms" namespace. These have decided to define these events and respond to these events in C #. Here are these different points. For the first three events described above: "Component Name". "Event Name" = New System.EventHandler ("Event Name"); Button1.MouseLeave = New SyeTem.Evenhandler (Button1_mleave); After completing the definition of the event, you will add the code to respond to this event in the program, otherwise it will report an error when the program is compiled. Here is the basic structure in response to the above event.
Private void button1_mleave (Object sender, System.EventArgs e) {This addepts the code} of this event} Defines the syntax of "Mousemove", "MouseDown" and "Mouseup" events, and the three events previously introduced are roughly the same, as follows: "Component Name". "Event Name" = New System.Windows.Forms. MouseEventHandler ("Event Name"); below is the specific implementation code in the program: Button1.Mousemove = new system.windows.Forms.MouseEventhandler (Button1_mmove) The following is the basic structure of the above event: private void button1_mmove (Object sender, system.windows.forms. MouseEventArgs e) {here to add a response to this event} Note: "Button1" in the above programs is a definition Button components. 2). Typical problems in mice related events: Type of incidents in C # in C #, we will explore typical issues related to mouse-related events. One is to read the current position of the mouse; its second is to determine that the mouse button is pressed. Determination of the position of the mouse can be processed by event "mousemove", two attributes "X" and "Y" are provided in the "MouseEventArgs" class to determine the current mouse shade and the abscissa. To determine the motion of the mouse button, you can process the event "mousedown" and a property "button" is also provided in the "MouseEventArgs" class to determine the mouse button. Based on this knowledge, you can get a read mouse current location with C # and determine the program code of the mouse button.
Below is this code (mouse.cs) and this code compiles run interface: Mouse.cs source code code as follows: use system; use system.drawing; use system.collections; using system.componentmodel; using system.windows.Forms Using system.data; public class form1: form {private system.componentmodel.container components = null; public form1 () {file: // Initialization in itIzecomponent ();} file: // Clear program The useful resource protected {if (disposing) {if (component! = Null) {components.dispose ();}} Base.dispose ();}. AutoScaleBasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (292, 273); this.name = "form1"; this.text = "C # handle the mouse Press Event ! "; File: // For the mouse, define an event handler" Form1_MouseDown "this.mousedown = new mouseeventhandler (Form1_MouseDown); file: // Defines an event handler for mouse to define an event process" form1_mousemove "this.mousemove = New mouseeventhandler (form1_onmousemove);} static void main () {Application.Run (New Form1 ());} Private Void Form1_onmousemove (Object) Sender, MouseEventArgs E) {this.text = "The location of the current mouse is: (" EX "," EY ")";} private void form1_mousedown (Object sender, mouseeventargs e) {file: // Response mouse Different buttons if (e.button == mousebuttons.left) {messagebox.show ("Press the left mouse button! ");} if (e.button == mousebuttons.middle) {messagebox.show (" Press the mouse ");} if (e.button == mousebuttons.right) {messagebox.show (" Press Right click on the mouse! .
(1). How to define these events in the C # program: "KEYDOWN", "Keyup" event is "KeyEventHandler" in the C # program. Describes that delegate used by "KeyPress" is "KeyPressEventHandler". These two delegate are packaged in the namespace "SyeTem.Windows.froms". The class for "keydown" and "keyup" is "keyeventargs". The class that provides data for the "KeyPress" event is "keypressEventArgs". The same is also packaged in the namespace "SyeTem.windows.froms". The syntax of "KeyDown" and "Keyup" event in the C # program is as follows: "Component Name". "Event Name" = New Syetem.Windows.froms. KeyEventHandler ("Event Name"); below is the specific implementation code in the program : Button1. Keyup = New Syetem.windows.froms. KeyEventHandler (Button1_kup); The following is the basic structure of the above event. Private Void Button1_kup (Object Sender, Syetem.Windows E) {Add to Response This event} The syntax of the "KeyPress" event in the C # program is as follows: "Component Name". "Event Name" = NEW SYETEM.WINDOWS.FROMS. KeypressEventHandler ("Event Name"); below is the specific implementation code in the program: Button1. Keypress = new sytem.windows.froms. KeypressEventArgs; after completing the event's definition, Add the code to respond to this event in the program, otherwise it will report an error when compiling. Here is the basic structure in response to the above event. Private void button1_kpress (Object sender, sytem.windows.froms. KeypressEventArgs e) {This addept to the code} Note: "Button1" that appears in the program is a button component defined. (2). Typical problems in the related events in the keyboard: Typical issues related to the keyboard are nothing more than the determination which button is pressed. It can be done by three events above. And in the "KeyEventargs" class, by "Keycode", you can use him to read the current button. So dealing with this problem in the "Keyup" or "KeyDown" event.
According to these knowledge, you can get the program code to read the read button with C #, which is the interface after this code (key.cs) and this code run: Figure 02: Running the program running with the C # read keyboard button key.cs interface code is as follows: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1: Form {private System.ComponentModel.Container Components = NULL; Public Form1 () {file: // Initialization Initialization (); ()} protected}} protected}} protected}} protections {file: // Clear the resource IF used in the program { IF (Components! = null) {Components.dispose ();}} Base.dispose (4); New system.drawing.size (292, 273); this.name = "form1"; this.text = "C # handles keyboard event!"; file: // For the button's piping "Form1_Keyup" THISKEYUP = new keyeventhandler (this.form1_keyup);} static void main () {Application.run (new form1 ());} file: // Displays the button name you press Private Void Form1_Keyup (Object Sender, KeyEventArgs e) {MessageBox.show (E.Keycode .Tostring (), "The elevation you press:");}}
http://blog.9cbs.net/cilong521/
related articles
[Reprinted] Block Visual Basic .NET or C # code reverse engineering [reproduced] C # handle the mouse and keyboard event with C # read and write the INI file Using the RAW Socket under C # to implement a simple analysis and security of the network package monitoring SNIFFER