将文本插入另一个应用程序的文本框中
如何使用 C# 或 C++ 将文本插入另一个应用程序的文本框中? 我很久以前就这样做了,似乎还记得一些有关使用应用程序 HWND 的事情。但由于应用程序的每个实例都发生了变化,我觉得我不记得完整的故事了。我是否可以以某种方式获取正在运行的应用程序的列表,提取我想要的应用程序,从中获取 HWND,然后......嗯......然后呢? :)
How do I, using C# or C++, insert text into the textbox of another application?
I did this a long time ago and seemed to remember something about using the applications HWND. But since that change for every instance of the application I feel that I fon't remember the complete story. Do I somehow get a list of running apps, extract the one I want, get the HWND from that and then... hmm.... then what? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 FindWindowEx() 查找句柄 (HWND) 并然后使用 WM_SETTEXT 消息://msdn.microsoft.com/en-us/library/ms644950%28v=vs.85%29.aspx">SendMessage()
使用 FindWindowEx 时,您需要首先使用以下命令查找主窗口句柄它的类名。然后,您需要找到文本框所在容器的句柄,调用 FindWindowEx,传递父级(窗口)的句柄和容器的类名。您需要重复此操作,直到到达文本框。您可以使用默认安装的 Spy++ 工具使用 Visual Studio 检查目标应用程序并找出容器的层次结构(所有对象在 API 中实际上都称为窗口,但我将它们称为容器,而不是顶级窗口)及其类名称。
Use FindWindowEx() to find the handle (HWND) and then send the WM_SETTEXT message using SendMessage()
When using FindWindowEx you will need to first find the main window handle by using its class name. Then you will need to find the handle of whatever container the textbox is in, calling FindWindowEx, passing the handle of the parent (the window), and the class name of the container. You will need to repeat this until you reach the textbox. You can use a tool called Spy++ that is installed by default with Visual Studio to inspect the target application and find out the hierarchy of containers (all objects are really called windows in the API but I'm calling them containers in contrast with the top-level window) with their class names.
然后SendMessage(),WM_SETTEXT
Then SendMessage(), WM_SETTEXT
您可以将击键发送到文本字段,而不是针对特定的应用程序。
Instead of targeting a specific app you could just send keystrokes to the text field.
您可以使用 ClipBoard 类来实现相同的效果
you can use ClipBoard class to achieve the same