C# 根据用户正在运行的分辨率转换分辨率

C# 根据用户正在运行的分辨率转换分辨率,c#,C#,我正在制作一个程序,它可以让游戏的输入速度更快。因为我的工具现在只适用于1920x1080,我想让它适用于多种分辨率。我现在的1920x1080就是这样的 SetCursorPos(105, 640); System.Threading.Thread.Sleep(30); sim.Mouse.LeftButtonClick(); System.Threading.Thread.Slee

我正在制作一个程序,它可以让游戏的输入速度更快。因为我的工具现在只适用于1920x1080,我想让它适用于多种分辨率。我现在的1920x1080就是这样的

              SetCursorPos(105, 640);
              System.Threading.Thread.Sleep(30);
              sim.Mouse.LeftButtonClick();
              System.Threading.Thread.Sleep(30);
              SetCursorPos(274, 547);
              System.Threading.Thread.Sleep(30);
              sim.Mouse.LeftButtonClick();
              System.Threading.Thread.Sleep(1560);
              sim.Keyboard.KeyPress(VirtualKeyCode.VK_T);
              System.Threading.Thread.Sleep(50);
              SetCursorPos(274, 547);
              sim.Mouse.LeftButtonClick();
              System.Threading.Thread.Sleep(1610);
              SetCursorPos(274, 547);
              sim.Mouse.LeftButtonClick();
              System.Threading.Thread.Sleep(1610);
              SetCursorPos(274, 547);
              sim.Mouse.LeftButtonClick();
              SetCursorPos(960, 540);

我有点想让程序检测实际屏幕分辨率,并将像素位置从1920x1080转换为所需位置。

理论上……将x、y坐标存储为十进制数字,表示原始分辨率的百分比

例如,你的第一点是105640。作为一个百分点,将x坐标除以1920,将y坐标除以1080,得到0.0546875,0.5925925925925926。这可以使用结构来存储

现在,您可以使用这些十进制百分比数字,通过简单地将它们乘以屏幕的宽度/高度,在任何分辨率下获得所需的等效点

您可以使用以下方法获得当前屏幕分辨率:

您需要的是缩放

您已为1920x1080的固定结果编码。即1920像素宽和1080像素高

如果需要缩放此比例,可以获得当前屏幕结果,然后计算比率

假设分辨率为640x480。然后用以下公式计算X或宽度比:

640/1920=0.3333

以及Y或高度比,其中:

480/1080=0.4444

要缩放,现在将宽度和高度乘以相应的比例:

SetCursorPos105*0.3333640*0.4444

在代码中,它看起来像:

int currentX = SystemParameters.PrimaryScreenHeight;
int currentY = SystemParameters.PrimaryScreenWidth;

var xScale = currentX / 1920;
var yScale = currenty / 1080;

SetCursorPos(105 * xScale, 640 * yScale);

我知道SystemParameters是用于WPF的Windows窗体有好的方法吗?你可以按照@Idle\u Mind的建议使用。过去几周我不在家。我试图将其应用到我的代码中,当我使用该操作时,它会将光标移动到第二个屏幕的右下角。也许我犯了个错误?
int currentX = SystemParameters.PrimaryScreenHeight;
int currentY = SystemParameters.PrimaryScreenWidth;

var xScale = currentX / 1920;
var yScale = currenty / 1080;

SetCursorPos(105 * xScale, 640 * yScale);