Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI Automation switch window

I've noticed that setforegroundwindow can be very flaky - no matter how you do it.

I've noticed that using UIAutomation, where possible, seems to improve things.

For example:

Getting the WindowPattern and using something like:

windowPattern.SetWindowVisualState( WindowVisualState.Normal );

windowPattern.SetWindowVisualState( WindowVisualState.Maximized );

Now my questions is:

How do I know whether I should make it maximized or normal. The task manager, and dragon naturally speaking both seem to know how to do this. If it was previous maximized, and then minimized, I'd like to maximize the window when I switch to it. If it was previously not maximized, I'd like to make it "Normal".

Any ideas?

like image 714
Derek Avatar asked Jul 15 '13 13:07

Derek


People also ask

What is window automation?

Window automation is the automation of your actual windows. This means that your windows are outfitted with electronics and will open and close with commands from a remote or from your smart phone.

How to use UI element in Power Automate?

UI automation actions accept desktop UI elements, while browser automation actions accept web UI elements. To add a new UI element to your flow, select to add a new UI element through an action or the UI elements pane, highlight the respective element, and press Ctrl + Left click.

How do you get UI flows in Power Automate?

To install the software, go to the Power Automate Documentation. Click the UI flows option. Then, click Set up UI flows.

How do I edit the selector in Power Automate desktop?

To edit an existing selector, select the appropriate UI element and choose the selector you want to edit. You can create multiple selectors for a UI element. Whenever a selector fails, Power Automate uses the next selector in the defined order.


2 Answers

SetFocus for AutomationElement didn't work.

From the following question: Get window state of another process

I found that the GetPlacement api gave me what I needed:

     if ( (windowplacement.flags & WPF_RESTORETOMAXIMIZED) > 0 )
     {
        windowPattern.SetWindowVisualState( WindowVisualState.Maximized );
     }
     else
     {
        windowPattern.SetWindowVisualState( WindowVisualState.Normal );
     }

With this the window will restore to maximized if it was maximized and restore to normal if it was not maximized.

like image 171
Derek Avatar answered Nov 02 '22 20:11

Derek


You may use the WindowPattern to set window to foreground as follows:

var p =
 (WindowPattern)_w.AutomationElement.GetCurrentPattern(WindowPattern.Pattern);
p.SetWindowVisualState(WindowVisualState.Normal);
like image 43
oshea00 Avatar answered Nov 02 '22 21:11

oshea00