鼠标重要性不必多说, 我们这些"小毛孩"可能不敢想象没有鼠标时的计算机是如何操作的, 我承认鼠标不是不可替代, 但也已不可或缺.
大多鼠标编程都与 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的. 汇总与此为大家引用方便.