图01:用C#读取鼠标位置和鼠标按键的程序运行界面
mouse.cs的源程序代码如下:
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://初始化窗体中的各个组件InitializeComponent ( ) ;}file://清除程序中使用过的资源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#处理鼠标按动事件!" ;file://为鼠标按动定义一个事件处理过程"Form1_MouseDown"this.MouseDown += new MouseEventHandler ( Form1_MouseDown ) ;file://为鼠标移动定义一个事件处理过程"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 = "当前鼠标的位置为:( " + e.X + " , " + e.Y + ")" ;}private void Form1_MouseDown ( object sender , MouseEventArgs e ){file://响应鼠标的不同按键if ( e.Button == MouseButtons.Left ){MessageBox.Show ( "按动鼠标左键!" ) ;}if ( e.Button == MouseButtons.Middle ){MessageBox.Show ( "按动鼠标中键!") ;}if ( e.Button == MouseButtons.Right ){MessageBox.Show ( "按动鼠标右键!") ;}}}
三.C#中处理和键盘相关的事件:
在C#中和键盘相关的事件相对比较少,大致就三种:"KeyDown"、"KeyUp"和"KeyPress"。
(1).如何在C#程序中定义这些事件:
C#中描述"KeyDown"、"KeyUp"的事件的Delegate是"KeyEventHandler"。而描述"KeyPress"所用的Delegate是"KeyPressEventHandler"。这二个Delegate都被封装在命名空间"Syetem.Windows.Froms"中。为"KeyDown"、"KeyUp"的事件提供数据的类是"KeyEventArgs"。而为"KeyPress"事件提供数据的类是"KeyPressEventArgs"。同样这二者也被封装在命名空间"Syetem.Windows.Froms"中。
在C#程序定义"KeyDown"、"KeyUp"事件的语法如下:
"组件名称"."事件名称"+= new Syetem.Windows.Froms. KeyEventHandler("事件名称");
下面是程序中具体实现代码:
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
button1. KeyUp += new Syetem.Windows.Froms. KeyEventHandler(button1_KUp);
下面是响应上面事件的基本结构。
private void button1_KUp ( object sender , Syetem.Windows.Froms. KeyEventArgs e ){此处加入响应此事件的代码}(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
在C#程序定义"KeyPress"事件的语法如下:
"组件名称"."事件名称"+= new Syetem.Windows.Froms. KeyPressEventHandler("事件名称");
下面是程序中具体实现代码:
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
button1. KeyPress += new Syetem.Windows.Froms. KeyPressEventArgs(button1_KPress);
在完成了事件的定义以后,就要在程序中加入响应此事件的代码,否则程序编译的时候会报错。下面是响应上面事件的基本结构。
private void button1_KPress ( object sender , Syetem.Windows.Froms. KeyPressEventArgs e ){此处加入响应此事件的代码}
注释:程序中出现的"button1"是定义的一个按钮组件。
(2).和键盘相关事件中的典型问题处理办法:
和键盘相关的典型问题无非就是判定到底是哪个按键被按动。通过上面的三个事件都可以完成。并且在"KeyEventArgs"类中通过了一个属性"KeyCode",可以用他来读取当前按键。所以就在"KeyUp"或者"KeyDown"事件中处理这个问题。根据上面这些知识,可以得到用C#编写读取读取按键的程序代码,下面就是此代码(key.cs)和此代码运行后的界面:
图02:用C#读取键盘按键的程序运行界面
key.cs的代码如下:
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://初始化窗体中的各个组件InitializeComponent ( ) ;}protected override void Dispose ( bool disposing ){file://清除程序中使用过的资源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#处理键盘事件!" ;file://为按键的按动定义一个事件处理过程"Form1_KeyUp"this.KeyUp += new KeyEventHandler ( this.Form1_KeyUp ) ;}static void Main ( ){Application.Run ( new Form1 ( ) ) ;}file://显示你所按动的按键名称private void Form1_KeyUp ( object sender , KeyEventArgs e ){MessageBox.Show ( e.KeyCode.ToString ( ) , "您所按动的健为:" ) ;}}
四.总结:
本文介绍了在C#中如何定义和鼠标和键盘相关的事件和在这些事件中一些典型问题的处理办法。虽然这些知识最为基本,但也最为重要,因为在程序设计中,这些问题和我们打交道的机会最多。当然和鼠标和键盘相关的事件和问题还有许多,可以参照根据上面的解决办法加以解决。