将鼠标事件发送到非活动窗口 [英] Send a mouse event to an inactive window

查看:70
本文介绍了将鼠标事件发送到非活动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,这是来自巴塞罗那的IsabelGarcía,我发现使用PostMessage方法将鼠标事件发送到网络浏览器时遇到了一些问题。



Hello, It's Isabel García from Barcelona and I am finding some problems with PostMessage method to send a mouse event to a web browser.

public void onClick2()
    {
        Process[] notepads = Process.GetProcessesByName("chrome");
        if (notepads.Length == 0) return;
        if (notepads[0] != null)
        {
            UnityEngine.Debug.Log("Encontrado");
            hWnd = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
         
            PostMessage(hWnd, MOUSEEVENTF_RIGHTDOWN, VK_RETURN, 0x001C0001);
            PostMessage(hWnd, MOUSEEVENTF_RIGHTDOWN, VK_DOWN, 0x00500001);

        }

    }





我必须点击的按钮有唯一ID:



The button in which I have to click has a unique id:

<a href="#" id="action-button" title="Share on WhatsApp" class="button button--simple button--primary">Send</a>



我想访问该ID。使用Spy ++我无法访问该按钮。



我尝试了什么:



我使用过SendKeys,但它与Notepad app一起使用。

我使用过Spy ++但没有成功使用chrome网页。

我点击了以下内容:


And I would like to access to that id. Using Spy++ I am not able to access to that button.

What I have tried:

I have used SendKeys but it has worked with Notepad app.
I have used Spy++ but without success with chrome web page.
I have perfom a click doing the following:

private const uint MOUSEEVENTF_MOVE = 0x0001;
    private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const uint MOUSEEVENTF_LEFTUP = 0x0004;
    private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
    private const uint MOUSEEVENTF_RIGHTUP = 0x0010;
    private const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
public void performClick(uint x, uint y)
    {
        //SetCursorPos(x, y);
        mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, x, y, 0, UIntPtr.Zero);
        Thread.Sleep(200);
        mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, x, y, 0, UIntPtr.Zero);
        UnityEngine.Debug.Log("performClick x: " + x  + "y: "+ y);
        //PostMessage(handle, MOUSEEVENTF_LEFTDOWN, 5, 5); https://stackoverflow.com/questions/409886/send-key-strokes-to-games-using-direct-input/50855426#50855426
    }

推荐答案

您可能需要先激活窗口,请参阅此处的文章:用C#向另一个应用程序发送击键 [ ^ ]
You probably need to make the window active first, see article here: Sending Keystrokes to another Application in C#[^]


这篇关于将鼠标事件发送到非活动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