Eine Element-Wrapper-Klasse für die Fensterautomatisierung, die einige Muster für ein Element unterstützt.
Code: Select all
public class WindowElement
{
private IUIAutomationElement? element;
public WindowElement(IUIAutomationElement element)
{
this.element = element;
}
public WindowElement()
{
}
public bool IsValid()
{
return element != null;
}
public IUIAutomationElement GetElement()
{
return element;
}
public bool Expand()
{
if (element != null)
{
IUIAutomationExpandCollapsePattern pattern = element.GetCurrentPattern(WindowAutomation.UIA_ExpandCollapsePatternId);
pattern.Expand();
return pattern.CurrentExpandCollapseState == ExpandCollapseState.ExpandCollapseState_Expanded;
}
return false;
}
public bool Invoke()
{
if (element != null)
{
IUIAutomationInvokePattern pattern = element.GetCurrentPattern(WindowAutomation.UIA_InvokePatternId);
pattern.Invoke();
return true;
}
return false;
}
public bool Select()
{
if (element != null)
{
IUIAutomationSelectionItemPattern pattern = element.GetCurrentPattern(WindowAutomation.UIA_SelectionItemPatternId);
pattern.Select();
return true;
}
return false;
}
}
Code: Select all
public class WindowAutomation
{
private CUIAutomation8 uia = new CUIAutomation8();
public WindowElement FindProcessNameElement(string processName)
{
IUIAutomationElement root = uia.GetRootElement();
IUIAutomationElementArray children = root.FindAll(TreeScope.TreeScope_Children, uia.CreateTrueCondition());
for (int i = 0; i < children.Length; i++)
{
IUIAutomationElement child = children.GetElement(i);
string childName = child.CurrentName;
if (childName.StartsWith(processName))
{
return new WindowElement(child);
}
}
return new WindowElement();
}
public WindowElement FindMenuBarItem(WindowElement appElement, string itemName)
{
if (appElement.IsValid())
{
IUIAutomationElementArray items = appElement.GetElement().FindAll(TreeScope.TreeScope_Children, uia.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_MenuBarControlTypeId));
if (items.Length > 0)
{
items = items.GetElement(0).FindAll(TreeScope.TreeScope_Children, uia.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_MenuItemControlTypeId));
for (int i = 0; i < items.Length; i++)
{
IUIAutomationElement item = items.GetElement(i);
if (item.CurrentName == itemName)
{
return new WindowElement(item);
}
}
}
}
return new WindowElement();
}
public WindowElement FindSubmenuItem(WindowElement menuItem, string subitemName)
{
if (menuItem.IsValid())
{
menuItem.Expand();
IUIAutomationElementArray items = menuItem.GetElement().FindAll(TreeScope.TreeScope_Descendants, uia.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_MenuItemControlTypeId));
if (items.Length > 0)
{
for (int i = 0; i < items.Length; i++)
{
IUIAutomationElement item = items.GetElement(i);
string name = item.CurrentName;
if (item.CurrentName == subitemName)
{
return new WindowElement(item);
}
}
}
}
return new WindowElement();
}`
Code: Select all
WindowElement app = ui.FindProcessNameElement("gMotor2 MAS");
WindowElement fileMenu = ui.FindMenuBarItem(app, "File");
if (fileMenu.Expand())
{
Console.WriteLine("Expanded file menu");
}
WindowElement exitItem = ui.FindSubmenuItem(fileMenu, "Exit");
exitItem.Invoke();