简体   繁体   English

设置WPF应用程序的控制文本

[英]Set control text for WPF application

I have a WPF application with two text box controls on a splash screen. 我有一个在初始屏幕上带有两个文本框控件的WPF应用程序。

Now I need to set some text in this textboxes. 现在,我需要在此文本框中设置一些文本。 Normally I would find control window with FindWindowEx and call SetWindowText , but the problem is that I can't see any windows here. 通常,我会使用FindWindowEx查找控件窗口并调用SetWindowText ,但问题是我在这里看不到任何窗口。

Even when I open Spy++ the only window I see is the window itself. 即使打开Spy++ ,我看到的唯一窗口也是窗口本身。 No children at all. 根本没有孩子。 Could you please explain what's going on? 你能解释发生了什么吗? Does it work somewhere else in WPF? 它可以在WPF中的其他地方工作吗? Isn't it just a normal window with normal HWND handle? 这不只是具有普通HWND句柄的普通窗口吗? How can I set text in those controls? 如何在这些控件中设置文本?

I tried Snoop as suggested in comments and I was able to see some of the windows, but not the controls on a splash screen. 我按照注释中的建议尝试了Snoop ,并且可以看到一些窗口,但看不到启动屏幕上的控件。 Also I would like to do this automatically from C# application, not manually with Snoop . 我也想从C#应用程序中自动执行此操作,而不是通过Snoop手动进行。 I've got the sources of Snoop and looks like you need to ijnect something into your application in order to do this. 我有Snoop的资源,看起来您需要将某些东西添加到您的应用程序中才能执行此操作。 Is it right? 这样对吗?

Is it really that hard to set text for the WPF control from another process? 从另一个进程为WPF控件设置文本真的很困难吗?

I think you can make a custom windows message for your WPF window(with, say, identifier 1234). 我认为您可以为WPF窗口(带有标识符1234)制作自定义Windows消息。 After that you can use PostMessage WinAPI function from another process to post this 1234-message to your WPF window with string you want to set. 之后,您可以使用另一个过程中的PostMessage WinAPI函数,将此1234消息与您要设置的字符串一起发布到WPF窗口中。 The WPF Window will have override for default message handling procedure and when encountering message with identifier 1234 it will set appropriate control's text using WPF engine classes and methods. WPF窗口将覆盖默认的消息处理过程,当遇到标识为1234的消息时,它将使用WPF引擎类和方法设置适当的控件文本。

Solution is to use Microsoft UI Automation 解决方案是使用Microsoft UI Automation

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!");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM