All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
C#教程: 控制鼠标操作使用实例

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/11 9:20:55 Browse times: 315 Comment times: 0

C#教程: 控制鼠标操作使用实例


Head photo

Go homepage
Upload pictures
Write articles

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 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.