All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
GDI+ 绘制折线图分析网站流量
delphi在系统菜单中添加菜单项实例
两行代码搞定多级关联搜索_[PHP教程]
asp.net的web打印功能简单实现_[Asp.Net教程]
C#,如何获取图片长宽_[Asp.Net教程]
用javascript操纵GridView中CheckBox的两个常用技巧_[Asp.Net教程]
Windows XP&2003 IIS安装PHP&MySQL环境完全教程_[PHP教程]
Delphi使用ODBC连接SQServer 2000数据库
Email地址加密javascript版_JavaScript技术_编程技术
利用C#实现分布式数据库查询_[Asp.Net教程]
Reading number is top 10 pictures
A beautiful girl to bud1
两张抽象画
Household design comfortable contracted
NeedWallpaper4
去瑜伽会所面试的经过
中国处女图鉴1
Absolutely shocked. National geographic 50 animal photographys10
身材野火台灣美女2
你的钱干净吗?
Beauty ZhiHuiLin1
Download software ranking
XML+Web+Service开发教程
Eclipse-CALMSANNY (second edition)
Macromedia Dreamweaver 8
都市狐狸姑娘传
电车之狼R
linux高级编程
The Bermuda triangle1
网络管理员第三版
jdk1.6 for windows
Ashlynn Video4
qq published in(发表于) 2014/7/11 9:20:55 Edit(编辑)
C#教程: 控制鼠标操作使用实例

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 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.