/// <summary> /// Build a cacherequest for properties and patterns /// </summary> /// <param name="uia"></param> /// <param name="pps">Property ids</param> /// <param name="pts">Pattern ids</param> /// <returns></returns> public static IUIAutomationCacheRequest GetPropertiesCache(CUIAutomation uia, List <int> pps, List <int> pts) { if (uia == null) { throw new ArgumentNullException(nameof(uia)); } var cr = uia.CreateCacheRequest(); if (pps != null) { foreach (var pp in pps) { cr.AddProperty(pp); } } if (pts != null) { foreach (var pt in pts) { if (pt != 0) { cr.AddPattern(pt); } } } return(cr); }
public FocusChangedEventListener(CUIAutomation uia, HandleUIAutomationEventMessage peDelegate) { this.UIAutomation = uia; this.ListenEventMessage = peDelegate; this.UIAutomation.AddFocusChangedEventHandler(null, this); IsHooked = true; }
/// <summary> /// Build a cacherequest for properties and patterns /// </summary> /// <param name="uia"></param> /// <param name="pps">Property ids</param> /// <param name="pts">Pattern ids</param> /// <returns></returns> public static IUIAutomationCacheRequest GetPropertiesCache(CUIAutomation uia, List <int> pps, List <int> pts) { var cr = uia.CreateCacheRequest(); if (pps != null) { foreach (var pp in pps) { cr.AddProperty(pp); } } if (pts != null) { foreach (var pt in pts) { if (pt != 0) { cr.AddPattern(pt); } } } return(cr); }
static void Main(string[] args) { try { var process = Process.Start(@"C:\Windows\system32\control.exe", @"/name Microsoft.DefaultPrograms /page pageDefaultProgram\pageAdvancedSettings?pszAppName=google%20chrome"); var exited = process.WaitForExit(3000); if (!exited) { process.Kill(); throw new Exception("Control panel process didn't exit"); } var uiAutomation = new CUIAutomation(); var window = FindAndWait(uiAutomation.GetRootElement(), TreeScope.TreeScope_Children, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Set Program Associations"), TimeSpan.FromSeconds(3)); try { //Sadly, the window can't be hidden or moved to another desktop since doing so results in UI Automation not seeing it. var listView = FindAndWait(window, TreeScope.TreeScope_Descendants, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Program Association List"), TimeSpan.FromSeconds(3)); var save = FindAndWait(window, TreeScope.TreeScope_Descendants, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Save"), TimeSpan.FromSeconds(3)); var listItems = new[] { ".htm", ".html", "HTTP", "HTTPS", }.Select(name => FindAndWait( listView, TreeScope.TreeScope_Descendants, uiAutomation.CreateAndCondition( uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, name), uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListItemControlTypeId) ), TimeSpan.FromSeconds(3) )).ToArray(); foreach (var listItem in listItems) { var toggle = ((IUIAutomationTogglePattern)listItem.GetCurrentPattern(UIA_PatternIds.UIA_TogglePatternId)); while (toggle.CurrentToggleState != ToggleState.ToggleState_On) //TODO: Watch out for infinite looping here! { toggle.Toggle(); } } ((IUIAutomationInvokePattern)save.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId)).Invoke(); } finally { ((IUIAutomationWindowPattern)window.GetCurrentPattern(UIA_PatternIds.UIA_WindowPatternId)).Close(); } } catch //Ensures all nested finally blocks execute { throw; } }
public void Test1() { IUIAutomation a = new CUIAutomation(); IUIAutomationElement x = a.GetRootElement(); IUIAutomation2 b = new CUIAutomation8(); }
private static IUIAutomationElement GetDesktopElement() { IUIAutomation uia = new CUIAutomation(); var cacheRequest = uia.CreateCacheRequest(); cacheRequest.AddProperty(PropertyType.UIA_BoundingRectanglePropertyId); return(uia.GetRootElementBuildCache(cacheRequest)); }
public FocusChangedEventListener(CUIAutomation uia, HandleUIAutomationEventMessage peDelegate) { if (uia == null) { throw new ArgumentNullException(nameof(uia)); } this.UIAutomation = uia; this.ListenEventMessage = peDelegate; this.UIAutomation.AddFocusChangedEventHandler(null, this); IsHooked = true; }
static void Main(string[] args) { // Instantiate the UIA object: IUIAutomation _automation = new CUIAutomation(); // Get the root element IUIAutomationElement rootElement = _automation.GetRootElement(); // Get its name string rootName = rootElement.CurrentName; Console.WriteLine( "The root automation element's name should be 'Desktop'."); Console.WriteLine("The actual value is: '{0}'", rootName); }
} // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { if (disposing && this.IsHooked) { UIAutomation = null; UIAutomation8 = null; } } disposedValue = true; } }
static UiaHelper() { try { cUIAutomation = new CUIAutomation(); IsAvailable = true; //var uiaTypes = typeof(UIA_ControlTypeIds); } catch (Exception ex) { //不支持Uia自动化 IsAvailable = false; } }
private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (IsHooked && this.UIAutomation != null) { try { this.UIAutomation.RemoveFocusChangedEventHandler(this); this.UIAutomation = null; } catch (ThreadAbortException) { // silently ignore exception since a ThreadAbortException is thrown at the process exit. } } disposedValue = true; } }
/// <summary> /// Get IUIAutomationTreeWalker based on inidcated mode. /// </summary> /// <param name="mode">TreeViewMode to get walker</param> /// <returns></returns> public static IUIAutomationTreeWalker GetTreeWalker(TreeViewMode mode) { IUIAutomationTreeWalker walker = null; CUIAutomation h = A11yAutomation.GetUIAutomationObject(); switch (mode) { case TreeViewMode.Content: walker = h.ContentViewWalker; break; case TreeViewMode.Control: walker = h.ControlViewWalker; break; case TreeViewMode.Raw: walker = h.RawViewWalker; break; } return(walker); }
public void ProcessHandlerDiagsTest() { var automation = new CUIAutomation(); var root = automation.GetRootElement(); var elements = root.FindAll(TreeScope.TreeScope_Children, automation.CreateTrueCondition()); for (var i = 0; i < elements.Length; i++) { var element = elements.GetElement(i); Debug.Print(element.CurrentClassName + ";" + element.CurrentName + ";" + element.CurrentProcessId + ";" + element.CurrentFrameworkId + ";" + element.CurrentNativeWindowHandle); } Debug.Print("---"); var frameElements = root.FindAll(TreeScope.TreeScope_Children, automation.CreatePropertyConditionEx( UIA_PropertyIds.UIA_ClassNamePropertyId, "ApplicationFrameWindow", PropertyConditionFlags.PropertyConditionFlags_IgnoreCase)); if (frameElements.Length == 0) { Debug.Print("No UWP apps"); return; } for (var i = 0; i < frameElements.Length; i++) { var frameElement = frameElements.GetElement(i); var subElements = frameElement.FindAll(TreeScope.TreeScope_Children, automation.CreateTrueCondition()); for (var j = 0; j < subElements.Length; j++) { var subElement = subElements.GetElement(j); Debug.Print(subElement.CurrentClassName + ";" + subElement.CurrentName + ";" + subElement.CurrentProcessId + ";" + subElement.CurrentFrameworkId + ";" + subElement.CurrentNativeWindowHandle); } Debug.Print("--"); } }
/// <summary> /// Create an event handler and register it. /// </summary> public StructureChangedEventListener(CUIAutomation uia, IUIAutomationElement element, TreeScope scope, HandleUIAutomationEventMessage peDelegate) : base(uia, element, scope, EventType.UIA_StructureChangedEventId, peDelegate) { Init(); }
public void Start() { _automation = new CUIAutomation(); EnumarateChildren(_automation.GetRootElement()); }
public void Test1() { ProcessStartInfo x = new ProcessStartInfo(); x.FileName = @"C:\Windows\System32\calc.exe"; var process = Process.Start(x); process.WaitForInputIdle(); var ROOT = new CUIAutomationClass().GetRootElement(); var prc = ROOT.FindAll(TreeScope.TreeScope_Children, new CUIAutomationClass().CreateTrueCondition()); int size = prc.Length; for (int i = 0; i < prc.Length; i++) { string l = prc.GetElement(i).CurrentName; int r = prc.GetElement(i).CurrentProcessId; } ////This is how you attach to an already opened process //foreach (Process procs in Process.GetProcesses()) //{ // if (procs.ProcessName.Equals("Calculator")) // { // int ProcessID = procs.Id; // } //} IUIAutomationCondition window_cond = new CUIAutomationClass().CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Calculator"); IUIAutomationElement window = null; int attempts = 1; while (window == null && attempts < 10) { window = ROOT.FindFirst(TreeScope.TreeScope_Children, window_cond); attempts++; } IUIAutomationCondition el_cond = new CUIAutomationClass().CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "num9Button"); IUIAutomationElement el = window.FindFirst(TreeScope.TreeScope_Descendants, el_cond); var pat = (IUIAutomationInvokePattern)el.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId); pat.Invoke(); IUIAutomation m = new CUIAutomation(); IUIAutomationCondition hist_Cond = new CUIAutomationClass().CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "HistoryButton"); IUIAutomationElement hist_El = window.FindFirst(TreeScope.TreeScope_Descendants, hist_Cond); var y = hist_El.CurrentHelpText; int p = 0; }
/// <summary> /// Create an event handler and register it. /// </summary> public PropertyChangedEventListener(CUIAutomation uia, IUIAutomationElement element, TreeScope scope, HandleUIAutomationEventMessage peDelegate, int[] properties) : base(uia, element, scope, EventType.UIA_AutomationPropertyChangedEventId, peDelegate) { this.propertyArray = properties; Init(); }
/// <summary> /// Ctor to create an event handler (with CUIAutomation) and register it. /// </summary> public EventListenerBase(CUIAutomation uia, IUIAutomationElement element, TreeScope scope, int eventId, HandleUIAutomationEventMessage peDelegate) : this(element, scope, eventId, peDelegate) { this.UIAutomation = uia; }
/// <summary> /// Create an event handler and register it. /// </summary> public EventListener(CUIAutomation uia, IUIAutomationElement element, TreeScope scope, int eventId, HandleUIAutomationEventMessage peDelegate) : base(uia, element, scope, eventId, peDelegate) { Init(); }