forked from auuunya/go-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.go
355 lines (308 loc) · 8.6 KB
/
element.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package uiautomation
import (
"fmt"
"reflect"
"strings"
"syscall"
"unsafe"
)
var (
oleAut = syscall.NewLazyDLL("OleAut32.dll")
procSysFreeString = oleAut.NewProc("SysFreeString")
procSysAllocString = oleAut.NewProc("SysAllocString")
)
type Element struct {
UIAutoElement *IUIAutomationElement
CurrentAcceleratorKey string
CurrentAccessKey string
CurrentAriaProperties string
CurrentAriaRole string
CurrentAutomationId string
CurrentBoundingRectangle *TagRect
CurrentClassName string
CurrentControllerFor *IUIAutomationElementArray
CurrentControlType ControlTypeId
CurrentCulture int32
CurrentDescribedBy *IUIAutomationElementArray
CurrentFlowsTo *IUIAutomationElementArray
CurrentFrameworkId string
CurrentHasKeyboardFocus int32
CurrentHelpText string
CurrentIsContentElement int32
CurrentIsControlElement int32
CurrentIsDataValidForForm int32
CurrentIsEnabled int32
CurrentIsKeyboardFocusable int32
CurrentIsOffscreen int32
CurrentIsPassword int32
CurrentIsRequiredForForm int32
CurrentItemStatus string
CurrentItemType string
CurrentLabeledBy *IUIAutomationElement
CurrentLocalizedControlType string
CurrentName string
CurrentNativeWindowHandle uintptr
CurrentOrientation OrientationType
CurrentProcessId int32
CurrentProviderDescription string
Child []*Element `json:"child,omitempty"`
}
func NewElement(uiaumation *IUIAutomationElement) *Element {
return &Element{
UIAutoElement: uiaumation,
}
}
func (e *Element) FormatString() string {
if e == nil {
return ""
}
elemType := reflect.TypeOf(e)
if elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem()
}
elemValue := reflect.ValueOf(e)
if elemValue.Kind() == reflect.Ptr {
elemValue = elemValue.Elem()
}
buf := ""
for i := 0; i < elemType.NumField(); i++ {
field := elemType.Field(i)
fieldName := field.Name
fieldValue := elemValue.Field(i)
if fieldValue.Kind() == reflect.Ptr && !fieldValue.IsNil() {
if fieldValue.CanAddr() {
v := fieldValue.Addr().Interface()
buf += fmt.Sprintf("[%s]:[%v],", fieldName, v)
}
} else {
buf += fmt.Sprintf("[%s]:[%v],", fieldName, fieldValue.Interface())
}
}
return buf
}
func (e *Element) SetUIAutomation(uiaumation *IUIAutomationElement) {
e.UIAutoElement = uiaumation
}
func (e *Element) AccleratorKey() error {
val, err := e.UIAutoElement.Get_CurrentAcceleratorKey()
e.CurrentAcceleratorKey = val
return err
}
func (e *Element) AccessKey() error {
val, err := e.UIAutoElement.Get_CurrentAccessKey()
e.CurrentAcceleratorKey = val
return err
}
func (e *Element) AriaProperties() error {
val, err := e.UIAutoElement.Get_CurrentAriaProperties()
e.CurrentAriaProperties = val
return err
}
func (e *Element) AriaRole() error {
val, err := e.UIAutoElement.Get_CurrentAriaRole()
e.CurrentAriaRole = val
return err
}
func (e *Element) AutomationId() error {
val, err := e.UIAutoElement.Get_CurrentAutomationId()
e.CurrentAutomationId = val
return err
}
func (e *Element) BoundingRectangle() {
val := e.UIAutoElement.Get_CurrentBoundingRectangle()
e.CurrentBoundingRectangle = val
}
func (e *Element) ClassName() error {
val, err := e.UIAutoElement.Get_CurrentClassName()
e.CurrentClassName = val
return err
}
func (e *Element) ControllerFor() {
val := e.UIAutoElement.Get_CurrentControllerFor()
e.CurrentControllerFor = val
}
func (e *Element) ControlType() {
val := e.UIAutoElement.Get_CurrentControlType()
e.CurrentControlType = val
}
func (e *Element) Culture() {
val := e.UIAutoElement.Get_CurrentCulture()
e.CurrentCulture = val
}
func (e *Element) DescribedBy() {
val := e.UIAutoElement.Get_CurrentDescribedBy()
e.CurrentDescribedBy = val
}
func (e *Element) FlowsTo() {
val := e.UIAutoElement.Get_CurrentFlowsTo()
e.CurrentFlowsTo = val
}
func (e *Element) FrameworkId() error {
val, err := e.UIAutoElement.Get_CurrentFrameworkId()
e.CurrentFrameworkId = val
return err
}
func (e *Element) HasKeyboardFocus() {
val := e.UIAutoElement.Get_CurrentHasKeyboardFocus()
e.CurrentHasKeyboardFocus = val
}
func (e *Element) HelpText() error {
val, err := e.UIAutoElement.Get_CurrentHelpText()
e.CurrentHelpText = val
return err
}
func (e *Element) IsControlElement() {
val := e.UIAutoElement.Get_CurrentIsControlElement()
e.CurrentIsControlElement = val
}
func (e *Element) IsContentElement() {
val := e.UIAutoElement.Get_CurrentIsContentElement()
e.CurrentIsContentElement = val
}
func (e *Element) IsDataValidForForm() {
val := e.UIAutoElement.Get_CurrentIsDataValidForForm()
e.CurrentIsDataValidForForm = val
}
func (e *Element) IsEnabled() {
val := e.UIAutoElement.Get_CurrentIsEnabled()
e.CurrentIsEnabled = val
}
func (e *Element) IsKeyboardFocusable() {
val := e.UIAutoElement.Get_CurrentIsKeyboardFocusable()
e.CurrentIsKeyboardFocusable = val
}
func (e *Element) IsOffscreen() {
val := e.UIAutoElement.Get_CurrentIsOffscreen()
e.CurrentIsOffscreen = val
}
func (e *Element) IsPassword() {
val := e.UIAutoElement.Get_CurrentIsPassword()
e.CurrentIsPassword = val
}
func (e *Element) IsRequiredForForm() {
val := e.UIAutoElement.Get_CurrentIsRequiredForForm()
e.CurrentIsRequiredForForm = val
}
func (e *Element) ItemStatus() error {
val, err := e.UIAutoElement.Get_CurrentItemStatus()
e.CurrentItemStatus = val
return err
}
func (e *Element) ItemType() error {
val, err := e.UIAutoElement.Get_CurrentItemType()
e.CurrentItemType = val
return err
}
func (e *Element) LabeledBy() {
val := e.UIAutoElement.Get_CurrentLabeledBy()
e.CurrentLabeledBy = val
}
func (e *Element) LocalizedControlType() error {
val, err := e.UIAutoElement.Get_CurrentLocalizedControlType()
e.CurrentLocalizedControlType = val
return err
}
func (e *Element) Name() error {
val, err := e.UIAutoElement.Get_CurrentName()
e.CurrentName = val
return err
}
func (e *Element) NativeWindowHandle() {
val := e.UIAutoElement.Get_CurrentNativeWindowHandle()
e.CurrentNativeWindowHandle = val
}
func (e *Element) Orientation() {
val := e.UIAutoElement.Get_CurrentOrientation()
e.CurrentOrientation = val
}
func (e *Element) ProcessId() {
val := e.UIAutoElement.Get_CurrentProcessId()
e.CurrentProcessId = val
}
func (e *Element) ProviderDescription() error {
val, err := e.UIAutoElement.Get_CurrentProviderDescription()
e.CurrentProviderDescription = val
return err
}
type SearchFunc func(elem *Element) bool
func SearchElem(elem *Element, searchFunc SearchFunc) *Element {
if elem == nil || searchFunc == nil {
return nil
}
if searchFunc(elem) {
return elem
}
for _, childElem := range elem.Child {
if found := SearchElem(childElem, searchFunc); found != nil {
return found
}
}
return nil
}
func FindElems(elem *Element, searchFunc SearchFunc) (elems []*Element) {
if searchFunc(elem) {
elems = append(elems, elem)
}
for _, childElem := range elem.Child {
if found := FindElems(childElem, searchFunc); found != nil {
elems = append(elems, found...)
}
}
return elems
}
func bstr2str(bstr uintptr) string {
return syscall.UTF16ToString((*[1 << 30]uint16)(unsafe.Pointer(bstr))[:])
}
func string2Bstr(str string) (uintptr, error) {
// 将Go字符串转换为UTF-16编码
utf16Str, err := syscall.UTF16PtrFromString(str)
if err != nil {
return 0, err
}
bstrPtr, _, _ := procSysAllocString.Call(
uintptr(unsafe.Pointer(utf16Str)),
)
if bstrPtr == 0 {
return 0, ErrorBstrPointerNil
}
return bstrPtr, nil
}
func TraverseUIElementTree(ppv *IUIAutomation, root *IUIAutomationElement) *Element {
return traverseUIElementTree(ppv, root)
}
const (
SetUIAutomation = "SetUIAutomation"
)
func traverseUIElementTree(ppv *IUIAutomation, root *IUIAutomationElement) *Element {
condition := CreateTrueCondition(ppv)
elementArr, _ := FindAll(root, condition)
arrLen := Get_Length(elementArr)
newElement := NewElement(root)
rVal := reflect.ValueOf(newElement)
rTyp := rVal.Type()
for i := 0; i < rVal.NumMethod(); i++ {
method := rVal.Method(i)
if rTyp.Method(i).Name == SetUIAutomation {
continue
}
method.Call(nil)
}
for i := 0; i < int(arrLen); i++ {
elem, _ := GetElement(elementArr, int32(i))
childElem := traverseUIElementTree(ppv, elem)
newElement.Child = append(newElement.Child, childElem)
}
return newElement
}
func TreeString(root *Element, level int) {
if root == nil {
return
}
fmt.Printf("%s- %v\n", getIndentation(level), root)
for _, child := range root.Child {
TreeString(child, level+1)
}
}
func getIndentation(level int) string {
return strings.Repeat(" ", level)
}