C#的SendKeys到其他应用程序特定的文本框 [英] C# sendkeys to other application to particular textfield

查看:1318
本文介绍了C#的SendKeys到其他应用程序特定的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的SendKeys到其他应用程序在使用C#其它应用特定的文本框。

I need to sendkeys to other application to particular textfield in other application using C#.

这可能吗?如果是的话,任何人都可以给我示例代码?

Is that possible? If yes, can anyone give me sample code?

推荐答案

的SendKeys 只发送按键组合到活动窗口

所以,如果你的C#应用​​程序调用在恰当的时间的SendKeys 功能,当用户有其他应用程序的文本框为中心,一切都将工作得很好。

So if your C# application calls the SendKeys function at precisely the right time, when the user has that other application's textbox focused, everything will work just fine.

这个问题,当然是现实世界中很少这样完美的。你不知道什么时候,用户将不得不在其它应用重点,当然不会知道,如果他们已经点击了TextBox控件。你也不会有任何方式为用户告诉你的C#应用​​程序的的发送文本(如单击按钮),因为为了点击按钮,的应用程序必须有前景的重点,因此的的要接收键盘输入应用程序。

The problem, of course, is that the real world is rarely this perfect. You don't know exactly when the user will have the other application focused, and certainly won't know if they've clicked in the textbox control. You also won't have any way for the user to tell your C# application when to send the text (such as clicking a button) because in order to click the button, your application must have the foreground focus, and therefore not the application that you want to receive the keyboard input.

而这只是一个简要概述中的尝试做UI自动化这样的时候,你会遇到潜在问题有些的。这是非常脆弱的,很容易导致对用户的一些意外,真气的行为。我强烈反对它。一个更好的选项与其他应用程序直接通信,如通过发送短信,而不是试图侵入控制它的UI。

And that's just a brief overview of some of the potential problems you'll encounter when trying to do UI automation this way. This is extremely fragile, and can easily lead to some unexpected, infuriating behavior for the user. I strongly recommend against it. A much better option is communicating directly with the other application, such as by sending it messages, rather than trying to invasively control its UI.

如果你绝对的必须的做到这一点,尽管相反的所有建议,最好的办法是让手柄要发送的文字窗口,然后发送的 WM_SETTEXT 消息。这当然会,要求从Win32 API,你的P / Invoke的一些功能。请参阅这个问题的答案对于一些示例代码。

If you absolutely must do this, despite all recommendations to the contrary, the best way is to get the handle to the window you want to send the text to, and then send a WM_SETTEXT message. That will, of course, require that you P/Invoke a few functions from the Win32 API. See the answers to this question for some sample code.

这篇关于C#的SendKeys到其他应用程序特定的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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