码迷,mamicode.com
首页 > Windows程序 > 详细

C#开发实例 鼠标篇

时间:2016-01-13 22:00:32      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:

鼠标的操作控制:

  鼠标是计算机的一个重要组成部分,有很多默认的设置,如双击时间间隔,闪烁频率,移动速度等,本篇使用C#获取这些基本的信息。

1.1获取鼠标信息

①实例001 获取鼠标双击时间间隔

主要用到的API函数为GetDoubleClickTime。函数主要用来判断连续2次鼠标单击之间会被处理成双击的时间间隔。

技术分享

主要程序代码如下:

1         [DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
2 
3         public static extern int GetDoubleClickTime();
4 
5         private void Frm_Main_Load(object sender, EventArgs e)
6         {
7 
8             label1.Text = GetDoubleClickTime() + "毫秒";
9         }

 

②实例002 获取光标闪烁频率

主要用到的API函数为GetCaretBlinkTime,获取光标闪烁的时间,返回值为int类型,毫秒为单位。

技术分享

1         [DllImport("user32.dll", EntryPoint = "GetCaretBlinkTime")]
2 
3         public static extern int GetCaretBlinkTime();
4 
5         private void Frm_Main_Load(object sender, EventArgs e)
6         {
7 
8             label1.Text = GetCaretBlinkTime() + "毫秒";
9         }

 

③实例003 获取鼠标键数

鼠标种类多种多样,除了左右键,还有滚轮,有的带功能键,主要用到的API函数为GetSystemMetrics,intcount指定欲获取的信息。

技术分享

 1         public const int SM = 43;
 2 
 3         [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
 4 
 5         public static extern int GetSystemMetrics(int intcount);
 6 
 7         private void Frm_Main_Load(object sender, EventArgs e)
 8         {
 9             int intCon = GetSystemMetrics(SM);
10             label1.Text = "鼠标键数" + intCon + "";
11         }

 

④实例004 显示鼠标等待光圈

主要用到了Form类的Cursor属性,Cursor属性主要用来获取或设置当鼠标指正位于窗体上时显示的光标。

技术分享

1         private void Frm_Main_Load(object sender, EventArgs e)
2         {
3             this.Cursor = Cursors.WaitCursor;
4         }

⑤获取鼠标在窗体上的位置

获取鼠标位置在开发过程中很重要,通过单击鼠标获取鼠标的当前位置。主要用到MouseEventArgs类的X和Y属性,MouseEventArgs类为MouseUp、MouseDown、MouseMove事件提供数据。

1         private void Frm_MouseDown(object sender, MouseEventArgs e)
2         {
3             this.label1.Text = e.X.ToString;
4             this.label2.Text = e.Y.ToString;
5         }

 

⑥记录鼠标行为

⑦通过截取系统消息判断鼠标的单击键

1.2鼠标基本设置

①鼠标指针形状

鼠标的指针经常会有多种样式,如“等待”,打开链接的“小手”。本实例是将鼠标放到label上显示小手。

1       this.label1.Cursor = Cursors.Hand;

技术分享

②鼠标的图形样式

用户使用鼠标时,为了美观,自定义鼠标图片。

1         private void button1_Click(object sender, EventArgs e)
2         {
3             this.Cursor = new Cursor("pen_il.cur");   //改变鼠标图片
4         }

 

③左右键互换功能

在控制面板中,用户可以对鼠标做相应的设置,在这里左右键互换用到的API函数为SwapMouseButton。在SwapMouseButton()中,返回为真交换左右键,否则恢复正常。

 1         [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
 2 
 3         public static extern int SwapMouseButton(int bSwap);
 4 
 5         public void DefultRightButton()
 6         {
 7             SwapMouseButton(1);
 8         }
 9         public void DefultLeftButton()
10         {
11             SwapMouseButton(0);
12         }
13 
14         private void button1_Click(object sender, EventArgs e)
15         {
16             this.DefultLeftButton();
17         }
18         private void button2_Click(object sender, EventArgs e)
19         {
20             this.DefultRightButton();
21         }

技术分享

④固定鼠标控制区域

在实际应用中,有时候要限制鼠标的移动区域,让其只能在某一区域移动。

 1         private void button1_Click(object sender, EventArgs e)
 2         {
 3             this.Cursor = new Cursor(Cursor.Current.Handle);
 4             Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
 5             Cursor.Clip = new Rectangle(this.Location, this.Size);
 6         }
 7         private void button2_Click(object sender, EventArgs e)
 8         {
 9             Screen[] screens = Screen.AllScreens;
10             this.Cursor = new Cursor(Cursor.Current.Handle);
11             Cursor.Clip = screens[0].Bounds;
12         }

技术分享

 

1.3鼠标操作在实际中的应用

①隐藏鼠标按钮

在实际应用中,有时候会将鼠标隐藏,通过使用键盘来操作。主要用到的API函数是ShowCursor。ShowCursor(bool bShow);bshow为true显示指针,如果为false隐藏指针。

 1         [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
 2         public static extern bool ShowCursor(bool bShow);
 3 
 4         private void button1_Click(object sender, EventArgs e)
 5         {
 6             ShowCursor(false);//隐藏鼠标
 7         }
 8         private void button2_Click(object sender, EventArgs e)
 9         {
10             ShowCursor(true);//显示鼠标
11         }

技术分享

C#开发实例 鼠标篇

标签:

原文地址:http://www.cnblogs.com/SeekHit/p/5125713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!