UI Automation switch window
c#, ui-automation, winapi
Solution
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.
Problem
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?