使用 C# 获取 Open Process Windows 文本框 [英] Get Open Process Windows Textbox with C#

查看:32
本文介绍了使用 C# 获取 Open Process Windows 文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到打开的进程或应用程序文本框并更改其值.但我想用 c# 的方式来做.如果有人知道你可以和我分享吗?还是我必须使用 C++ 以及如何使用?

I need to find open process or application textbox and change its value. But i wanna do it with c# way. If anyone knows could you please share with me? Or do i have to use c++ and how?

感谢您的建议.

推荐答案

正如另一个人所说,UIAutomation 是要走的路.http://msdn.microsoft.com/en-us/library/ms753107.aspx

Like another said, UIAutomation is the way to go. http://msdn.microsoft.com/en-us/library/ms753107.aspx

以下代码将打开 Notepad.exe,打开其文件对话框,然后在文件名字段中输入一些文本.

The following code will open Notepad.exe, open its File dialog, then type in some text into the file name field.

        Process notepad = Process.Start("notepad");

        Thread.Sleep(5000);

        SendKeys.SendWait("^o"); //ctrl+o to open the File Dialog

        var notepadTextBox = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, 
            new PropertyCondition(AutomationElement.AutomationIdProperty, "1148"));

        object valuePattern = null;

        if (notepadTextBox.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern))
        {
            ((ValuePattern)valuePattern).SetValue("THERE YOU GO"); // this text will be entered in the textbox
        }
        else 
        {
            //ERROR
        }

所以这实际上是发送击键以控制 UI(打开文件打开对话框)的组合.UIAutomation,但如果需要,您可以将其更改为驱动菜单,就像用户一样.

So this is really a combination of sending keystrokes to control the UI (bring up the File Open dialog) & UIAutomation, but you could change it to drive the Menu like a user would if you need to.

此外,您可能想知道魔术字符串1148"从何而来 - 即记事本中文件名输入字段的自动化 ID".我使用inspect.exe(包含在Windows SDK 中)来查找自动化ID,您将需要它来让您的应用程序查看其AutomationId(如果有).

Also you might be wondering where the magic string "1148" comes from - that is the "Automation Id" of the file name entry field in Notepad. I used inspect.exe (included in the Windows SDK) to find the automation Id, you will need that for your application to see its AutomationIds, if it has any.

这篇关于使用 C# 获取 Open Process Windows 文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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