该示例实现了控制鼠标的坐标,分别用WIndows Api和.Net库自带的命令实现。
APi控制和获取鼠标分别是: GetCursorPos和SetCursorPost。
下面是截图:
using System.Runtime.InteropServices;//
/// <summary>
/// 设置鼠标的坐标
/// </summary>
/// <param name="x">横坐标</param>
/// <param name="y">纵坐标</param>
[DllImport("User32")]
public extern static void SetCursorPos(int x, int y);
/// <summary>
/// 获取鼠标的坐标
/// </summary>
/// <param name="lpPoint">传址参数,坐标point类型</param>
/// <returns>获取成功返回真</returns>
[DllImport("User32")]
public extern static bool GetCursorPos(ref Point lpPoint);
private void button_go_Click(object sender, EventArgs e)
{
SetCursorPos(int.Parse(textBox_x.Text), (int.Parse(textBox_y.Text)));
}
Point p = new Point(1, 1);//定义存放获取坐标的point变量
private void timer1_Tick(object sender, EventArgs e)
{
GetCursorPos(ref p);
label_p.Text = "X:" + p.X + "\r\nY:" + p.Y;
//label_p.Text = "X:" + Cursor.Position.X + "\r\nY:" + Cursor.Position.Y; //用C#自带命令获取
}