WindowPattern控件模式用于支持传统的图形用户界面(GUI)内提供基于窗口的功能的控件。必须实现此控件模式的控件示例包括顶级应用程序窗口,多文档界面(MDI)子窗口,大小可调的拆分窗格控件,模式对话框以及气球状帮助窗口。可以使用WindowPattern来对window进行操作,例如验证window是否激活,是否最大化,最小化,正常模式以及关闭window等。
示例:
public static void launchCal()
{
//Launch Calculator
Process proc = Process.Start(@"C:/WINDOWS/system32/calc.exe");
Thread.Sleep(2000);
//Recognize Calculator Window "Calculator"
AutomationElement window = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Calculator"));
Assert.IsNotNull(window);
Thread.Sleep(2000);
WindowPattern window1pattern = Utility.UIA.ControlPattern.Windowpattern.GetWindowPattern(window);
window1pattern.SetWindowVisualState(WindowVisualState.Minimized);
Thread.Sleep(2000);
window1pattern.SetWindowVisualState(WindowVisualState.Normal);
Thread.Sleep(2000);}
public static WindowPattern GetWindowPattern(AutomationElement element)
{
object currentPattern;
if (!element.TryGetCurrentPattern(WindowPattern.Pattern, out currentPattern))
{
throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the WindowPattern.",
element.Current.AutomationId, element.Current.Name));
}
return currentPattern as WindowPattern;
}