简体   繁体   English

SendInput 到最小化 window

[英]SendInput to minimized window

Is it possible to utilize the sendInput function on windows that currently do not have focus, and maybe through the use of multithreading, sendinput to multiple minimized windows at the same time, or send input to one window while you're working on another window? Is it possible to utilize the sendInput function on windows that currently do not have focus, and maybe through the use of multithreading, sendinput to multiple minimized windows at the same time, or send input to one window while you're working on another window?

I'd like to do something like this in c#我想在 c# 中做这样的事情

thanks in advance.提前致谢。

You can only use SendInput to send input to the HWND with keyboard focus.您只能使用SendInput将输入发送到具有键盘焦点的 HWND。 Furthermore the window must be attached to the calling thread's message queue, so one cannot simply SetFocus either.此外, SetFocus必须附加到调用线程的消息队列,因此也不能简单地设置焦点。

You'll need to get the window's thread id with GetProcessIdOfThread .您需要使用GetProcessIdOfThread获取窗口的线程 ID。

When you have the thread id you can use the AttachThreadInput function to attach your thread to the other threads input processing.当您拥有线程 ID 时,您可以使用AttachThreadInput function 将您的线程附加到其他线程输入处理。

After all this you can probably use SetFocus and SendInput .毕竟,您可能可以使用SetFocusSendInput

You'll probably want to detach your thread when you've sent your input.发送输入后,您可能希望分离线程。

To get access to these method you'll have to use P/Invoke for C# or C++/CLI.要访问这些方法,您必须对 C# 或 C++/CLI 使用 P/Invoke。 PInvoke.net is very handy as a reference. PInvoke.net作为参考非常方便。 It will be a small chore importing all those functions, but when you are done you should be able to send input to whatever "window" you want.导入所有这些功能将是一件小事,但是当您完成后,您应该能够将输入发送到您想要的任何“窗口”。

Also as a side note, I'm not sure if you are aware of this, but in pure Win32 everything is regarded as a window, even a button.另外作为旁注,我不确定您是否知道这一点,但在纯 Win32 中,所有内容都被视为 window,甚至是按钮。 If you are unlucky you may have to send the input to the handle of the text control belonging to the notepad application.如果运气不好,您可能不得不将输入发送到属于记事本应用程序的文本控件的句柄。

That is not possible with SendInput.这对于 SendInput 是不可能的。 What you probably want to do is find the messages that were sent to the window by the OS when that particular event was performed then emulate them.您可能想要做的是找到执行该特定事件时操作系统发送到 window 的消息,然后模拟它们。 You can use Spy++ to attach to the target window and perform your event.您可以使用 Spy++ 附加到目标 window 并执行您的事件。 Then use SendMessage() and PostMessage() to reproduce the messages that were generated by your event.然后使用 SendMessage() 和 PostMessage() 重现您的事件生成的消息。 This will work fine for notepad.这适用于记事本。

If you do use this method, note that you need to send messages to notepad's child window which you can find with FindWindowEx() with a classname of "edit".如果您确实使用此方法,请注意您需要向记事本的子 window 发送消息,您可以使用类名为“edit”的 FindWindowEx() 找到它。 For example to type text you could try WM_KEYDOWN.例如要键入文本,您可以尝试 WM_KEYDOWN。 You should note that this method is not necessarily reliable: http://blogs.msdn.com/b/oldnewthing/archive/2005/05/30/423202.aspx您应该注意,这种方法不一定可靠: http://blogs.msdn.com/b/oldnewthing/archive/2005/05/30/423202.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM