鼠标单击事件ir对应于显示比例 [英] mouse click event ir respecive of display scaling
问题描述
我试图将鼠标移至(x,y)位置,然后在控制台c#应用程序中使用以下代码单击它
I am trying to move the mouse to a position(x, y), and click on it with the following code in a console c# application
[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#应用程序中的显示比例如何
if the display scaling is at 100% it is working fine but if the display scaling is other than 100%, actual mouse click is different from the mouse co-ordinates. Please let me know any c# way to click on screen irrespective of display scaling in a console c# application
推荐答案
如果这是Windows桌面应用程序,那么您不仅希望在鼠标位置上单击,还要在两个轴上的窗口当前位置上减去它.
If this is a windows desktop application then you would want to perform the click not on mouseposition only but subtract it with your window current position on both axis
例如:
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会在显示器上而不是在窗口上返回Pointer位置.因此,通过减去窗口位置,您将最终获得鼠标指针在窗口上的位置.
just a quick explanation of what you were doing wrong, when checking Cursor.Position will return the Pointer position on your monitor rather then on window. So by subtracting the window position you will end up with the position of the mouse pointer on your window.
这篇关于鼠标单击事件ir对应于显示比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!