模拟鼠标移动/单击/按下非活动应用程序中的按钮

时间:2022-11-03 18:02:35

I know how to simulate mouse and keyboard events, but they act as if the user did them, so they will affect the window that is active. What I need is to simulate one of those inputs, but in a Window that is not active.

我知道如何模拟鼠标和键盘事件,但它们就像用户那样做,因此它们会影响活动的窗口。我需要的是模拟其中一个输入,但在一个不活动的窗口中。

I'm not saying that it is minimized, imagine for example, you have msPaint, and notepad. Notepad is in front of paint. And you want to simulate mouse clicks in certain coordinates of the paint window, but without setting it active, making it possible for the user to keep using notepad which is in fron of paint.

我并不是说它被最小化了,想象一下,例如,你有msPaint和记事本。记事本在油漆面前。并且您希望在绘画窗口的某些坐标中模拟鼠标点击,但不将其设置为活动状态,从而使用户可以继续使用油漆边缘的记事本。

Is this possible at all? Thanks!

这有可能吗?谢谢!

I've tried this:

我试过这个:

            const int WM_KEYDOWN = 0x100;
        Thread.Sleep(5000);
        Process x = Process.Start(@"C:\WINDOWS\NOTEPAD.EXE");
        PInvokes.PlatformInvokeUSER32.SendMessage(x.MainWindowHandle, WM_KEYDOWN, ((int)Keys.W), 0);

but it doesn't work =( Doesn't do anything :(

但它不起作用=(什么都不做:(

4 个解决方案

#1


2  

You can try the UI automation API. It supports also legacy applications.

您可以尝试UI自动化API。它还支持遗留应用程序。

#2


2  

Maybe try the PostMessage function instead:

也许尝试使用PostMessage函数:

    [DllImport("user32.dll", CharSet=CharSet.Auto)] 
    private static extern int PostMessage(
            int hWnd, int msg, int wParam, IntPtr lParam);

#3


1  

Depending on the target app, there could be lots of issues. Some apps trigger on WM_KEYDOWN or WM_KEYUP instead of WM_CHAR. Also, the key might already be down and is being ignored. For targets like webbrowsers, you need to get the right window handle. Winspector can get that for you.

根据目标应用程序,可能会出现很多问题。某些应用程序在WM_KEYDOWN或WM_KEYUP而不是WM_CHAR上触发。此外,密钥可能已经关闭并被忽略。对于像webbrowsers这样的目标,你需要获得正确的窗口句柄。 Winspector可以帮你。

#4


0  

Your code wont work because SendMessage requires that the window you're sending to be active. Try PostMessage.

您的代码无法正常工作,因为SendMessage要求您发送的窗口处于活动状态。试试PostMessage。

#1


2  

You can try the UI automation API. It supports also legacy applications.

您可以尝试UI自动化API。它还支持遗留应用程序。

#2


2  

Maybe try the PostMessage function instead:

也许尝试使用PostMessage函数:

    [DllImport("user32.dll", CharSet=CharSet.Auto)] 
    private static extern int PostMessage(
            int hWnd, int msg, int wParam, IntPtr lParam);

#3


1  

Depending on the target app, there could be lots of issues. Some apps trigger on WM_KEYDOWN or WM_KEYUP instead of WM_CHAR. Also, the key might already be down and is being ignored. For targets like webbrowsers, you need to get the right window handle. Winspector can get that for you.

根据目标应用程序,可能会出现很多问题。某些应用程序在WM_KEYDOWN或WM_KEYUP而不是WM_CHAR上触发。此外,密钥可能已经关闭并被忽略。对于像webbrowsers这样的目标,你需要获得正确的窗口句柄。 Winspector可以帮你。

#4


0  

Your code wont work because SendMessage requires that the window you're sending to be active. Try PostMessage.

您的代码无法正常工作,因为SendMessage要求您发送的窗口处于活动状态。试试PostMessage。