Question

I have a WPF application with two text box controls on a splash screen.

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.

Even when I open Spy++ the only window I see is the window itself. No children at all. Could you please explain what's going on? Does it work somewhere else in WPF? Isn't it just a normal window with normal HWND handle? 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. Also I would like to do this automatically from C# application, not manually with Snoop. I've got the sources of Snoop and looks like you need to ijnect something into your application in order to do this. Is it right?

Is it really that hard to set text for the WPF control from another process?

Was it helpful?

Solution

Solution is to use 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!");
        }
    }
}

OTHER TIPS

I think you can make a custom windows message for your WPF window(with, say, identifier 1234). 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. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top