什么可能导致 PostMessage 发送的鼠标点击被忽略?

发布于 2024-12-02 07:48:18 字数 1490 浏览 1 评论 0原文

我正在寻找一种将鼠标点击发送到 Windows 上的后台应用程序(即通过句柄)的方法,我用来确认我的代码正在工作的测试窗口接受并处理点击,但我的目标应用程序不接受并处理点击(即使 Spy++显示消息)。

可能是什么原因造成的?有解决方法吗?

这是我正在使用的 C# 代码。

public enum WMessages : int
{
    WM_LBUTTONDOWN = 0x201,
    WM_LBUTTONUP = 0x202,

    WM_KEYDOWN = 0x100,
    WM_KEYUP = 0x101,

    WH_KEYBOARD_LL = 13,
    WH_MOUSE_LL = 14,
}

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern int PostMessage(HandleRef hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

public void SendClick(WMessages type, Point pos)
{
    switch(type)
    {
        case WMessages.WM_LBUTTONDOWN:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        case WMessages.WM_LBUTTONUP:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        default:
            return;
    }
}

SendClick(WMessages.WM_LBUTTONDOWN, Cursor.Position);
SendClick(WMessages,WM_LBUTTONUP, Cursor.Position);

这有可能实现吗?有没有更好的方法来实现这一目标?

注意:当应用程序处于活动状态并且鼠标悬停在正确位置时,上述代码也不起作用。我还在寻找专门将输入发送到后台应用程序的方法,因此 SendInput 和其他方法是不可能的。

谢谢

I was looking for a way to send mouse clicks to a background application on Windows (ie. by handle), The test window I used to confirm my code was working accepts and processes the clicks, but my target application does not (even though Spy++ shows the messages).

What could be causing this? And is there a work-around?

here's the C# code i'm using.

public enum WMessages : int
{
    WM_LBUTTONDOWN = 0x201,
    WM_LBUTTONUP = 0x202,

    WM_KEYDOWN = 0x100,
    WM_KEYUP = 0x101,

    WH_KEYBOARD_LL = 13,
    WH_MOUSE_LL = 14,
}

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern int PostMessage(HandleRef hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

public void SendClick(WMessages type, Point pos)
{
    switch(type)
    {
        case WMessages.WM_LBUTTONDOWN:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        case WMessages.WM_LBUTTONUP:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        default:
            return;
    }
}

SendClick(WMessages.WM_LBUTTONDOWN, Cursor.Position);
SendClick(WMessages,WM_LBUTTONUP, Cursor.Position);

Is this possible to achieve? Is there a better way of acheiving this?

Note: The above code doesn't work when the application is active and the mouse is hovered in the correct location, either. I'm also looking specifically send input to a background application, so SendInput and others are out of the question.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分开我的手 2024-12-09 07:48:18
     public void SendClick(WMessages type, Point pos)
{
    switch(type)
    {
        case WMessages.WM_LBUTTONDOWN:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        case WMessages.WM_LBUTTONUP:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1, // <--(2) but you are telling to do WM_LBUTTONDOWN
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        default:
            return;
    }
}

SendClick(WMessages.WM_LBUTTONDOWN, Cursor.Position);
SendClick(WMessages.WM_LBUTTONUP, Cursor.Position); // <--(1) you are sending WM_LBUTTONUP

所以只要先读(1)然后读(2)你的问题就解决了

     public void SendClick(WMessages type, Point pos)
{
    switch(type)
    {
        case WMessages.WM_LBUTTONDOWN:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        case WMessages.WM_LBUTTONUP:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1, // <--(2) but you are telling to do WM_LBUTTONDOWN
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        default:
            return;
    }
}

SendClick(WMessages.WM_LBUTTONDOWN, Cursor.Position);
SendClick(WMessages.WM_LBUTTONUP, Cursor.Position); // <--(1) you are sending WM_LBUTTONUP

so just read (1) first then (2) and your problem is solved

冷清清 2024-12-09 07:48:18

您是否尝试使用 SendMessage 调用而不是 PostMessage ? SendMessage 立即调用窗口的处理程序。 PostMessage 将消息放入列表中以供稍后处理。

Did you try a SendMessage call, instead of PostMessage? SendMessage immediately calls the handler for the Window. PostMessage puts the message on a list for later processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文