您的位置:首页 > 编程语言 > C#

C# .NET中如何使用GetCursorPos函数

2011-11-28 17:31 323 查看
标签:

getcursorpos

鼠标

编程

屏幕取词

it

分类: 编程技法
5月22日

用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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