How do I bring Set Focus of MDI Child Window using

2020-08-01 06:05发布

问题:

We have an old legacy application we need to automate. It uses MDI Windows.

We're using UIAutomation and I can succesfully get the appropriate AutomationElement for each MDI Child window. What I cannot do is bring that element into focus.

Here is some example code that I tried, that fails:

        var desktop = AutomationElement.RootElement;
        var dolphin = desktop.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.NameProperty,
                    "Dolphin for Windows",
                    PropertyConditionFlags.IgnoreCase));
        dolphin.SetFocus();

        var workspace = dolphin.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.NameProperty,
                    "Workspace",
                    PropertyConditionFlags.None));

        var childWindow = workspace.FindFirst(TreeScope.Children, new
                PropertyCondition(AutomationElement.NameProperty, "Sharp   "));
        childWindow.SetFocus();

The last line in this code fails with System.InvalidOperationException

Experimenting, I tried finding a control on the childWindow, and calling SetFocus on it. It DID correctly set the focus on the right control, but it did not bring the MDI window to the foreground.

Any ideas?

回答1:

Have you tried "BringToFront" before you set focus? I can imagine that the top-level control (mdi-parent) won't allow focus on children or is unable to do so when the child (mdi-child) when it's not visible.