0

我有一个 WPF 应用程序,在初始屏幕上有两个文本框控件。

现在我需要在这个文本框中设置一些文本。通常我会用FindWindowEx和 call找到控制窗口SetWindowText,但问题是我在这里看不到任何窗口。

即使我打开Spy++唯一的窗口,我看到的就是窗口本身。根本没有孩子。你能解释一下发生了什么吗?它在 WPF 的其他地方工作吗?它不只是一个带有普通HWND句柄的普通窗口吗?如何在这些控件中设置文本?

我按照评论中的建议进行了尝试Snoop,我能够看到一些窗口,但看不到启动屏幕上的控件。我也想从 C# 应用程序自动执行此操作,而不是手动使用Snoop. 我有来源,Snoop看起来你需要在你的应用程序中加入一些东西才能做到这一点。这样对吗?

从另一个进程为 WPF 控件设置文本真的那么难吗?

4

2 回答 2

1

解决方案是使用Microsoft UI 自动化

AutomationElement rootElement = AutomationElement.RootElement;

if (rootElement != null)
{
    Condition condition =
            new PropertyCondition(AutomationElement.NameProperty, "WindowSplash");

    AutomationElement appElement =
            rootElement.FindFirst(TreeScope.Children, condition);

    if (appElement != null)
    {
        Condition condition =
            new PropertyCondition(
                    AutomationElement.AutomationIdProperty, "element1");
        AutomationElement element =
            parentElement.FindFirst(TreeScope.Descendants, condition);

        if (element != null)
        {
            ValuePattern valuePatternB =
                    element.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
            valuePatternB.SetValue("hello automation world!");
        }
    }
}
于 2014-04-19T10:23:08.677 回答
0

我认为您可以为您的 WPF 窗口制作自定义窗口消息(例如,标识符 1234)。之后,您可以使用来自另一个进程的 PostMessage WinAPI 函数将此 1234 消息与您要设置的字符串一起发布到您的 WPF 窗口。WPF 窗口将覆盖默认消息处理过程,当遇到标识符为 1234 的消息时,它将使用 WPF 引擎类和方法设置适当的控件文本。

于 2014-04-19T09:51:18.203 回答