GPAL - Generally Positive Automation Library v1.0
GPAL The Fluent Automation LIbrary
Loading...
Searching...
No Matches
GPALAutomationElement.cs
1// =============================================================================
2// GPAL - Generally Positive Automation Library
3// Copyright © 2026 Software Decisions, Inc. All rights reserved.
4//
5// This file is part of GPAL.
6// Licensed under the Business Source License 1.1
7//
8// Primary development, architecture, and vision by Michael B. Vederman,
9// CEO of Software Decisions, Inc., Texas.
10//
11// Internal development maintained privately.
12// Public releases appear on GitHub: https://github.com/SoftwareDecisionsInc/GPAL.
13//
14// See LICENSE for full terms, including Additional Use Grant.
15// =============================================================================
16
17//extern alias mUIA;
18//extern alias mUIAtypes;
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using System.Text;
23using System.Threading.Tasks;
24using System.Windows.Automation;
25
27{
31 public class GPALAutomationElement : IGPALAutomationElement
32 {
33 public Application Application { get; internal set; }
34 public Selector Selector { get; internal set; }
35
36 // public mUIA.System.Windows.Automation.AutomationElement Mae { get; set; }
40 public AutomationElement Ae { get; set; }
47 public IGPALElement Gae { get; set; }
53 internal GPALAutomationElement ImageMatch { get; set; }
63 public bool Selected
64 {
65 get
66 {
67 bool retVal = false;
68 object pattern;
69
70 if (null != Ae && true == Ae.TryGetCurrentPattern(SelectionItemPattern.Pattern, out pattern))
71 retVal = ((SelectionItemPattern)pattern).Current.IsSelected;
72 else if (null != Ae && true == Ae.TryGetCurrentPattern(TogglePattern.Pattern, out pattern))
73 retVal = ToggleState.On == ((TogglePattern)pattern).Current.ToggleState;
74 else
75 retVal = ContainerSaysSelected(Ae) || true == MsaaHelper.Selected(Application, Ae);
76
77 return retVal;
78 }
79 }
80
95 private static bool ContainerSaysSelected(AutomationElement element)
96 {
97 bool retVal = false;
98 AutomationElement above = null == element ? null : TreeWalker.ControlViewWalker.GetParent(element);
99
100 for (int level = 0; level < ContainerLevels && false == retVal && null != above; level++)
101 {
102 if (true == above.TryGetCurrentPattern(SelectionPattern.Pattern, out object pattern))
103 foreach (AutomationElement selected in ((SelectionPattern)pattern).Current.GetSelection())
104 if (true == element.Equals(selected))
105 retVal = true;
106
107 above = ControlType.Window == above.Current.ControlType ? null : TreeWalker.ControlViewWalker.GetParent(above);
108 }
109
110 return retVal;
111 }
112
113 // how many parents up a selection container is looked for. The Explorer ribbon puts two elements
114 // between a tab and the element holding the tabs, and nothing legitimately sits further away than this
115 private const int ContainerLevels = 5;
116
124 public bool? Checked
125 {
126 get
127 {
128 bool? retVal = null;
129 object pattern;
130
131 if (null != Ae && true == Ae.TryGetCurrentPattern(TogglePattern.Pattern, out pattern))
132 {
133 ToggleState state = ((TogglePattern)pattern).Current.ToggleState;
134
135 if (ToggleState.On == state)
136 retVal = true;
137 else if (ToggleState.Off == state)
138 retVal = false;
139 }
140
141 return retVal;
142 }
143 }
144
146 public bool Enabled => null != Ae && true == Ae.Current.IsEnabled;
147
153 public bool Displayed => null != Ae && false == Ae.Current.IsOffscreen;
154
160 public string Text => null == Ae ? string.Empty : (Ae.GetText() ?? string.Empty);
161
166 public string TagName => null == Ae
167 ? string.Empty
168 : (Ae.Current.ControlType?.ProgrammaticName ?? string.Empty).Replace("ControlType.", "");
169
171 public string Name => null == Ae ? string.Empty : (Ae.Current.Name ?? string.Empty);
172
177 public System.Drawing.Point Location => null == Ae
178 ? new System.Drawing.Point(0, 0)
179 : new System.Drawing.Point((int)Ae.Current.BoundingRectangle.Left, (int)Ae.Current.BoundingRectangle.Top);
180
182 public System.Drawing.Size Size => null == Ae
183 ? new System.Drawing.Size(0, 0)
184 : new System.Drawing.Size((int)Ae.Current.BoundingRectangle.Width, (int)Ae.Current.BoundingRectangle.Height);
185
190 internal int MenuCommand { get; set; } = 0;
194 internal IntPtr MenuWindow { get; set; } = IntPtr.Zero;
195 internal GPALAutomationElement()
196 {
197
198 }
199 internal GPALAutomationElement(AutomationElement automationElement = null, IGPALElement gPALElement = null/*, mUIA.System.Windows.Automation.AutomationElement managedAutomationElement = null*/)
200 {
201 Ae = automationElement;
202 Gae = gPALElement;
203// Mae = managedAutomationElement;
204 }
205 }
206}
207
Application workflow container for automation elements.
bool Enabled
Whether the element is enabled, from UIAutomation's IsEnabled.
string Text
The element's text, read through TextPattern where the control has one and ValuePattern otherwise,...
System.Drawing.Size Size
Dimensions of the element, from its bounding rectangle.
bool Selected
Whether the element is selected, the same question GPALElement.Selected answers on a page....
bool Displayed
Whether the element is on screen, from UIAutomation's IsOffscreen. A control scrolled out of view or...
string Name
The element's name, which is how a window labels a control.
IGPALElement Gae
GPAL psuedo element. Only the following properties are actually used: Location, Size,...
System.Drawing.Point Location
Screen coordinates of the element, the top left of its bounding rectangle. Always screen coordinates...
string TagName
What kind of control this is, Button, ListItem, Edit and so on. A window's answer to a tag name,...
bool? Checked
A checkbox's state, which has three of them: true ticked, false clear, and null for the third one,...