Chapter 4 UI Automation Pattern
DockPattern
DockPattern用于操作可停靠容器控件,我们最熟悉的VS2005/2008中的ToolBox,Solution Explorer都可以设置不同的DockPosition, 但是目前并不支持DockPattern,所以无法做为实例来讲。使用DockPattern的前提为控件支持DockPattern。 DockPattern中的DockPosition有六个枚举变量,即Bottom、Left、Right、Top、Fill和None。如果控件支持DockPattern, 则可以获取相对应的DockPosition以及设置控件的DockPosition。
如下代码是获取控件的DockPattern、获取控件当前的DockPosition以及设置控件的DockPosition。
Code
#region DockPattern helper
/// <summary>
/// Get DockPattern
/// </summary>
/// <param name="element">AutomationElement instance</param>
/// <returns>DockPattern instance</returns>
public static DockPattern GetDockPattern(AutomationElement element)
{
object currentPattern;
if (!element.TryGetCurrentPattern(DockPattern.Pattern, out currentPattern))
{
throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the DockPattern.",
element.Current.AutomationId, element.Current.Name));
}
return currentPattern as DockPattern;
}
/// <summary>
/// Get DockPosition
/// </summary>
/// <param name="element">AutomationElement instance</param>
/// <returns>DockPosition instance</returns>
public static DockPosition GetDockPosition(AutomationElement element)
{
return GetDockPattern(element).Current.DockPosition;
}
/// <summary>
/// Set DockPosition
/// </summary>
/// <param name="element">AutomationElement instance</param>
public static void SetDockPattern(AutomationElement element, DockPosition dockPosition)
{
GetDockPattern(element).SetDockPosition(dockPosition);
}
#endregion
由于目前未找到支持DockPattern的实例,所以比较遗憾。
Open Source code
Github: https://github.com/cumtkangyi/ATP