遥远的梦
--心有多大, 世界就有多大
随笔- 97  文章- 97  评论- 503 
博客园  首页  新随笔  联系  管理  订阅 订阅
鼠标 知多少?

鼠标重要性不必多说, 我们这些"小毛孩"可能不敢想象没有鼠标时的计算机是如何操作的, 我承认鼠标不是不可替代, 但也已不可或缺.

大多鼠标编程都与 API 函数有着某种关系, 毕竟鼠标的操作已进入了非纯软件领域. 因此, 要对鼠标下刀, 就得熟悉相关的 API 函数, 这点无可厚非.

今天我们就对鼠标编程进行一个大杂汇. 工作中能否用到, 不太清楚..... 

下面就分四个部分讲述我们与鼠标的不解之缘.

 

一. 应用篇:

1. 利用鼠标绘图

很多很多书籍, 很多很多网页都在讲述这个东东, 为了不找骂, 这里直接附代码! 

实现: 利用窗体的 MouseDown, MouseMove, MouseUp事件及Pen, Graphics等类实现.

代码: 

鼠标绘图
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ziyiMouse1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pen
= new Pen(Color.FromName("black"));//始末画笔
graphics = CreateGraphics();//初始画板
}

public bool G_OnMouseDown = false;//控制画图
public Point lastPoint = Point.Empty;
public Pen pen;
public Graphics graphics;

//将上一个点的LastPoint的值设为目前点的currPoint值.
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (lastPoint.Equals(Point.Empty))
{ lastPoint
= new Point(e.X, e.Y); }
if (G_OnMouseDown)
{
Point cruuPoint
= new Point(e.X, e.Y);
graphics.DrawLine(pen, cruuPoint, lastPoint);
}
lastPoint
= new Point(e.X, e.Y);
}

//当鼠标离开时把控制画图设为false;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
G_OnMouseDown
= false;
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
G_OnMouseDown
= true;
}

private void Form1_Load(object sender, EventArgs e)
{

}

}
}

效果:

 

 

2. 限制鼠标只在某一区域内工作

去过网吧的小朋友都知道, 开机后一段时间内鼠标只能在一个很小的区域内移动, 并且提示 "系统正在初始化, 鼠标已锁"之类似东东. 

原理: 利用 API 函数 ClipCursor和 GetWindowRect 联手实现. 前者指定区域大小, 后者获得整个窗口的范围矩形, 窗口的边框等信息.

代码:

限制鼠标活动区域
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ziyiMouse2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[System.Runtime.InteropServices.DllImport(
"user32", EntryPoint = "ClipCursor")]
public extern static int ClipCursor(ref RECT lpRect);
[System.Runtime.InteropServices.DllImport(
"user32.dll", EntryPoint = "GetWindowRect")]
public extern static int GetWindowRect(int hwnd, ref RECT lpRect);

public struct RECT//声明参数的值
{
public int left;
public int top;
public int right;
public int bottom;
}
public void Lock(System.Windows.Forms.Form ObjectForm)
{
RECT _FormRect
= new RECT();
GetWindowRect(ObjectForm.Handle.ToInt32(),
ref _FormRect);
ClipCursor(
ref _FormRect);
}
public void UnLock()
{
RECT _ScreenRect
= new RECT();

_ScreenRect.top
= 0;
_ScreenRect.left
= 0;
_ScreenRect.bottom
= System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
_ScreenRect.right
= System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
ClipCursor(
ref _ScreenRect);
}
private void bntKong_Click(object sender, EventArgs e)
{
this.Lock(this);
}

private void bntMove_Click(object sender, EventArgs e)
{
this.UnLock();
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

 

3. 利用鼠标拖放复制(剪切)文本

现在基本上所有的文本编辑器都有这个功能: 选中一行字, 然后按下鼠标拖动到另一个位置然后放手即可. 这里演示将一个简单的: 将一个文本框中的内容直接拖动到另一个文本框中(复制粘贴--平时用的文本编辑器是剪切粘贴原理). 

原理: 将第二文本框 ( 目标文本框 ) 的 AllowDrop 属性设为 True. 它指示控件是否可以接受拖放到它上面的数据. 然后再定义一些 MouseMove, DragDrop, DragEnter等事件.

代码:  

利用鼠标拖放复制文本
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ziyiMouse3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void txt1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
this.Cursor = new Cursor("arrow_l.cur");
DragDropEffects dropEffect
= this.txt1.DoDragDrop
(
this.txt1.Text, DragDropEffects.Copy | DragDropEffects.Link);
}
}

