gpt4 book ai didi

c# - 使用 C# 获取打开进程 Windows 文本框

转载 作者:行者123 更新时间:2023-12-02 22:04:17 25 4
gpt4 key购买 nike

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

感谢您的建议。

最佳答案

正如另一个人所说,UIAutomation 是必经之路。 http://msdn.microsoft.com/en-us/library/ms753107.aspx

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

        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 的组合,但如果需要,您可以将其更改为像用户那样驱动菜单。

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

关于c# - 使用 C# 获取打开进程 Windows 文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16367996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com