C# 获取和设置鼠标坐标运行界面:
代码:
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;//
namespace mouseTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
///<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);
Point p = new Point(1, 1);//定义存放获取坐标的point变量
private void button_go_Click(object sender, EventArgs e)
{
timer1.Start();
SetCursorPos(int.Parse(textBox_x.Text), (int.Parse(textBox_y.Text)));
}
private void timer1_Tick_1(object sender, EventArgs e)
{
GetCursorPos(ref p);
label_p.Text = "X:" + p.X + "\r\nY:" + p.Y;
if (Convert.ToInt16(p.X) > 560 && Convert.ToInt16(p.X) < 680 && Convert.ToInt16(p.Y) > 350 && Convert.ToInt16(p.Y) < 460)
pictureBox1.Visible = true;
else
pictureBox1.Visible = false;
}
}
}
源代码下载地址:/s/1jG9mqY6。