将文本插入另一个应用程序的文本框中

发布于 2024-10-09 11:16:40 字数 177 浏览 0 评论 0原文

如何使用 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 技术交流群。

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

发布评论

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

评论(4

画尸师 2024-10-16 11:16:40

使用 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.

╭⌒浅淡时光〆 2024-10-16 11:16:40

然后SendMessage(),WM_SETTEXT

Then SendMessage(), WM_SETTEXT

凌乱心跳 2024-10-16 11:16:40

您可以将击键发送到文本字段,而不是针对特定的应用程序。

  private void button1_Click(object sender, EventArgs e)
    {
       System.Threading.Thread.Sleep(5000);
       SendKeys.Send(send_text);

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        send_text = textBox1.Text;            
    }

Instead of targeting a specific app you could just send keystrokes to the text field.

  private void button1_Click(object sender, EventArgs e)
    {
       System.Threading.Thread.Sleep(5000);
       SendKeys.Send(send_text);

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        send_text = textBox1.Text;            
    }
深者入戏 2024-10-16 11:16:40

您可以使用 ClipBoard 类来实现相同的效果

you can use ClipBoard class to achieve the same

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