C # handles mouse and keyboard events

Source: Internet
Author: User

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 )
{
此处加入响应此事件的代码
}

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 )
{
此处加入响应此事件的代码
}

Note: the "Button1" in the above program is defined as a button component.

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.