gpt4 book ai didi

c# - 鼠标点击事件与显示缩放无关

转载 作者:太空宇宙 更新时间:2023-11-03 15:55:19 29 4
gpt4 key购买 nike

我正在尝试将鼠标移动到一个位置 (x, y),然后在控制台 C# 应用程序中使用以下代码单击它

[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
private const int MOUSEEVENTF_MOVE = 0x0001;
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
private const int MOUSEEVENTF_RIGHTUP = 0x0010;
private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
public static void Move(int xDelta, int yDelta)
{
mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0);
}
public static void MoveTo(int x, int y)
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0);
}
public static void LeftClick()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, Cursor.Position.X, Cursor.Position.Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
}
/* public static void LeftClick(int x, int y)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
*/
public static void LeftDown()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, Cursor.Position.X, Cursor.Position.Y, 0, 0);
}

public static void LeftUp()
{
mouse_event(MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
}

public static void RightClick()
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, Cursor.Position.X, Cursor.Position.Y, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
}

public static void RightDown()
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, Cursor.Position.X, Cursor.Position.Y, 0, 0);
}

public static void RightUp()
{
mouse_event(MOUSEEVENTF_RIGHTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
}

click_position()
{


Cursor.Position = new Point(xwidth, yheight);
LeftClick
}

如果显示比例为 100%,则工作正常,但如果显示比例不是 100%,则实际鼠标点击与鼠标坐标不同。请让我知道任何点击屏幕的 c# 方式,无论控制台 c# 应用程序中的显示缩放如何

最佳答案

如果这是一个 Windows 桌面应用程序,那么您不仅希望在鼠标位置上执行单击,还希望在两个轴上用您的窗口当前位置减去它

例如:

public static void LeftClick() {
Point mousePosOnScreen = Cursor.Position - this.Location;
mouse_event(MOUSEEVENTF_LEFTDOWN, mousePosOnScreen.X, mousePosOnScreen.Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, mousePosOnScreen.X, mousePosOnScreen.Y, 0, 0);
}

只是快速解释一下你做错了什么,当检查 Cursor.Position 时将返回显示器上的指针位置而不是窗口上的位置。因此,通过减去窗口位置,您将得到鼠标指针在窗口上的位置。

关于c# - 鼠标点击事件与显示缩放无关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23903673/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com