private void txt2_DragDrop(object sender, DragEventArgs e)
{
txt2.Text
= e.Data.GetData(DataFormats.Text).ToString();
}

private void txt2_DragEnter(object sender, DragEventArgs e)
{
e.Effect
= DragDropEffects.Copy;
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

 

二. 小 CASE 篇

1. 鼠标双击窗体时模拟键盘 Tab 键实现光标移位

原理: 在窗体的 DoubleClick事件中使用 SendKeys 类的 Send 方法实现.

代码: 

鼠标模拟键盘Tab键功能
private void Form1_DoubleClick(object sender, EventArgs e)
{
SendKeys.Send(
"{Tab}");
}

 

2. 定义鼠标形状

原理: 每一个 Winform 控件都有一个 Cursor 属性, 其作用就是改变鼠标形状的. 

( 比如, 常用的是在读大量数据时可以将鼠标形状定义为"忙", 即专业术语--等待状态, 而  当数据读完后恢复默认...) 

代码: 

定义鼠标形状
private void Form1_Load(object sender, EventArgs e)
{
this.labCur .Cursor =Cursors.Hand;
}

 

3.自定义鼠标图片

原理: 将整个窗体( 注, 不是控件)的 Cursor 属性重新定义.

代码: 

自定义鼠标图片
private void btnClickMe_Click(object sender, EventArgs e)
{
this.Cursor = new Cursor("ziyiPicture.cur");
}

  

4.获得鼠标在窗体上的位置 (汗~~~)

没有原理, 这是常识.

代码: 

获得鼠标在窗体上的位置
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
this.labX.Text = e.X.ToString ();
this.labY.Text = e.Y.ToString();
}

 

5.获取鼠标双击时间间隔

 原理: 利用 API 函数 GetDoubleClickTime

 代码:

获取鼠标双击时间间隔
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ziyiMouse5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[System.Runtime.InteropServices.DllImport(
"user32.dll", EntryPoint = "GetDoubleClickTime")]
public extern static int GetDoubleClickTime();
///鼠标双击时间
public string DoubleClickTime_Get()
{
return GetDoubleClickTime().ToString();
}
private void button1_Click(object sender, EventArgs e)
{

MessageBox.Show(DoubleClickTime_Get());
}

private void Form1_Load(object sender, EventArgs e)
{

}



}
}

 

6. 隐藏和显示鼠标

原理: API:  ShowCursor

代码:

隐藏和显示鼠标
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ziyiMouse6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport
       ("user32.dll", EntryPoint = "ShowCursor")]
public extern static bool ShowCursor(bool bShow);
//鼠标隐藏
public void Hide()
{
ShowCursor(
false);
}
//鼠标显示
public void Show()
{
ShowCursor(
true);
}
private void btnHide_Click(object sender, EventArgs e)
{
this.Hide();
}

