Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:20:55 C#教程: 控制鼠标操作使用实例
C#教程: 控制鼠标操作使用实例|方法
控制鼠标操作控制鼠标操作包括很多种,如限定鼠标指针的移动范围、设置鼠标的左右键、控制鼠标指针的显示和隐藏等。本节中将通过两个具体的示例来介绍有关控制鼠标操作方面的知识。1.限定鼠标指针的移动范围利用API函数ClipCursor和GetWindowRect可以实现限定鼠标移动范围的功能。API函数声明如下:[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);示例控制鼠标移动本示例通过API函数ClipCursor和GetWindowRect实现了限定鼠标指针移动范围的功能。示例运行结果如图1所示。图1 限定鼠标移动的范围单击【控制鼠标移动】按钮,鼠标指针只能在窗体中移动,关键代码如下: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);}完整程序代码如下:★ ★★★★frmMove.cs窗体代码文件完整程序代码★★★★★using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _2_06{public partial class frmMove : Form{public frmMove(){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 frmMove_Load(object sender, EventArgs e){}}}★ ★★★★frmMove.Designer.cs窗体代码文件完整程序代码★★★★★namespace _2_06{partial class frmMove{/// /// 必需的设计器变量。/// private System.ComponentModel.IContainer components = null;/// /// 清理所有正在使用的资源。/// /// 如果应释放托管资源,为 true;否则为 false。protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// /// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。 赞 If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长) QQ:154298438 QQ:417480759
赞