UI Automation Toggle Window

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

I noticed that using UIAutomation, where possible, seems to improve the situation.

For instance:

Getting WindowPattern and using something like:

windowPattern.SetWindowVisualState( WindowVisualState.Normal ); windowPattern.SetWindowVisualState( WindowVisualState.Maximized ); 

Now my questions are:

How to find out if I should do this as much as possible or normally. The task manager and the dragon naturally both seem to know how to do this. If it was previously maximized and then minimized, I would like to enlarge the window when I switch to it. If this had not previously been maximized, I would like to make it "Normal."

Any ideas?

+6
source share
3 answers

SetFocus for AutomationElement does not work.

From the following question: Get the window state of another process

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

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

In this case, the window will be restored to the maximum value if it was maximized and restored to normal if it was not maximum.

+2
source

Well, I misunderstood the question. I believe the key to what you want is to use the AutomationElement.SetFocus () method.

Here is a basic example.

 //I assume you already have some way of getting the window handle var wih = new System.Windows.Interop.WindowInteropHelper(window); IntPtr hWnd = wih.Handle; //get the automation element from the handle var ae = AutomationElement.FromHandle(hWnd); //this will bring the window into focus. ae.SetFocus(); 
+1
source

You can use WindowPattern to set the window to the front as follows:

 var p = (WindowPattern)_w.AutomationElement.GetCurrentPattern(WindowPattern.Pattern); p.SetWindowVisualState(WindowVisualState.Normal); 
+1
source

Source: https://habr.com/ru/post/949470/


All Articles