转换鼠标位置
本文关键字:位置 鼠标 转换 | 更新日期: 2023-09-27 18:06:07
我需要一些鼠标位置和屏幕分辨率的帮助。
我有两个应用程序在两台不同的机器上运行:
application1(分辨率:1920 x 1200)捕获鼠标位置,然后将位置值发送给application2。
application2(分辨率:1280 x 800)接收并根据这些值设置游标位置。
这工作得很好,我的问题是,application1与application2的屏幕分辨率不同,因此从application1发送的鼠标位置不能转换为application2上的屏幕分辨率和光标位置。
有人知道如何将这些光标位置(X, Y)值转换为正确的值吗?当然,所有这些都假设application2表单窗口完全最大化,否则将不得不根据表单窗口大小进行类似的值转换。
这是application1捕获鼠标位置的方式:
Point mouseLocation;
public Form1()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
mouseLocation = e.Location;
// now we're send the "mouseLocation" values to the application2
}
,下面是application2根据接收到的值设置光标位置的方式:
public Form1()
{
InitializeComponent();
// we bring the position values
int x_value = int.Parse(position[0].ToString());
int y_value = int.Parse(position[1].ToString());
Cursor.Position = new Point(x_value, y_value);
}
您可以像这样编写一个简单的助手方法:
private static Point Translate(Point point, Size from, Size to)
{
return new Point((point.X * to.Width) / from.Width, (point.Y * to.Height) / from.Height);
}
private static void Main(string[] args)
{
Size fromResolution = new Size(1920, 1200);//From resolution
Size toResolution = new Size(1280, 800);//To resolution
Console.WriteLine(Translate(new Point(100, 100), fromResolution, toResolution));
//Prints 66,66
}