零星的鼠标位置?

时间:2017-05-19 21:59:24

标签: c#

我正在尝试制作一个缩小屏幕图像(1024x576)的程序,无论用户点击图片框,他们的鼠标都会在实际屏幕上移动。我遇到的问题是鼠标位置似乎是零星的。我使用了反复试验来让鼠标移动到"停止"当我点击屏幕截图中的按钮时,Visual Studio中的按钮有效。但是,我尝试点击滚动条,它没有点击正确的位置。不应该与每个轴的差异是静态的吗?我不明白为什么这只是半工作。

private void Form1_Load(object sender, EventArgs e)
{
    Thread.Sleep(1000);

    Graphics graphics = Graphics.FromImage(rawScreenBMP);
    graphics.CopyFromScreen(0, 0, 0, 0, rawScreenBMP.Size);
    graphics = Graphics.FromImage(downscaledBMP);
    graphics.DrawImage(rawScreenBMP, new RectangleF(0, 0, downscaledBMP.Width, downscaledBMP.Height));

    pictureBox1.Image = downscaledBMP;
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    RemoteClick(e.X, e.Y);
    Console.WriteLine(e.X.ToString() + " " + e.Y.ToString());
}

public static void RemoteClick(int locx, int locy)
{
    Console.WriteLine((locx + 390).ToString() + " " + (locy+30).ToString());
    SetCursorPos(locx + 390, locy + 30);
    //adding 390 for X and 30 for Y seemed to get the cursor in the right position when I clicked the debug "stop" button. But it clicks in the incorrect spot elsewhere
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    Console.WriteLine(Cursor.Position);
}

1 个答案:

答案 0 :(得分:1)

根据我的评论

,我建议使用比率而不是静态数字,因为它们不起作用

相反,如果屏幕是1920 * 1080,例如

double ratiox = (1920 / 1024);
double ratioy = (1080 / 576);
SetCursorPos(locx * ratiox, locy * ratioy);

如果代码不准确,请在手机上写字,但这是一个如何计算比率的示例