c# - 以编程方式在另一个窗口中单击鼠标

标签 c# windows click mouse message

是否可以在不将鼠标移动到该位置的情况下以编程方式单击另一个窗口中的位置,即使该窗口不在顶部?我想向另一个窗口发送一种消息来模拟鼠标点击某个位置。

我试图用 PostMessage 来完成这个:

PostMessage(WindowHandle, 0x201, IntPtr.Zero, CreateLParam(300,300));
PostMessage(WindowHandle, 0x202, IntPtr.Zero, CreateLParam(300,300));

我是这样创建 CreateLParam 函数的:

private static IntPtr CreateLParam(int LoWord, int HiWord)
{
     return (IntPtr)((HiWord << 16) | (LoWord & 0xffff));
}

问题是窗口在他的位置上被锁定了。我认为我的应用程序点击了 (1,1) 坐标。有人可以帮我解决这个问题吗?

编辑: 这是 PostMessage:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr WindowHandle, int Msg, IntPtr wParam, IntPtr lParam);

而0x201和0x202分别是WM_LBUTTONDOWN和WM_LBUTTONUP。

最佳答案

您不能通过发送消息来做到这一点,而是使用 SendInput Windows API。

调用方法 ClickOnPoint,这是一个来自表单点击事件的例子,所以 this.handle 是表单句柄,注意这些是窗口句柄发送时的客户端坐标,你可以很容易地改变这个和发送屏幕坐标,在这种情况下,您不需要下面的 handle 或 ClientToScreen 调用。

ClickOnPoint(this.Handle, new Point(375, 340));

更新:现在使用 SendInput,tnx Tom。

顺便说一句。我只使用了这个示例所需的声明,还有一个不错的库:Windows Input Simulator (C# SendInput Wrapper - Simulate Keyboard and Mouse)

  public class ClickOnPointTool
  {

    [DllImport("user32.dll")]
    static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);

    [DllImport("user32.dll")]
    internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs,  int cbSize);

#pragma warning disable 649
    internal struct INPUT
    {
      public UInt32 Type;
      public MOUSEKEYBDHARDWAREINPUT Data;
    }

    [StructLayout(LayoutKind.Explicit)]
    internal struct MOUSEKEYBDHARDWAREINPUT
    {
      [FieldOffset(0)]
      public MOUSEINPUT Mouse;
    }

    internal struct MOUSEINPUT
    {
      public Int32 X;
      public Int32 Y;
      public UInt32 MouseData;
      public UInt32 Flags;
      public UInt32 Time;
      public IntPtr ExtraInfo;
    }

#pragma warning restore 649


    public static void ClickOnPoint(IntPtr wndHandle , Point clientPoint)
    {
      var oldPos = Cursor.Position;

      /// get screen coordinates
      ClientToScreen(wndHandle, ref clientPoint);

      /// set cursor on coords, and press mouse
      Cursor.Position = new Point(clientPoint.X, clientPoint.Y);

      var inputMouseDown = new INPUT();
      inputMouseDown.Type = 0; /// input type mouse
      inputMouseDown.Data.Mouse.Flags = 0x0002; /// left button down

      var inputMouseUp = new INPUT();
      inputMouseUp.Type = 0; /// input type mouse
      inputMouseUp.Data.Mouse.Flags = 0x0004; /// left button up

      var inputs = new INPUT[] { inputMouseDown, inputMouseUp };
      SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));

      /// return mouse 
      Cursor.Position = oldPos;
    }

  }

关于c# - 以编程方式在另一个窗口中单击鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10355286/

相关文章:

c# - 什么进入模型/ View 模型?

c# - 如何实现用更具体的类型覆盖基础成员的效果?

c# - 解析 "Certificate Policies"扩展

c# - 如何从 ASP.NET MVC 中的 404 Not Found 错误中删除查询字符串

java - 如何给jvm设置默认参数?

javascript - asp.net按钮-JS点击不起作用

css - 如何删除或更改默认按钮样式?

python 3.6.1 需要安装 windows 7 service pack 1

c++ - 将 Qt 与自定义 MinGW 结合使用

Javascript 按类点击元素