【问题标题】:How do you set a cursor's position in a different window/application?如何在不同的窗口/应用程序中设置光标的位置?
【发布时间】:2019-07-30 15:07:22
【问题描述】:

我正在尝试将光标位置设置为不同窗口中的一组特定坐标,但除非我移动我拥有的物理鼠标,否则鼠标永远不会在该窗口上刷新。

这适用于将用户光标移动到不同窗口中的特定位置,并在逻辑表达式返回 true 后单击的程序。

int x = 0;
int y = 0;
Cursor.Position = new Point(x, y);

我希望光标实际在窗口内移动,而不仅仅是在 form1 中。 代码会将光标移动到该位置,但不会在我当前所在的窗口上移动。

【问题讨论】:

  • 移动光标几乎总是一个非常糟糕的主意。
  • 不需要先在需要的窗口上设置输入焦点吗?
  • 问题在于它只是被隐藏或似乎不在窗口中。只有将鼠标移动一个像素或更多像素才会显示它的新位置。

标签: c# winforms position window cursor


【解决方案1】:
Point cursorPos = Cursor.Position;
  cursorPos.X = control.PointToScreen(coordinate).X;
  Cursor.Position = cursorPos;

【讨论】:

  • 它给了我两个错误,第一个控件未定义,然后无法将 int 转换为 system.drawing,point。
  • 请尝试详细说明您的答案并解释您在做什么。仅代码回答被视为低质量回答。
【解决方案2】:

发送 RAW 输入数据以使用鼠标。一些应用程序读取原始鼠标笔划,而其他应用程序读取虚拟鼠标笔划。

int to_x = 500;
int to_y = 300;
int screenWidth = InternalGetSystemMetrics(0);
screenHeight = InternalGetSystemMetrics(1);
// Mickey X coordinate
int mic_x = (int)System.Math.Round(to_x * 65536.0 / screenWidth);
// Mickey Y coordinate
int mic_y = (int)System.Math.Round(to_y * 65536.0 / screenHeight);
// 0x0001 | 0x8000: Move + Absolute position
Mouse_Event(0x0001 | 0x8000, mic_x, mic_y, 0, 0);
// 0x0002: Left button down
Mouse_Event(0x0002, mic_x, mic_y, 0, 0);
// 0x0004: Left button up
Mouse_Event(0x0004, mic_x, mic_y, 0, 0);```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    相关资源
    最近更新 更多