以编程方式鼠标单击另一个窗口

Dag*_*gob 22 c# windows mouse message click

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

我试图用PostMessage完成这个:

PostMessage(WindowHandle, 0x201, IntPtr.Zero, CreateLParam(300,300));
PostMessage(WindowHandle, 0x202, IntPtr.Zero, CreateLParam(300,300));
Run Code Online (Sandbox Code Playgroud)

我用这种方式创建了CreateLParam函数:

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

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

编辑:这是PostMessage:

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

0x201和0x202分别是WM_LBUTTONDOWN和WM_LBUTTONUP.

Ant*_*ula 30

您不能通过发送消息来执行此操作,而是使用SendInput Windows API.

调用方法ClickOnPoint,这是一个表单点击事件的例子,所以this.handle是表单句柄,注意这些是窗口女巫句柄发送的客户端坐标,你可以轻松改变这个并发送屏幕坐标,在这种情况下你不需要下面的句柄或ClientToScreen调用.

ClickOnPoint(this.Handle, new Point(375, 340));
Run Code Online (Sandbox Code Playgroud)

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

顺便说一句.我只使用了此示例所需的声明,因为还有一个很好的库:Windows输入模拟器(C#SendInput Wrapper - 模拟键盘和鼠标)

  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;
    }

  }
Run Code Online (Sandbox Code Playgroud)

  • 如果目标窗口不在顶部,则无效.因为它点击屏幕而不是窗口.在这个问题中,它被明确询问"即使窗口不在顶部" (11认同)
  • 没有SetCursorPos或没有移动鼠标没有另一种方法??? 在我搜索的任何地方,我都看到几乎相同的非答案...... (4认同)
  • 请注意,您可以使用Cursor.Position而不是GetCursorPos和SetCursorPos.这也消除了对POINT结构的需要. (2认同)
  • mouse_event已经被弃用很久了,你现在应该使用SendInput. (2认同)

小智 17

我在过去发现了一种向Windows Media Player发送消息的方法, 所以我用它来模拟我想要的应用程序中的点击!

使用此类(下面的代码)来查找窗口并发送您想要的消息!

using System;
using System.Runtime.InteropServices;

namespace Mouse_Click_Simulator
{
    /// <summary>
    /// Summary description for Win32.
    /// </summary>
    public class Win32
    {
        // The WM_COMMAND message is sent when the user selects a command item from 
        // a menu, when a control sends a notification message to its parent window, 
        // or when an accelerator keystroke is translated.
        public const int WM_KEYDOWN = 0x100;
        public const int WM_KEYUP = 0x101;
        public const int WM_COMMAND = 0x111;
        public const int WM_LBUTTONDOWN = 0x201;
        public const int WM_LBUTTONUP = 0x202;
        public const int WM_LBUTTONDBLCLK = 0x203;
        public const int WM_RBUTTONDOWN = 0x204;
        public const int WM_RBUTTONUP = 0x205;
        public const int WM_RBUTTONDBLCLK = 0x206;

        // The FindWindow function retrieves a handle to the top-level window whose
        // class name and window name match the specified strings.
        // This function does not search child windows.
        // This function does not perform a case-sensitive search.
        [DllImport("User32.dll")]
        public static extern int FindWindow(string strClassName, string strWindowName);

        // The FindWindowEx function retrieves a handle to a window whose class name 
        // and window name match the specified strings.
        // The function searches child windows, beginning with the one following the
        // specified child window.
        // This function does not perform a case-sensitive search.
        [DllImport("User32.dll")]
        public static extern int FindWindowEx(
            int hwndParent, 
            int hwndChildAfter, 
            string strClassName, 
            string strWindowName);


        // The SendMessage function sends the specified message to a window or windows. 
        // It calls the window procedure for the specified window and does not return
        // until the window procedure has processed the message. 
        [DllImport("User32.dll")]
        public static extern Int32 SendMessage(
            int hWnd,               // handle to destination window
            int Msg,                // message
            int wParam,             // first message parameter
            [MarshalAs(UnmanagedType.LPStr)] string lParam); // second message parameter

        [DllImport("User32.dll")]
        public static extern Int32 SendMessage(
            int hWnd,               // handle to destination window
            int Msg,                // message
            int wParam,             // first message parameter
            int lParam);            // second message parameter
    }
}
Run Code Online (Sandbox Code Playgroud)

例如:

 Win32.SendMessage(iHandle, Win32.WM_LBUTTONDOWN, 0x00000001, 0x1E5025B);
Run Code Online (Sandbox Code Playgroud)

是我创建的应用程序源代码,可以在特定的时间间隔内自动点击"BlueStacks"应用程序!

对于FindWindow,wParam,lParam等你可以随意问我该怎么办呢!这不是太难:);)希望它有所帮助!:)

  • @Kosmos使用:var w =(y << 16)| X; Win32.SendMessage(iHandle,Win32.WM_LBUTTONDOWN,0x00000001,w); (4认同)
  • 如何为点击提供坐标?我需要点击某个位置,而不是单击未知的位置. (2认同)