C# .NET中如何使用GetCursorPos函数
2011-11-28 17:31
323 查看
标签:getcursorpos鼠标编程屏幕取词it | 分类: 编程技法 |
用Mouse_event()来控制鼠标操作
在自动化测试的开发中,有一些控件的ID是很难找到的,所以有些时候,我们直接设置鼠标的位置,然后是用click事件,会收到很好的效果。在Windows API中有个mouse_event函数为我们准备好了这一切。这个函数在user32.dll这个库文件里面。我们可以在C:/WINDOWS/system32(XP系统)这个目录下找到这个文件,他是系统自带的。 我们以C#直接调用这个文件中的API为例子来说下怎么进行鼠标操作,首先在我们C#中声明引用,如果是一个基于From的程序,这个声明的位置写在你的From class就可以了
[System.Runtime.InteropServices.DllImport("user32")]
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
参数 意义
dwFlags Long,下表中标志之一或它们的组合
dx,dy Long,根据MOUSEEVENTF_ABSOLUTE标志,指定x,y方向的绝对位置或相对位置
cButtons Long,没有使用
dwExtraInfo Long,没有使用
dwFlags常数 意义
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; 标示是否采用绝对坐标
程序中我们直接调用mouse_event函数就可以了
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0);
1、这里是鼠标左键按下和松开两个事件的组合即一次单击:
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
2、模拟鼠标右键单击事件:
mouse_event (MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 )
3、两次连续的鼠标左键单击事件 构成一次鼠标双击事件:
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
4、使用绝对坐标
MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0
需要说明的是,如果没有使用MOUSEEVENTF_ABSOLUTE,函数默认的是相对于鼠标当前位置的点,如果dx,和dy,用0,0表示,这函数认为是当前鼠标所在的点。5、直接设定绝对坐标并单击
mouse_event(MOUSEEVENTF_LEFTDOWN, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
其中X,Y分别是你要点击的点的横坐标和纵坐标
C# .NET中如何使用GetCursorPos函数 例程
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 CursorPosition
{
public partial class frmMain : Form
{
// We need to use unmanaged code
[DllImport("user32.dll")]
// GetCursorPos() makes everything possible
static extern bool GetCursorPos(ref Point lpPoint);
// Variable we will need to count the traveled pixels
static protected long totalPixels = 0;
static protected int currX;
static protected int currY;
static protected int diffX;
static protected int diffY;
public frmMain()
{
InitializeComponent();
}
private void tmrDef_Tick(object sender, EventArgs e)
{
// New point that will be updated by the function with the current coordinates
Point defPnt = new Point();
// Call the function and pass the Point, defPnt
GetCursorPos(ref defPnt);
// Now after calling the function, defPnt contains the coordinates which we can read
lblCoordX.Text = "X = " + defPnt.X.ToString();
lblCoordY.Text = "Y = " + defPnt.Y.ToString();
// If the cursor has moved at all
if (diffX != defPnt.X | diffY != defPnt.Y)
{
// Calculate the distance of movement (in both vertical and horizontal movement)
diffX = (defPnt.X - currX);
diffY = (defPnt.Y - currY);
// The difference will be negative if the cursor was moved left or up
// and if it is so, make the number positive
if (diffX < 0)
{
diffX *= -1;
}
if (diffY < 0)
{
diffY *= -1;
}
// Add to the "pixels traveled" counter
totalPixels += diffX + diffY;
// And display inside a label
lblTravel.Text = "You have traveled " + totalPixels + " pixels";
}
// We need this to see the difference of pixels between two mouse movements
currX = defPnt.X;
currY = defPnt.Y;
}
private void btnReset_Click(object sender, EventArgs e)
{
totalPixels = 0;
}
}
}
相关文章推荐
- C# .NET中如何使用GetCursorPos函数
- C# .NET中如何使用GetCursorPos函数 例程
- C# .NET中如何使用GetCursorPos函数 例程(转载)
- [引]如何藉由使用 Visual C# . NET 處理 Word 中的事件
- 如何使用 Visual C# 2005 或 Visual C# .NET 向 Excel 工作簿传输数据
- 如何使用 WebBrowser 控件在 Visual C# 2005 或 Visual C# .NET 中打开 Office 文档
- 如何使用 Visual C# 2005 或 Visual C# .NET 向 Excel 工作簿传输数据
- 如何使用 Visual C# 2005 或 Visual C# .NET 向 Excel 工作簿传输数据
- 关于asp.net(c#),webconfig中如何定义一个字符串让所有页面共同使用?
- 微信公众号入门学习2_使用C#,ASP.NET APIController如何被动回复用户消息
- 如何高效使用SQLite .net (C#)
- 如何在ASP.NET 网站项目中使用C# 6?
- 如何使用 Visual C# .NET 从 DataSet 对象更新数据库
- 如何使用<Beginning ASP.NET 3.5 in C# and VB>书中的示例代码
- C#.NET常见问题(FAQ)-delegate委托链如何使用
- C#.NET常见问题(FAQ)-如何使用变量动态添加控件
- 如何在C#.net中自定义和使用特性
- asp.net c#后台页代码 如何使用confirm这样的函数,并获得其bool返回值?
- 如何使用ADO.Net助手生成C#数据库代码
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作