Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式在另一个窗口中单击鼠标_C#_Windows_Click_Mouse_Message - Fatal编程技术网

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

C# 以编程方式在另一个窗口中单击鼠标,c#,windows,click,mouse,message,C#,Windows,Click,Mouse,Message,是否可以通过编程方式单击另一个窗口中的某个位置,而不将鼠标移动到该位置,即使该窗口不在顶部?我想向另一个窗口发送一种消息,模拟鼠标单击某个位置 我试图通过PostMessage实现这一点: PostMessage(WindowHandle, 0x201, IntPtr.Zero, CreateLParam(300,300)); PostMessage(WindowHandle, 0x202, IntPtr.Zero, CreateLParam(300,300)); [return: Marsh

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

我试图通过PostMessage实现这一点:

PostMessage(WindowHandle, 0x201, IntPtr.Zero, CreateLParam(300,300));
PostMessage(WindowHandle, 0x202, IntPtr.Zero, CreateLParam(300,300));
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr WindowHandle, int Msg, IntPtr wParam, IntPtr lParam);
我以这种方式创建了CreateLParam函数:

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

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

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

调用方法ClickOnPoint,这是表单单击事件中的一个示例,因此
这个句柄是表单句柄,请注意,这些是窗口上的客户端坐标,当句柄被发送时,您可以轻松地更改它并发送屏幕坐标,在这种情况下,您不需要下面的句柄或客户端到屏幕调用

ClickOnPoint(this.Handle, new Point(375, 340));
更新:现在使用SendInput,tnx Tom

顺便说一句,我只使用了这个示例所需的声明,还有一个很好的库:


在过去,我发现了一种向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
    }
}
我创建它是为了在特定的时间间隔内自动点击“BlueStacks”应用程序

对于
FindWindow
wParam
lParam
等,您可以随时询问我如何操作!这不太难:);)
希望有帮助!:)

我无法添加评论:D

为我工作:

[DllImport("User32.dll")]
    public static extern Int32 SendMessage(
    int hWnd,               
    int Msg,                
    int wParam,            
    IntPtr lParam);
并在lParam中组合coord,如下所示:

private static IntPtr CreateLParam(int LoWord, int HiWord)
    {
        return (IntPtr)((HiWord << 16) | (LoWord & 0xffff));
    }
0x00040156-窗口句柄

我在找一个窗户把手用间谍++ 每次都是新的,所以最好使用FindWindow

附言

即使窗口不在顶部,应用程序窗口的屏幕截图


我使用这个解决方案。很好。

这是您控制的另一个窗口吗?如果不是,这似乎是一个非常奇怪的请求。你想点击什么程序?有些程序(主要是游戏)有机制来处理您试图做的事情,并忽略它。在这种情况下,您最好的机会是使用WinApi使游戏处于最顶端,移动鼠标,单击,将鼠标移回,将游戏移回上一个z顺序。此外,在执行
之前,请尝试将
HiWord
转换为
uint
,谢谢,这非常有效。我还补充说,当点击时窗口会激活。没有SetCursorPos或不移动鼠标的其他方法吗???我在搜索的每一个地方都看到几乎相同的答案…请注意,您可以使用Cursor.Position而不是GetCursorPos和SetCursorPos。这也消除了对点结构的需要。如果目标窗口不在顶部,这将不起作用。因为它点击的是屏幕而不是窗口。在这个问题中,有人明确地问到“即使窗口不在顶部”,鼠标事件已经有很长一段时间不受欢迎了,您现在应该使用SendInput。看起来,鼠标单击是发布的,而不是发送的。PostMessage是一个更好的解决方案。至少Spy++为我声称点击已经发布。来这里试图将点击发送到Bluestack,并没有失望!如何为点击提供坐标?我需要在某个位置单击,而不仅仅是在未知位置单击。@Kosmos使用这个:var w=(y
[DllImport("User32.dll")]
    public static extern Int32 SendMessage(
    int hWnd,               
    int Msg,                
    int wParam,            
    IntPtr lParam);
private static IntPtr CreateLParam(int LoWord, int HiWord)
    {
        return (IntPtr)((HiWord << 16) | (LoWord & 0xffff));
    }
Clicktoapp.SendMessage(0x00040156, Clicktoapp.WM_LBUTTONDOWN, 0x00000001, CreateLParam(150,150));
        Clicktoapp.SendMessage(0x00040156, Clicktoapp.WM_LBUTTONUP, 0x00000000, CreateLParam(150, 150));