forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchWindow.vb
2206 lines (1798 loc) · 103 KB
/
SearchWindow.vb
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
' *****************************************************************************************
' * File: SearchWindow.cs
' *
' * Description:
' * This sample shows how User Interface (UI) Automation clients
' * can use UI Automation to do the following:
' * 1. Find an AutomationElement
' * 2. Get Supported Control Patterns
' * 3. Register for an Event
' * 4. Get Property Values
' *
' *****************************************************************************************
' *****************************************************************************************
' * File: FindText.cs
' *
' * Description:
' * This sample opens a 'canned' text file (Text.txt) in Notepad and shows how to use
' * UI Automation to find/select text and track text selection changes in the Notepad instance.
' *
' * Text.txt should be automatically copied to the same folder as the executable when
' * you build the sample. You may have to manually copy this file if you receive an error
' * stating the file cannot be found.
' *
' * Programming Elements:
' * This sample demonstrates the following UI Automation programming elements from...
' *
' * System.Windows.Automation Namespace:
' * Automation Class
' * AddAutomationEventHandler
' * AddAutomationPropertyChangedEventHandler
' * WindowPattern Class
' * WindowClosedEvent field
' * AutomationPattern Class
' * AutomationEventHandler Delegate
' * AutomationElement Class
' * RootElement property
' * GetCurrentPropertyValue method
' * BoundingRectangleProperty field
' * GetCurrentPattern method
' * FromHandle method
' * ControlTypeProperty field
' * FindFirst method
' * SetFocus method
' * TreeScope Enumeration
' * Element member
' * Descendants member
' * ControlType Class
' * Document field
' * TextPattern Class
' * Pattern field
' * TextSelectionChangedEvent field
' * SupportsTextSelection property
' * DocumentRange property
' * ValuePattern Class
' * Pattern field
' * ValueProperty field
' * AutomationPropertyChangedEventHandler Delegate
' *
' * System.Windows.Automation.Searcher Namespace:
' * PropertyCondition Class
' *
' * System.Windows.Automation.Text Namespace:
' * TextPatternRange Class
' * FindText method
' * Select method
' * TextPatternRangeEndpoint Enumeration
' *
' *
' * This file is part of the Microsoft .NET Framework SDK Code Samples.
' *
' * Copyright (C) Microsoft Corporation. All rights reserved.
' *
' * This source code is intended only as a supplement to Microsoft
' * Development Tools and/or on-line documentation. See these other
' * materials for detailed information regarding Microsoft code samples.
' *
' * THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
' * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
' * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' * PARTICULAR PURPOSE.
' *
' ******************************************************************************************
Imports System.Windows
Imports System.Windows.Documents
Imports System.Windows.Automation
Imports System.Windows.Controls
Imports System.Diagnostics
Imports System.ComponentModel
Imports System.Threading
Imports System.IO
Imports System.Windows.Automation.Text
Imports System.Windows.Threading
Namespace SDKSample
Public Class SearchWindow
Private _window As Window
Private _dp As DockPanel
Private _root As AutomationElement
Private _btnApp As Button
Private _txtAppResult As TextBlock
Private _tbSearch As TextBox
Private _btnSearch As Button
Private _cbSearch As CheckBox
Private _txtSearchResult As TextBlock
Private _txtHighlighted As TextBlock
Private _txtHighlightResult As TextBlock
Private targetTextPattern As TextPattern
Private _tprDoc As TextPatternRange
Private _tprSearch As TextPatternRange
Private _bBOD As Boolean
Private _bBackward As Boolean
Private _sApplication As String = "WordPad.exe"
Public Event BtnAppClick(ByVal Status As String)
' Constructor
Public Sub SearchWindow()
' Specs for Window
Dim wHeight As Double = 400
Dim wWidth As Double = 400
' Specs for TextBox
Dim tbMaxlines As Integer = 1
Dim tbMaxlength As Integer = 25
' Specs for Buttons
Dim bWidth As Double = 100
' Instantiate our window and set location and size
_window = New Window()
_window.Height = wHeight
_window.Width = wWidth
_window.Left = SystemParameters.WorkArea.Left
_window.Top = SystemParameters.WorkArea.Top
_window.Title = "Find Text"
_window.WindowStyle = WindowStyle.ToolWindow
_btnApp = New Button()
_btnApp.Width = bWidth
_btnApp.Content = "Start " + _sApplication
_btnApp.IsEnabled = True
AddHandler _btnApp.Click, AddressOf btnApp_Click
_txtAppResult = New TextBlock()
_txtAppResult.Text = ""
_txtAppResult.HorizontalAlignment = HorizontalAlignment.Center
_txtAppResult.Margin = New Thickness(0, 10, 0, 20)
Dim sp As StackPanel = New StackPanel()
sp.Orientation = Orientation.Horizontal
sp.HorizontalAlignment = HorizontalAlignment.Center
Dim txt1 As TextBlock = New TextBlock()
txt1.Text = "Search for: "
txt1.VerticalAlignment = VerticalAlignment.Center
_tbSearch = New TextBox()
_tbSearch.MaxLines = tbMaxlines
_tbSearch.MaxLength = tbMaxlength
_tbSearch.IsEnabled = False
_tbSearch.Margin = New Thickness(10, 0, 10, 0)
AddHandler _tbSearch.TextChanged, AddressOf tbSearch_TextChanged
_btnSearch = New Button()
_btnSearch.Width = bWidth
_btnSearch.Content = "Search"
_btnSearch.IsEnabled = False
AddHandler _btnSearch.Click, AddressOf btnSearch_Click
_cbSearch = New CheckBox()
_cbSearch.VerticalAlignment = VerticalAlignment.Center
_cbSearch.IsEnabled = False
_cbSearch.Margin = New Thickness(10, 0, 0, 0)
'cbSearch.IsCheckedChanged += new RoutedPropertyChangedEventHandler<bool?>(cbSearch_IsCheckedChanged);
AddHandler _cbSearch.Unchecked, AddressOf cbSearch_IsCheckedChanged
AddHandler _cbSearch.Checked, AddressOf cbSearch_IsCheckedChanged
Dim txt2 As TextBlock = New TextBlock()
txt2.Text = "Reverse"
txt2.VerticalAlignment = VerticalAlignment.Center
sp.Children.Add(txt1)
sp.Children.Add(_tbSearch)
sp.Children.Add(_btnSearch)
sp.Children.Add(_cbSearch)
sp.Children.Add(txt2)
_txtSearchResult = New TextBlock()
_txtSearchResult.Text = ""
_txtSearchResult.HorizontalAlignment = HorizontalAlignment.Center
_txtSearchResult.Margin = New Thickness(0, 10, 0, 20)
_txtHighlighted = New TextBlock()
_txtHighlighted.Text = "Currently highlighted:"
_txtHighlightResult = New TextBlock()
_txtHighlightResult.Text = ""
_txtHighlightResult.HorizontalAlignment = HorizontalAlignment.Left
_txtHighlightResult.Margin = New Thickness(0, 10, 0, 20)
' Create a DockPanel to hold output
_dp = New DockPanel()
_dp.Width = wWidth
_dp.Height = wHeight
_dp.LastChildFill = False
DockPanel.SetDock(_btnApp, Dock.Top)
_dp.Children.Add(_btnApp)
DockPanel.SetDock(_txtAppResult, Dock.Top)
_dp.Children.Add(_txtAppResult)
DockPanel.SetDock(sp, Dock.Top)
_dp.Children.Add(sp)
DockPanel.SetDock(_txtSearchResult, Dock.Top)
_dp.Children.Add(_txtSearchResult)
DockPanel.SetDock(_txtHighlighted, Dock.Top)
_dp.Children.Add(_txtHighlighted)
DockPanel.SetDock(_txtHighlightResult, Dock.Top)
_dp.Children.Add(_txtHighlightResult)
_window.Content = _dp
_window.Show()
End Sub
Private Sub cbSearch_IsCheckedChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
_bBackward = _cbSearch.IsChecked
' Reset the document range
' <Snippet2050>
_tprDoc = targetTextPattern.DocumentRange
' </Snippet2050>
' Re-enable search button if necessary (eod reached)
_btnSearch.IsEnabled = True
End Sub
' Reset all controls if user changes search text
Private Sub tbSearch_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
_btnSearch.IsEnabled = True
_tprDoc = targetTextPattern.DocumentRange
End Sub
' Start the text application that you are going to use for the TextPattern sample
Private Sub btnApp_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
' Start notepad.exe and use it as your root element.
' For performance reasons, it's not a good idea to start searching for UI from
' the root unless the UI you are looking for is very near the root.
Dim sFile As String = System.Windows.Forms.Application.StartupPath + "\\" + "Text.txt"
_root = StartApp(_sApplication, sFile)
If (_root Is Nothing) Then
_btnApp.IsEnabled = False
Return
End If
FindEdit(_root, _sApplication, sFile)
End Sub
Private Function StartApp(ByVal app As String, ByVal args As String) As AutomationElement
Try
If (File.Exists(args)) Then
' Start application.
Dim p As Process = Process.Start(app, args)
' Give application a second to start up.
Thread.Sleep(1000)
' Return the AutomationElement
Return (AutomationElement.FromHandle(p.MainWindowHandle))
Else
Throw New FileNotFoundException(args + " not found.")
End If
Catch [error] As Exception
Output([error].ToString())
Return Nothing
End Try
End Function
' Initialize our TextPattern and event handlers
Private Sub FindEdit(ByVal target As AutomationElement, ByVal app As String, ByVal file As String)
Try
' <Snippet2037>
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
' target --> The root AutomationElement.
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim o As Object = textProvider.GetCurrentPattern(TextPattern.Pattern)
targetTextPattern = CType(o, TextPattern)
If (targetTextPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
' </Snippet2037>
_txtAppResult.Text = app + " started and identified in the UI Automation tree."
_btnSearch.IsEnabled = True
_tbSearch.IsEnabled = True
_btnApp.IsEnabled = False
_cbSearch.IsEnabled = True
' Initialize the document range for the text of the document
_tprDoc = targetTextPattern.DocumentRange
' Beginning of document range
_bBOD = True
' Search starts at end of doc and goes backward
_bBackward = False
' <Snippet2052>
Dim ehTextChanged As AutomationEventHandler = New AutomationEventHandler(AddressOf onTextChange)
System.Windows.Automation.Automation.AddAutomationEventHandler(TextPattern.TextChangedEvent, textProvider, TreeScope.Element, ehTextChanged)
' </Snippet2052>
' <Snippet2042>
Dim ehTextSelection As AutomationEventHandler = New AutomationEventHandler(AddressOf onTextSelectionChange)
System.Windows.Automation.Automation.AddAutomationEventHandler(TextPattern.TextSelectionChangedEvent, textProvider, TreeScope.Element, ehTextSelection)
' </Snippet2042>
Dim ehClose As AutomationEventHandler = New AutomationEventHandler(AddressOf onNotepadClose)
System.Windows.Automation.Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, target, TreeScope.Element, ehClose)
Catch [error] As Win32Exception
Output([error].ToString())
Output(app + " not found.")
Catch [error] As Exception
Output([error].ToString())
End Try
End Sub
' Handle the ValueChange event
Private Sub onTextChange(ByVal sender As Object, ByVal e As AutomationEventArgs)
_tprDoc = targetTextPattern.DocumentRange
End Sub
' Handle the TextSelectChange event on the WCP thread
Private Sub onTextSelectionChange(ByVal sender As Object, ByVal e As AutomationEventArgs)
_window.Dispatcher.BeginInvoke(DispatcherPriority.Background, New DispatcherOperationCallback(AddressOf ChangeSelection), Nothing)
End Sub
' The delegate for the onTextSelectionChange event
Private Function ChangeSelection(ByVal arg As Object) As Object
Dim currentSelection As TextPatternRange() = targetTextPattern.GetSelection()
Dim selectedText As String = currentSelection(0).GetText(-1)
Dim oAttribute As Object = currentSelection(0).GetAttributeValue(TextPattern.FontNameAttribute)
If (oAttribute Is TextPattern.MixedAttributeValue) Then
' Fontface: Mixed
Else
' Fontface: _oAttribute.ToString()
End If
_txtHighlightResult.Text = selectedText
selectedText = currentSelection(0).GetText(-1)
Return (Nothing)
End Function
' Handle the WindowClose event for the instance of Notepad on the WCP thread
Private Sub onNotepadClose(ByVal sender As Object, ByVal e As AutomationEventArgs)
_window.Dispatcher.BeginInvoke(DispatcherPriority.Background, New DispatcherOperationCallback(AddressOf CloseApp), Nothing)
End Sub
' The delegate for the Notepad close event
Private Function CloseApp(ByVal arg As Object) As Object
_window.Close()
Return (Nothing)
End Function
' Find the text specified in the text box
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Try
' Check if search text entered
If (_tbSearch.Text.Trim() = "") Then
_txtSearchResult.Text = "No search criteria."
Return
End If
' Check if the TextPattern supports text selection
If (targetTextPattern.SupportedTextSelection = SupportedTextSelection.None) Then
_txtSearchResult.Text = "Unable to select text."
End If
' Move the TextPatternRange endpoints for 'Highlight Next' functionality
Dim currentSelection As TextPatternRange() = targetTextPattern.GetSelection()
If Not currentSelection Is Nothing Then
If (_bBackward) Then
_tprDoc.MoveEndpointByRange(TextPatternRangeEndpoint.End, CurrentSelection(0), TextPatternRangeEndpoint.Start)
Else
_tprDoc.MoveEndpointByRange(TextPatternRangeEndpoint.Start, CurrentSelection(0), TextPatternRangeEndpoint.End)
End If
_btnSearch.Content = "Highlight next"
End If
' Find the text specified in the Search textbox
' backward = false -- search from the start of the document range
' ignoreCase = false -- search is case sensitive
_tprSearch = _tprDoc.FindText(_tbSearch.Text, _bBackward, False)
If (_tprSearch Is Nothing) Then
If Not _bBOD Then
_txtSearchResult.Text = "End of document reached."
_btnSearch.IsEnabled = False
Else
_txtSearchResult.Text = "Text not found."
End If
Return
End If
'root.SetFocus()
_tprSearch.Select()
' No longer at the beginning of the TextPatternRange
_bBOD = False
' Scroll the selection into view, aligning with top of viewport
_tprSearch.ScrollIntoView(True)
_txtSearchResult.Text = "Text found."
Catch [error] As Exception
Output([error].ToString())
End Try
End Sub
Private Sub Output(ByVal s As String)
MessageBox.Show(s)
End Sub
' <Snippet2000>
Private Sub GetAnimationStyleAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.AnimationStyleAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed animation styles.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2000>
' <Snippet2001>
Private Sub GetBackgroundColorAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.BackgroundColorAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed background colors.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2001>
' <Snippet2002>
Private Sub GetBulletStyleAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.BulletStyleAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed bullet styles.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2002>
' <Snippet2003>
Private Sub GetCapStyleAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.CapStyleAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed cap styles.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2003>
'' <Snippet2004>
'Private Sub GetCompositionStateAttribute()
' ' Start application.
' Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' ' target --> The root AutomationElement.
' Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' ' Specify the control type we're looking for, in this case 'Document'
' Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
' Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
' Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
' If (textpatternPattern Is Nothing) Then
' Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
' Return
' End If
' Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.CompositionStateAttribute)
' If (oAttribute = TextPattern.MixedAttributeValue) Then
' Console.WriteLine("Mixed composition state.")
' Else
' Console.WriteLine(oAttribute.ToString())
' End If
'End Sub
'' </Snippet2004>
' <Snippet2005>
Private Sub GetCultureAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.CultureAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed culture info.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2005>
' <SnippetStartTarget>
''' -------------------------------------------------------------------
''' <summary>
''' Starts the target application and returns the AutomationElement
''' obtained from the targets window handle.
''' </summary>
''' <param name="exe">
''' The target application.
''' </param>
''' <param name="filename">
''' The text file to be opened in the target application
''' </param>
''' <returns>
''' An AutomationElement representing the target application.
''' </returns>
''' -------------------------------------------------------------------
Private Function StartTarget( _
ByVal exe As String, ByVal filename As String) As AutomationElement
' Start text editor and load with a text file.
Dim p As Process = Process.Start(exe, filename)
' targetApp --> the root AutomationElement.
Dim targetApp As AutomationElement
targetApp = AutomationElement.FromHandle(p.MainWindowHandle)
Return targetApp
End Function
' </SnippetStartTarget>
' <SnippetGetTextElement>
''' -------------------------------------------------------------------
''' <summary>
''' Obtain the text control of interest from the target application.
''' </summary>
''' <param name="targetApp">
''' The target application.
''' </param>
''' <returns>
''' An AutomationElement. representing a text control.
''' </returns>
''' -------------------------------------------------------------------
Private Function GetTextElement(ByVal targetApp As AutomationElement) As AutomationElement
' The control type we're looking for; in this case 'Document'
Dim cond1 As PropertyCondition = _
New PropertyCondition( _
AutomationElement.ControlTypeProperty, _
ControlType.Document)
' The control pattern of interest; in this case 'TextPattern'.
Dim cond2 As PropertyCondition = _
New PropertyCondition( _
AutomationElement.IsTextPatternAvailableProperty, _
True)
Dim textCondition As AndCondition = New AndCondition(cond1, cond2)
Dim targetTextElement As AutomationElement = _
targetApp.FindFirst(TreeScope.Descendants, textCondition)
' If targetText is null then a suitable text control was not found.
Return targetTextElement
End Function
' </SnippetGetTextElement>
' <SnippetFontName>
''' -------------------------------------------------------------------
''' <summary>
''' Outputs the FontNameAttribute value for a range of text.
''' </summary>
''' <param name="targetTextElement">
''' The AutomationElement. that represents the text provider.
''' </param>
''' -------------------------------------------------------------------
Private Sub GetFontNameAttribute( _
ByVal targetTextElement As AutomationElement)
Dim targetTextPattern As TextPattern = _
DirectCast(targetTextElement.GetCurrentPattern( _
TextPattern.Pattern), TextPattern)
If (targetTextPattern Is Nothing) Then
' Target control doesn't support TextPattern.
Return
End If
' If the target control doesn't support selection then return.
' Otherwise, get the text attribute for the selected text.
' If there are currently no selections then the text attribute
' will be obtained from the insertion point.
Dim textRanges() As TextPatternRange
If (targetTextPattern.SupportedTextSelection = SupportedTextSelection.None) Then
Return
Else
textRanges = targetTextPattern.GetSelection()
End If
Dim textRange As TextPatternRange
For Each textRange In textRanges
Dim textAttribute As Object = _
textRange.GetAttributeValue( _
TextPattern.FontNameAttribute)
If (textAttribute = TextPattern.MixedAttributeValue) Then
' Returns MixedAttributeValue if the value of the
' specified attribute varies over the text range.
Console.WriteLine("Mixed fonts.")
ElseIf (textAttribute = AutomationElement.NotSupported) Then
' Returns NotSupported if the specified attribute is
' not supported by the provider or the control.
Console.WriteLine( _
"FontNameAttribute not supported by provider.")
Else
Console.WriteLine(textAttribute.ToString())
End If
Next
End Sub
' </SnippetFontName>
' <Snippet2007>
Private Sub GetFontSizeAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.FontSizeAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed font sizes.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2007>
' <Snippet2008>
Private Sub GetFontWeightAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.FontWeightAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed font weights.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2008>
' <Snippet2009>
Private Sub GetForegroundColorAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.ForegroundColorAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed foreground colors.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2009>
'' <Snippet2010>
'Private Sub GetHeadingLevelAttribute()
' ' Start application.
' Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' ' target --> The root AutomationElement.
' Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' ' Specify the control type we're looking for, in this case 'Document'
' Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
' Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
' Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
' If (textpatternPattern Is Nothing) Then
' Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
' Return
' End If
' Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.HeadingLevelAttribute)
' If (oAttribute = TextPattern.MixedAttributeValue) Then
' Console.WriteLine("Mixed heading levels.")
' Else
' Console.WriteLine(oAttribute.ToString())
' End If
'End Sub
'' </Snippet2010>
' <Snippet2011>
Private Sub GetHorizontalTextAlignmentAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.HorizontalTextAlignmentAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed horizontal alignments.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2011>
' <Snippet2012>
Private Sub GetIndentationFirstLineAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.IndentationFirstLineAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed first line indentations.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2012>
' <Snippet2013>
Private Sub GetIndentationLeadingAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.IndentationLeadingAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed leading indentations.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2013>
' <Snippet2014>
Private Sub GetIndentationTrailingAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.IndentationTrailingAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixed trailing indentations.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2014>
' <Snippet2015>
Private Sub GetIsHiddenAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.IsHiddenAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixture of hidden and visible.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2015>
' <Snippet2016>
Private Sub GetIsItalicAttribute()
' Start application.
Dim p As Process = Process.Start("Notepad.exe", "text.txt")
' target --> The root AutomationElement.
Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)
' Specify the control type we're looking for, in this case 'Document'
Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)
Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)
If (textpatternPattern Is Nothing) Then
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
Return
End If
Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.IsItalicAttribute)
If (oAttribute = TextPattern.MixedAttributeValue) Then
Console.WriteLine("Mixture of italic and non-italic.")
Else
Console.WriteLine(oAttribute.ToString())
End If
End Sub
' </Snippet2016>
'' <Snippet2017>
'Private Sub GetIsMarkedAutocorrectedAttribute()
' ' Start application.