In a program run, there are many bodies that produce events, especially the keyboard and mouse. This article explores the process of handling events related to these two principals in C #.
A This article describes the program design and operation of the software environment:
(1) Microsoft company Windows 2000 Server Edition
(2). Net FrameWork SDK Beta 2
Two Handle mouse-related events in C #:
There are approximately six mouse-related events, respectively:
"MouseHover", "MouseLeave", "MouseEnter", "MouseMove", "MouseDown" and "MouseUp".
(1). How to define these events in a C # program:
In C #, the above events are described by different delegate, where the delegate describing "MouseHover", "MouseLeave", "MouseEnter" Events is "EventHandler", The delegate that describes the three events that follow are described as "MouseEventHandler". These two delegate are encapsulated in different namespaces, where "EventHandler" is encapsulated in the "System" namespace, and "MouseEventHandler" is encapsulated in the "Syetem.Windows.Froms" namespace. The class that passes data for "MouseHover", "MouseLeave", "MouseEnter" Events is "EventArgs" and is encapsulated in the "System" namespace, while the class that provides data for the following three events is " MouseEventArgs, "he was encapsulated in the" Syetem.Windows.Froms "namespace. These determine that there are different approaches to defining these events in C # and responding to them. Here are some of the differences.
For the first three events mentioned above, the following syntax is used to define:
"Component name." Event name "+ = new System.EventHandler (" event name ");
The following is the specific implementation code in the program:
Button1. MouseLeave + = new Syetem.evenhandler (button1_mleave);
After the definition of the event is completed, the code that responds to this event is added to the program, or the program compiles an error. The following is the basic structure that responds to the above events.
private void Button1_mleave (object sender, System.EventArgs e)
{
Code in response to this event is added here
}
The syntax for defining the "MouseMove", "MouseDown", and "MouseUp" events is roughly the same as the three events described earlier:
"Component name." Event name "+ = new System.Windows.Forms. MouseEventHandler ("event name");
The following is the specific implementation code in the program:
Button1. MouseMove + = new System.Windows.Forms.MouseEventHandler (button1_mmove);
The following is the basic structure in response to the above events:
private void Button1_mmove (object sender, System.Windows.Forms. MouseEventArgs e)
{
Code in response to this event is added here
}
Note: the "Button1" in the above program is defined as a button component.
2. Mouse related events in the typical problem-handling methods:
Having mastered the definition and mouse-related events in C #, we'll explore typical problems with mouse-related events. The first is to read the current position of the mouse, and the second is to determine whether the mouse button press move.
Determining the position of the mouse can be handled by the event "MouseMove", which provides two properties "X" and "Y" in the "MouseEventArgs" class to determine the current mouse ordinate and horizontal coordinates. The mouse button to determine the press, you can use the event "MouseDown" to deal with, and in the "MouseEventArgs" class also provides a property "button" to determine the mouse button situation. Based on this knowledge, you can get the program code written in C # to read the mouse's current position and determine the mouse button. Here's the code (Mouse.cs) and the compile-time interface for this code:
The source code for the Mouse.cs 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://Initialize individual components in a form
InitializeComponent ();
}
file://resources used in the cleanup program
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
private void InitializeComponent ()
{
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (292, 273);
This. Name = "Form1";
This. Text = "C # handles mouse press events!" " ;
FILE://defines an event-handling process for mouse movement "Form1_mousedown"
This. MouseDown + = new MouseEventHandler (Form1_mousedown);
FILE://defines an event-handling process for mouse movement "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 current mouse position is: (" + e.x + "," + E.y + ")";
}
private void Form1_mousedown (object sender, MouseEventArgs e)
{
FILE://responds to different mouse buttons
if (E.button = = MouseButtons.Left)
{
MessageBox.Show ("Press the left mouse button!") " ) ;
}
if (E.button = = Mousebuttons.middle)
{
MessageBox.Show ("Press the middle mouse button!") ") ;
}
if (E.button = = mousebuttons.right)
{
MessageBox.Show ("Press the right mouse button!") ") ;
}
}
}
Three Handling keyboard-related events in C #:
In C # and keyboard-related events are relatively small, roughly three kinds: "KeyDown", "KeyUp" and "KeyPress."
(1). How to define these events in a C # program:
The delegate of events that describe "KeyDown" and "KeyUp" in C # is "Keyeventhandler". The delegate used to describe "KeyPress" is "Keypresseventhandler". These two delegate are encapsulated in the namespace "Syetem.Windows.Froms". The class that provides data for events for "KeyDown", "KeyUp" is "KeyEventArgs". The class that provides data for the "KeyPress" event is "KeyPressEventArgs." Likewise, the two are encapsulated in the namespace "Syetem.Windows.Froms".
The syntax for defining "KeyDown" and "KeyUp" events in the C # program is as follows:
"Component name." Event name "+ = new Syetem.Windows.Froms. Keyeventhandler ("event name");
The following is the specific implementation code in the program:
Button1. KeyUp + = new Syetem.Windows.Froms. Keyeventhandler (Button1_kup);
The following is the basic structure that responds to the above events.
private void Button1_kup (object sender, Syetem.Windows.Froms. KeyEventArgs e)
{
Code in response to this event is added here
}
The syntax for defining the "KeyPress" event in a C # program is as follows:
"Component name." Event name "+ = new Syetem.Windows.Froms. Keypresseventhandler ("event name");
The following is the specific implementation code in the program:
Button1. KeyPress + = new Syetem.Windows.Froms. KeyPressEventArgs (button1_kpress);
After the definition of the event is completed, the code that responds to this event is added to the program, or the program compiles an error. The following is the basic structure that responds to the above events.
private void Button1_kpress (object sender, Syetem.Windows.Froms. KeyPressEventArgs e)
{
Code in response to this event is added here
}
Note: the "Button1" that appears in the program is a defined button component.
(
2. and keyboard related events in the typical problem-handling methods:
The typical problem with a keyboard is to determine which button is pressed. All of the above three events can be done. And in the "KeyEventArgs" class passed a property "KeyCode", you can use him to read the current key. So just deal with the problem in the "KeyUp" or "KeyDown" event. Based on the above knowledge, you can get the program code to write read-read keys in C #, and here's the code (Key.cs) and the interface after this code is run:
Figure 02: The program run interface with C # reading keyboard keys
The code for Key.cs 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://Initialize individual components in a form
InitializeComponent ();
}
protected override void Dispose (bool disposing)
{
file://resources used in the cleanup program
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
private void InitializeComponent ()
{
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (292, 273);
This. Name = "Form1";
This. Text = "C # handles keyboard events!" " ;
FILE://defines an event-handling process "Form1_keyup" for pressing the key.
This. KeyUp + = new Keyeventhandler (this. Form1_keyup);
}
static void Main ()
{
Application.Run (New Form1 ());
}
file://Display the key name you pressed
private void Form1_keyup (object sender, KeyEventArgs e)
{
MessageBox.Show (E.keycode.tostring (), "The health you are pressing is:");
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.