private void btnShow_Click(object sender, EventArgs e)
{
this.Show();
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

 

三. 无聊篇

1. 获取鼠标键数 

原理: API: GetSystemMetricsg 

代码: 

电脑鼠标键数
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ziyiMouse7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport
(
"user32",EntryPoint="GetSystemMetrics")]
public static extern int GetSystemMetrics(int intcoutn);
public int SM_CMOUSEBUTTONS=43;
private void button1_Click(object sender, EventArgs e)
{
int intCon=GetSystemMetrics(SM_CMOUSEBUTTONS);
MessageBox.Show(
"我电脑鼠标键数为:"+intCon.ToString());

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

 

2. 交换鼠标左右键的功能

原理: API : SwapMouseButton

代码:

交换鼠标左右键功能
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ziyiMouse8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport(
"user32.dll", EntryPoint = "SwapMouseButton")]
public extern static int SwapMouseButton(int bSwap);
//鼠标右键
public void DefaultRightButton()
{
SwapMouseButton(
1);
}
//鼠标左键
public void DefaultLeftButton()
{
SwapMouseButton(
0);
}
//交换鼠标左右键
private void btnChange_Click(object sender, EventArgs e)
{
this.DefaultRightButton();
}
//恢复默认设置
private void btnReset_Click(object sender, EventArgs e)
{
this.DefaultLeftButton();
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

 

3. 记录鼠标行为

原理: 可在MouseDown 事件中判断您到底单击了鼠标的左右中3个键中的哪一个.

代码:

记录鼠标行为
private void textBox2_MouseDown(object sender, MouseEventArgs e)
{
string str=null;
str
=textBox2.Text;
if (e.Button == MouseButtons.Right)
{
str
+="鼠标右键被您按下、";
}
if(e.Button==MouseButtons.Left)
{
str
+="您按下了鼠标左键、";
}
if(e.Button==MouseButtons.Middle)
{ str
+="鼠标中间键按下、";}
textBox2.Text
=str;

}

 

四. 超越篇

1. 禁止使用鼠标左(右)键

直接附代码:

禁用鼠标左键
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ziyiMouse
{
public partial class Form1 : Form, IMessageFilter
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}
public bool PreFilterMessage(ref System.Windows.Forms.Message MyMessage)
{
//不响应鼠标左键消息
if (MyMessage.Msg >= 513 && MyMessage.Msg <= 515)
{
return true;
}
return false;
}
private void button1_Click(object sender, EventArgs e)
{
Application.AddMessageFilter(
this);
MessageBox.Show(
"鼠标左键已经被禁止,请用Tab键执行操作!", "信息提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void button2_Click(object sender, EventArgs e)
{
Application.RemoveMessageFilter(
this);
MessageBox.Show(
"鼠标左键已经被解禁,可以执行操作!", "信息提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void Form1_Load_1(object sender, EventArgs e)
{

}


}
}

 

 

2. 模拟鼠标操作

直接附代码:

模拟鼠标操作
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ziyiMouse9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[System.Runtime.InteropServices.DllImport(
"user32")]
private static extern int mouse_event
       (int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
const int MOUSEEVENTF_MOVE = 0x0001;
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
const int MOUSEEVENTF_LEFTUP = 0x0004;
const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
const int MOUSEEVENTF_RIGHTUP = 0x0010;
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const int MOUSEEVENTF_MIDDLEUP = 0x0040;
const int MOUSEEVENTF_ABSOLUTE = 0x8000;
private void button1_Click(object sender, EventArgs e)
{
mouse_event(MOUSEEVENTF_MOVE,
-20, -20, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN,
-20, -20, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP,
0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN,
0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP,
0, 0, 0, 0);
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

  

3. 鼠标穿透窗体

直接附代码:

鼠标穿透窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ziyiMouse10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const uint WS_EX_LAYERED = 0x80000;
private const int WS_EX_TRANSPARENT = 0x20;
private const int GWL_STYLE = (-16);
private const int GWL_EXSTYLE = (-20);
private const int LWA_ALPHA = 0x2;

[DllImport(
"user32", EntryPoint = "SetWindowLong")]
private static extern uint SetWindowLong(
IntPtr hwnd,
int nIndex,
uint dwNewLong
);

[DllImport(
"user32", EntryPoint = "GetWindowLong")]
private static extern uint GetWindowLong(
IntPtr hwnd,
int nIndex
);

[DllImport(
"user32", EntryPoint = "SetLayeredWindowAttributes")]
private static extern int SetLayeredWindowAttributes(
IntPtr hwnd,
int crKey,
int bAlpha,
int dwFlags
);
private void Form1_Load(object sender, EventArgs e)
{
uint intExTemp = GetWindowLong(
this.Handle, GWL_EXSTYLE);
uint oldGWLEx = SetWindowLong(this.Handle,
GWL_EXSTYLE, WS_EX_TRANSPARENT
| WS_EX_LAYERED);
SetLayeredWindowAttributes(
this.Handle, 0, 100, LWA_ALPHA);
}



}
}

 

 

后记: 这些案例有的是自己平时工作中写的, 有的是园子里不小心看到的, 有的是直接Google的. 汇总与此为大家引用方便.

 

 

 

 

 

标签: 鼠标操作
绿色通道:好文要顶关注我收藏该文与我联系
posted on 2008-09-27 08:20 子逸 阅读(3330) 评论(21) 编辑 收藏
发表评论
2231760
 回复 引用 查看   
#1楼 2008-09-27 08:26 | 齐.net      
很不错
支持一下。

 回复 引用 查看   
#2楼 2008-09-27 08:42 | 小兽      
收藏下 虽然目前用不到 有时间再研究!
 回复 引用 查看   
#3楼 2008-09-27 08:58 | 非空      
收藏
 回复 引用 查看   
#4楼 2008-09-27 09:04 | 二叉树      
收藏,谢谢博主费心了。
 回复 引用   
#5楼 2008-09-27 09:05 | 坐忘山水[未注册用户]
不错,很实用!!!!!11111
 回复 引用 查看   
#6楼 2008-09-27 09:19 | 包建强      
谁能计算出一天内鼠标移动了多少距离?
 回复 引用 查看   
#7楼[楼主] 2008-09-27 09:26 | 子逸      
@包建强
---------------------
好, 这个算法您做出来后加到 上面的 "无聊篇"中...

 回复 引用   
#8楼 2008-09-27 09:41 | 东方队[未注册用户]
不错 顶一下

 回复 引用 查看   
#9楼 2008-09-27 09:54 | 侯垒      
不错,不错,就是点开代码后,页面全乱了。也没有办法看了。
 回复 引用 查看   
#10楼[楼主] 2008-09-27 10:07 | 子逸      
@侯垒
----------------
谢谢您的提醒: 第三个实例代码没有换行,
我改一下.

 回复 引用   
#11楼 2008-09-27 10:08 | fa[未注册用户]
fdf
 回复 引用 查看   
#12楼 2008-09-27 10:14 | chegan      
有意思,
 回复 引用 查看   
#13楼 2008-09-27 10:29 | 逖靖寒      
hehe
 回复 引用 查看   
#14楼 2008-09-27 10:51 | John Rambo      
Very nice.
 回复 引用 查看   
#15楼 2008-09-27 10:58 | 张挚      
很好!喜欢.先收藏了.
 回复 引用 查看   
#16楼 2008-09-27 13:48 | peace      
无条件支持下
 回复 引用 查看   
#17楼 2008-09-27 23:09 | 曲滨*銘龘鶽      
最后一个用
SetLayeredWindowAttributes API,这东西用png 配合一下可以造出
很cool 的窗口,不过wpf 一出,这些东西视乎都多余了;
是 win2000 以上的;

 回复 引用 查看   
#18楼 2008-09-29 11:54 | Doho      
这些小应用挺有意思的哦,直接调用Win32 API挺方便的。
 回复 引用 查看   
#19楼 2008-10-24 16:52 | VisualStudio      
收藏……
 回复 引用   
#20楼 2009-06-03 01:04 | promisinglin[未注册用户]
感谢楼主的努力
 回复 引用 查看   
#21楼 2011-10-31 14:50 | Fly_Click      
收藏了,感谢LZ!
注册用户登录后才能发表评论,请 登录 或 注册,返回博客园首页。
首页博问闪存新闻园子招聘知识库
最新IT新闻:
· Chrome将给老机带来更快的3D绘图性能
· 在线支付创业公司Stripe获红杉资本等1800万美元的投资,公司估值达1亿美元
· 创新工场孵化公司磊友科技今天正式推出首款大型手机HTML5网页游戏《黎明帝国》
· 霍金的伟大与不幸
· Linux为什么成功?因为它的失败是免费的!
» 更多新闻...
最新知识库文章:
· 高级编程语言的发展历程
· 如何学习一门新的编程语言?
· 学习不同编程语言的重要性
· 为什么我喜欢富于表达性的编程语言
· 计算机专业的女生为什么要学编程
» 更多知识库文章...

China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
<2008年9月>
日一二三四五六
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
昵称:子逸
园龄:3年5个月
粉丝:38
关注:2

搜索

 
 

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论
  • 我的标签

最新随笔

  • 1. 急!!!求从字符串中提取形如: div([MC0010000000006],此若干个字符或数字,0) 的正则表达式
  • 2. (转)VS.NET使用
  • 3. (转)SQL 优化原则
  • 4. 工作备忘一: Oracle相关
  • 5. 控件 招谁惹谁了
  • 6. DevXpress控件: 第三篇: 将 父子 关系进行到底--TreeList 控件
  • 7. DevXpress控件: 第二篇--将绘图进行到底: Charter控件(附图)
  • 8. Oracle 游标问题: 急待高人指点
  • 9. DevXpress 控件: 第一篇: 将 Master_Details 关系进行到底--XtraPivotGridControl控件
  • 10. IP地址与子网掩码总结

我的标签

  • 社会(58)
  • 常用算法(25)
  • mootools(17)
  • Ajax(14)
  • 数据结构(12)
  • 搞笑(8)
  • 精华文章(7)
  • 特殊类型窗体制作(6)
  • GridView(5)
  • 验证码(5)
  • 更多

随笔分类

  • .NET(25)
  • Ajax(12)
  • ASP.NET(20)
  • C#(84)
  • CSS
  • DevXpress(4)
  • HTML & XHTML * XML
  • JavaScript(2)
  • mootools(12)
  • Oracle(4)
  • Sqlserver(3)
  • VB.NET
  • 操作系统(1)
  • 计算机体系结构
  • 精华文章(收集,转载,链接)区(9)
  • 嵌入式编程(1)
  • 软件工程(6)
  • 数据结构(12)
  • 水晶报表
  • 算法 * 技巧 * 思想 * 经典(36)
  • 随心所欲(8)
  • 网络相关(1)

积分与排名

  • 积分 - 152695
  • 排名 - 608

阅读排行榜

Copyright ©2012 子逸