-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplaceBrowseForFolder.ahk
1206 lines (1145 loc) · 48.1 KB
/
ReplaceBrowseForFolder.ahk
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
#Requires AutoHotkey v1.1.33+
;@Ahk2Exe-SetMainIcon ReplaceBrowseForFolder.ico
;@Ahk2Exe-SetVersion 1.2
;@Ahk2Exe-Base Unicode 32*
;@Ahk2Exe-PostExec "MPRESS.exe" "%A_WorkFileName%" -q -x, 0,, 1
;@Ahk2Exe-IgnoreBegin
; Set tray icon
Try
Menu, Tray, Icon, shell32.dll, 301
;@Ahk2Exe-IgnoreEnd
#NoEnv
#Persistent
#SingleInstance Force
;#Include <RemoteTreeViewClass>
;#Include <SelectFolderEx>
SetBatchLines -1
; Togglable dialog watcher in tray menu
MenuItemName := "Replace Browse For Folder"
Menu, Tray, Add, % MenuItemName, ToggleReplaceBrowseForFolder
ToggleReplaceBrowseForFolder(ItemName) {
Static Toggle
ReplaceBrowseForFolder(Toggle := !Toggle)
Menu, Tray, ToggleCheck, % ItemName
}
; Toggle on by default
ToggleReplaceBrowseForFolder(MenuItemName)
; or, simply enable the dialog watcher without the tray toggle
;ReplaceBrowseForFolder(true)
Return
; For testing/demo purposes
/*
; Start up a "Browse For Folder" dialog
F1::
FileSelectFolder, SelectedFolder
If !ErrorLevel
MsgBox % "SelectedFolder: " SelectedFolder
Return
; Manually trigger the set path process
F2::
DialogHwnd := WinExist("Browse For Folder ahk_class #32770")
If (SelectedPath := SelectFolderEx(, , DialogHwnd))
SetPathForBrowseForFolder(SelectedPath, DialogHwnd)
Return
*/
ReplaceBrowseForFolder(Params*) {
Static EVENT_OBJECT_SHOW := 0x8002
, OBJID_WINDOW := 0
, INDEXID_CONTAINER := 0
, hHook := 0
If IsObject(Params) {
Return hHook := Params[1]
? DllCall("SetWinEventHook", "Int", EVENT_OBJECT_SHOW
, "Int", EVENT_OBJECT_SHOW, "Ptr", 0, "Ptr"
, RegisterCallback(A_ThisFunc)
, "Int", 0, "Int", 0, "Int", 0, "Ptr")
: DllCall("UnhookWinEvent", "Ptr", hHook), DllCall("CoUninitialize")
} Else {
hwnd := NumGet(params+0, 2*A_PtrSize, "Ptr")
idObject := NumGet(params+0, 3*A_PtrSize, "Int")
idChild := NumGet(params+0, 4*A_PtrSize, "Int")
If (idObject != OBJID_WINDOW || idChild != INDEXID_CONTAINER)
Return
WinGetClass wndClass, % "ahk_id" hwnd
If (wndClass != "#32770")
Return
WinGet CtlList, ControlList, % "ahk_id" hwnd
If !( InStr(CtlList, "SHBrowseForFolder ShellNameSpace Control")
|| CtlList = "Static1`nStatic2`nSysTreeView321`nButton1`nButton2" )
Return
If (SelectedPath := SelectFolderEx(, , hwnd))
SetPathForBrowseForFolder(SelectedPath, hwnd)
Else
WinClose % "ahk_id" hwnd
}
}
SetPathForBrowseForFolder(TargetPath, DialogHwnd:=0x0) {
If !InStr(FileExist(TargetPath), "D") {
Msgbox, 0x10, Error, % "Directory does not exist: `n" TargetPath
Return
}
PathTree := GetExplorerPathTree(TargetPath)
If !DialogHwnd
DialogHwnd := WinExist("A")
Else If !WinActive(DialogHwnd) {
WinActivate % "ahk_id" DialogHwnd
WinWaitActive % "ahk_id" DialogHwnd
}
ControlGet, hctl, Hwnd,, SysTreeView321, % "ahk_id" DialogHwnd
RTV := new RemoteTreeview(hctl)
For Index, Level in PathTree
{
T_Start := A_TickCount
Loop {
hItem := RTV.GetHandleByText(Level)
If hItem {
Break
} Else If (A_TickCount - T_Start >= 60000) {
Msgbox, 0x1004, , % "Been waiting for a minute. Abort?"
IfMsgBox, Yes
Return
Else
T_Start := A_TickCount
} Else If !WinExist("ahk_id" DialogHwnd) {
Return
}
}
RTV.SetSelection(hItem, 0)
RTV.Expand(hItem)
}
Loop
Controlsend, , {Enter}, % "ahk_id " DialogHwnd
Until !WinExist("ahk_id" DialogHwnd)
}
GetExplorerPathTree(FolderPath) {
Shell := ComObjCreate("Shell.Application")
Folder := Shell.NameSpace(FolderPath)
PathTree := []
Loop
PathTree.InsertAt(1, Folder.Self.Name)
Until !(Folder := Folder.ParentFolder)
Return PathTree
}
; ==================================================================================================================================
; Shows a dialog to select a folder.
; Depending on the OS version the function will use either the built-in FileSelectFolder command (XP and previous)
; or the Common Item Dialog (Vista and later).
; Parameter:
; StartingFolder - the full path of a folder which will be preselected.
; Prompt - a text used as window title (Common Item Dialog) or as text displayed withing the dialog.
; ---------------- Common Item Dialog only:
; OwnerHwnd - HWND of the Gui which owns the dialog. If you pass a valid HWND the dialog will become modal.
; BtnLabel - a text to be used as caption for the apply button.
; Return values:
; On success the function returns the full path of selected folder; otherwise it returns an empty string.
; MSDN:
; Common Item Dialog -> msdn.microsoft.com/en-us/library/bb776913%28v=vs.85%29.aspx
; IFileDialog -> msdn.microsoft.com/en-us/library/bb775966%28v=vs.85%29.aspx
; IShellItem -> msdn.microsoft.com/en-us/library/bb761140%28v=vs.85%29.aspx
; ==================================================================================================================================
SelectFolderEx(StartingFolder := "", Prompt := "", OwnerHwnd := 0, OkBtnLabel := "") {
Static OsVersion := DllCall("GetVersion", "UChar")
, IID_IShellItem := 0
, InitIID := VarSetCapacity(IID_IShellItem, 16, 0)
& DllCall("Ole32.dll\IIDFromString", "WStr", "{43826d1e-e718-42ee-bc55-a1e261c37bfe}", "Ptr", &IID_IShellItem)
, Show := A_PtrSize * 3
, SetOptions := A_PtrSize * 9
, SetFolder := A_PtrSize * 12
, SetTitle := A_PtrSize * 17
, SetOkButtonLabel := A_PtrSize * 18
, GetResult := A_PtrSize * 20
SelectedFolder := ""
If (OsVersion < 6) { ; IFileDialog requires Win Vista+, so revert to FileSelectFolder
FileSelectFolder, SelectedFolder, *%StartingFolder%, 3, %Prompt%
Return SelectedFolder
}
OwnerHwnd := DllCall("IsWindow", "Ptr", OwnerHwnd, "UInt") ? OwnerHwnd : 0
If !(FileDialog := ComObjCreate("{DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7}", "{42f85136-db7e-439c-85f1-e4075d135fc8}"))
Return ""
VTBL := NumGet(FileDialog + 0, "UPtr")
; FOS_CREATEPROMPT | FOS_NOCHANGEDIR | FOS_PICKFOLDERS
DllCall(NumGet(VTBL + SetOptions, "UPtr"), "Ptr", FileDialog, "UInt", 0x00002028, "UInt")
If (StartingFolder <> "")
If !DllCall("Shell32.dll\SHCreateItemFromParsingName", "WStr", StartingFolder, "Ptr", 0, "Ptr", &IID_IShellItem, "PtrP", FolderItem)
DllCall(NumGet(VTBL + SetFolder, "UPtr"), "Ptr", FileDialog, "Ptr", FolderItem, "UInt")
If (Prompt <> "")
DllCall(NumGet(VTBL + SetTitle, "UPtr"), "Ptr", FileDialog, "WStr", Prompt, "UInt")
If (OkBtnLabel <> "")
DllCall(NumGet(VTBL + SetOkButtonLabel, "UPtr"), "Ptr", FileDialog, "WStr", OkBtnLabel, "UInt")
If !DllCall(NumGet(VTBL + Show, "UPtr"), "Ptr", FileDialog, "Ptr", OwnerHwnd, "UInt") {
If !DllCall(NumGet(VTBL + GetResult, "UPtr"), "Ptr", FileDialog, "PtrP", ShellItem, "UInt") {
GetDisplayName := NumGet(NumGet(ShellItem + 0, "UPtr"), A_PtrSize * 5, "UPtr")
If !DllCall(GetDisplayName, "Ptr", ShellItem, "UInt", 0x80028000, "PtrP", StrPtr) ; SIGDN_DESKTOPABSOLUTEPARSING
SelectedFolder := StrGet(StrPtr, "UTF-16"), DllCall("Ole32.dll\CoTaskMemFree", "Ptr", StrPtr)
ObjRelease(ShellItem)
} }
If (FolderItem)
ObjRelease(FolderItem)
ObjRelease(FileDialog)
Return SelectedFolder
}
;--------------------------------------------------------------------------------------------------
; Title: Remote TreeView class
; This class allows a script to work with TreeViews controlled by a third party process.
;
; 8/30/2012 Released for beta testing
;
; 8/31/2012 Added a wait parameter to SetSelection
; Changed name of ExpandCollapse to Expand
; Changed default WaitTime of Expand to 0
;
; 9/2/2012 Removed GetState method and replaced it with the IsBold, IsChecked, IsExpanded
; and IsSelected methods.
;
; 9/7/2012 Added Check method.
; For ease of use, changed parameters of SetSelection, Expand and IsChecked methods.
;
; 9/17/2012 Extended the "Full" option of GetNext() to allow it to transverse sub-trees.
; Given a starting node, all decendents of that node will be transversed depth
; first. Those nodes which are not descendants of the starting node will NOT be
; visited. To traverse the entire tree, including the real root, pass zero as the
; starting node.
;
; 11/02/2014 Fix for GetText and ddditional function from just me
; See http://ahkscript.org/boards/viewtopic.php?f=5&t=4998#p29339
;
class RemoteTreeView
{
; ======================================================================================================================
; Function: Constants for TreeView controls
; Version: 1.0.00.00/2012-04-01/just me
; ======================================================================================================================
; CCM_FIRST = 0x2000
; TV_FIRST = 0x1100
; TVN_FIRST = -400
; ======================================================================================================================
; Class ================================================================================================================
WC_TREEVIEW := "SysTreeView32"
; Messages =============================================================================================================
TVM_CREATEDRAGIMAGE := 0x1112 ; (TV_FIRST + 18)
TVM_DELETEITEM := 0x1101 ; (TV_FIRST + 1)
TVM_EDITLABELA := 0x110E ; (TV_FIRST + 14)
TVM_EDITLABELW := 0x1141 ; (TV_FIRST + 65)
TVM_ENDEDITLABELNOW := 0x1116 ; (TV_FIRST + 22)
TVM_ENSUREVISIBLE := 0x1114 ; (TV_FIRST + 20)
TVM_EXPAND := 0x1102 ; (TV_FIRST + 2)
TVM_GETBKCOLOR := 0x112F ; (TV_FIRST + 31)
TVM_GETCOUNT := 0x1105 ; (TV_FIRST + 5)
TVM_GETEDITCONTROL := 0x110F ; (TV_FIRST + 15)
TVM_GETEXTENDEDSTYLE := 0x112D ; (TV_FIRST + 45)
TVM_GETIMAGELIST := 0x1108 ; (TV_FIRST + 8)
TVM_GETINDENT := 0x1106 ; (TV_FIRST + 6)
TVM_GETINSERTMARKCOLOR := 0x1126 ; (TV_FIRST + 38)
TVM_GETISEARCHSTRINGA := 0x1117 ; (TV_FIRST + 23)
TVM_GETISEARCHSTRINGW := 0x1140 ; (TV_FIRST + 64)
TVM_GETITEMA := 0x110C ; (TV_FIRST + 12)
TVM_GETITEMHEIGHT := 0x111C ; (TV_FIRST + 28)
TVM_GETITEMPARTRECT := 0x1148 ; (TV_FIRST + 72) ; >= Vista
TVM_GETITEMRECT := 0x1104 ; (TV_FIRST + 4)
TVM_GETITEMSTATE := 0x1127 ; (TV_FIRST + 39)
TVM_GETITEMW := 0x113E ; (TV_FIRST + 62)
TVM_GETLINECOLOR := 0x1129 ; (TV_FIRST + 41)
TVM_GETNEXTITEM := 0x110A ; (TV_FIRST + 10)
TVM_GETSCROLLTIME := 0x1122 ; (TV_FIRST + 34)
TVM_GETSELECTEDCOUNT := 0x1146 ; (TV_FIRST + 70) ; >= Vista
TVM_GETTEXTCOLOR := 0x1120 ; (TV_FIRST + 32)
TVM_GETTOOLTIPS := 0x1119 ; (TV_FIRST + 25)
TVM_GETUNICODEFORMAT := 0x2006 ; (CCM_FIRST + 6) CCM_GETUNICODEFORMAT
TVM_GETVISIBLECOUNT := 0x1110 ; (TV_FIRST + 16)
TVM_HITTEST := 0x1111 ; (TV_FIRST + 17)
TVM_INSERTITEMA := 0x1100 ; (TV_FIRST + 0)
TVM_INSERTITEMW := 0x1142 ; (TV_FIRST + 50)
TVM_MAPACCIDTOHTREEITEM := 0x112A ; (TV_FIRST + 42)
TVM_MAPHTREEITEMTOACCID := 0x112B ; (TV_FIRST + 43)
TVM_SELECTITEM := 0x110B ; (TV_FIRST + 11)
TVM_SETAUTOSCROLLINFO := 0x113B ; (TV_FIRST + 59)
TVM_SETBKCOLOR := 0x111D ; (TV_FIRST + 29)
TVM_SETEXTENDEDSTYLE := 0x112C ; (TV_FIRST + 44)
TVM_SETIMAGELIST := 0x1109 ; (TV_FIRST + 9)
TVM_SETINDENT := 0x1107 ; (TV_FIRST + 7)
TVM_SETINSERTMARK := 0x111A ; (TV_FIRST + 26)
TVM_SETINSERTMARKCOLOR := 0x1125 ; (TV_FIRST + 37)
TVM_SETITEMA := 0x110D ; (TV_FIRST + 13)
TVM_SETITEMHEIGHT := 0x111B ; (TV_FIRST + 27)
TVM_SETITEMW := 0x113F ; (TV_FIRST + 63)
TVM_SETLINECOLOR := 0x1128 ; (TV_FIRST + 40)
TVM_SETSCROLLTIME := 0x1121 ; (TV_FIRST + 33)
TVM_SETTEXTCOLOR := 0x111E ; (TV_FIRST + 30)
TVM_SETTOOLTIPS := 0x1118 ; (TV_FIRST + 24)
TVM_SETUNICODEFORMAT := 0x2005 ; (CCM_FIRST + 5) ; CCM_SETUNICODEFORMAT
TVM_SHOWINFOTIP := 0x1147 ; (TV_FIRST + 71) ; >= Vista
TVM_SORTCHILDREN := 0x1113 ; (TV_FIRST + 19)
TVM_SORTCHILDRENCB := 0x1115 ; (TV_FIRST + 21)
; Notifications ========================================================================================================
TVN_ASYNCDRAW := -420 ; (TVN_FIRST - 20) >= Vista
TVN_BEGINDRAGA := -427 ; (TVN_FIRST - 7)
TVN_BEGINDRAGW := -456 ; (TVN_FIRST - 56)
TVN_BEGINLABELEDITA := -410 ; (TVN_FIRST - 10)
TVN_BEGINLABELEDITW := -456 ; (TVN_FIRST - 59)
TVN_BEGINRDRAGA := -408 ; (TVN_FIRST - 8)
TVN_BEGINRDRAGW := -457 ; (TVN_FIRST - 57)
TVN_DELETEITEMA := -409 ; (TVN_FIRST - 9)
TVN_DELETEITEMW := -458 ; (TVN_FIRST - 58)
TVN_ENDLABELEDITA := -411 ; (TVN_FIRST - 11)
TVN_ENDLABELEDITW := -460 ; (TVN_FIRST - 60)
TVN_GETDISPINFOA := -403 ; (TVN_FIRST - 3)
TVN_GETDISPINFOW := -452 ; (TVN_FIRST - 52)
TVN_GETINFOTIPA := -412 ; (TVN_FIRST - 13)
TVN_GETINFOTIPW := -414 ; (TVN_FIRST - 14)
TVN_ITEMCHANGEDA := -418 ; (TVN_FIRST - 18) ; >= Vista
TVN_ITEMCHANGEDW := -419 ; (TVN_FIRST - 19) ; >= Vista
TVN_ITEMCHANGINGA := -416 ; (TVN_FIRST - 16) ; >= Vista
TVN_ITEMCHANGINGW := -417 ; (TVN_FIRST - 17) ; >= Vista
TVN_ITEMEXPANDEDA := -406 ; (TVN_FIRST - 6)
TVN_ITEMEXPANDEDW := -455 ; (TVN_FIRST - 55)
TVN_ITEMEXPANDINGA := -405 ; (TVN_FIRST - 5)
TVN_ITEMEXPANDINGW := -454 ; (TVN_FIRST - 54)
TVN_KEYDOWN := -412 ; (TVN_FIRST - 12)
TVN_SELCHANGEDA := -402 ; (TVN_FIRST - 2)
TVN_SELCHANGEDW := -451 ; (TVN_FIRST - 51)
TVN_SELCHANGINGA := -401 ; (TVN_FIRST - 1)
TVN_SELCHANGINGW := -450 ; (TVN_FIRST - 50)
TVN_SETDISPINFOA := -404 ; (TVN_FIRST - 4)
TVN_SETDISPINFOW := -453 ; (TVN_FIRST - 53)
TVN_SINGLEEXPAND := -415 ; (TVN_FIRST - 15)
; Styles ===============================================================================================================
TVS_CHECKBOXES := 0x0100
TVS_DISABLEDRAGDROP := 0x0010
TVS_EDITLABELS := 0x0008
TVS_FULLROWSELECT := 0x1000
TVS_HASBUTTONS := 0x0001
TVS_HASLINES := 0x0002
TVS_INFOTIP := 0x0800
TVS_LINESATROOT := 0x0004
TVS_NOHSCROLL := 0x8000 ; TVS_NOSCROLL overrides this
TVS_NONEVENHEIGHT := 0x4000
TVS_NOSCROLL := 0x2000
TVS_NOTOOLTIPS := 0x0080
TVS_RTLREADING := 0x0040
TVS_SHOWSELALWAYS := 0x0020
TVS_SINGLEEXPAND := 0x0400
TVS_TRACKSELECT := 0x0200
; Exstyles =============================================================================================================
TVS_EX_AUTOHSCROLL := 0x0020 ; >= Vista
TVS_EX_DIMMEDCHECKBOXES := 0x0200 ; >= Vista
TVS_EX_DOUBLEBUFFER := 0x0004 ; >= Vista
TVS_EX_DRAWIMAGEASYNC := 0x0400 ; >= Vista
TVS_EX_EXCLUSIONCHECKBOXES := 0x0100 ; >= Vista
TVS_EX_FADEINOUTEXPANDOS := 0x0040 ; >= Vista
TVS_EX_MULTISELECT := 0x0002 ; >= Vista - Not supported. Do not use.
TVS_EX_NOINDENTSTATE := 0x0008 ; >= Vista
TVS_EX_NOSINGLECOLLAPSE := 0x0001 ; >= Vista - Intended for internal use; not recommended for use in applications.
TVS_EX_PARTIALCHECKBOXES := 0x0080 ; >= Vista
TVS_EX_RICHTOOLTIP := 0x0010 ; >= Vista
; Others ===============================================================================================================
; Item flags
TVIF_CHILDREN := 0x0040
TVIF_DI_SETITEM := 0x1000
TVIF_EXPANDEDIMAGE := 0x0200 ; >= Vista
TVIF_HANDLE := 0x0010
TVIF_IMAGE := 0x0002
TVIF_INTEGRAL := 0x0080
TVIF_PARAM := 0x0004
TVIF_SELECTEDIMAGE := 0x0020
TVIF_STATE := 0x0008
TVIF_STATEEX := 0x0100 ; >= Vista
TVIF_TEXT := 0x0001
; Item states
TVIS_BOLD := 0x0010
TVIS_CUT := 0x0004
TVIS_DROPHILITED := 0x0008
TVIS_EXPANDED := 0x0020
TVIS_EXPANDEDONCE := 0x0040
TVIS_EXPANDPARTIAL := 0x0080
TVIS_OVERLAYMASK := 0x0F00
TVIS_SELECTED := 0x0002
TVIS_STATEIMAGEMASK := 0xF000
TVIS_USERMASK := 0xF000
; TVITEMEX uStateEx
TVIS_EX_ALL := 0x0002 ; not documented
TVIS_EX_DISABLED := 0x0002 ; >= Vista
TVIS_EX_FLAT := 0x0001
; TVINSERTSTRUCT hInsertAfter
TVI_FIRST := -65535 ; (-0x0FFFF)
TVI_LAST := -65534 ; (-0x0FFFE)
TVI_ROOT := -65536 ; (-0x10000)
TVI_SORT := -65533 ; (-0x0FFFD)
; TVM_EXPAND wParam
TVE_COLLAPSE := 0x0001
TVE_COLLAPSERESET := 0x8000
TVE_EXPAND := 0x0002
TVE_EXPANDPARTIAL := 0x4000
TVE_TOGGLE := 0x0003
; TVM_GETIMAGELIST wParam
TVSIL_NORMAL := 0
TVSIL_STATE := 2
; TVM_GETNEXTITEM wParam
TVGN_CARET := 0x0009
TVGN_CHILD := 0x0004
TVGN_DROPHILITE := 0x0008
TVGN_FIRSTVISIBLE := 0x0005
TVGN_LASTVISIBLE := 0x000A
TVGN_NEXT := 0x0001
TVGN_NEXTSELECTED := 0x000B ; >= Vista (MSDN)
TVGN_NEXTVISIBLE := 0x0006
TVGN_PARENT := 0x0003
TVGN_PREVIOUS := 0x0002
TVGN_PREVIOUSVISIBLE := 0x0007
TVGN_ROOT := 0x0000
; TVM_SELECTITEM wParam
TVSI_NOSINGLEEXPAND := 0x8000 ; Should not conflict with TVGN flags.
; TVHITTESTINFO flags
TVHT_ABOVE := 0x0100
TVHT_BELOW := 0x0200
TVHT_NOWHERE := 0x0001
TVHT_ONITEMBUTTON := 0x0010
TVHT_ONITEMICON := 0x0002
TVHT_ONITEMINDENT := 0x0008
TVHT_ONITEMLABEL := 0x0004
TVHT_ONITEMRIGHT := 0x0020
TVHT_ONITEMSTATEICON := 0x0040
TVHT_TOLEFT := 0x0800
TVHT_TORIGHT := 0x0400
TVHT_ONITEM := 0x0046 ; (TVHT_ONITEMICON | TVHT_ONITEMLABEL | TVHT_ONITEMSTATEICON)
; TVGETITEMPARTRECTINFO partID (>= Vista)
TVGIPR_BUTTON := 0x0001
; NMTREEVIEW action
TVC_BYKEYBOARD := 0x0002
TVC_BYMOUSE := 0x0001
TVC_UNKNOWN := 0x0000
; TVN_SINGLEEXPAND return codes
TVNRET_DEFAULT := 0
TVNRET_SKIPOLD := 1
TVNRET_SKIPNEW := 2
; ======================================================================================================================
PAGE_NOACCESS := 0x01
PAGE_READONLY := 0x02
PAGE_READWRITE := 0x04
PAGE_WRITECOPY := 0x08
PAGE_EXECUTE := 0x10
PAGE_EXECUTE_READ := 0x20
PAGE_EXECUTE_READWRITE := 0x40
PAGE_EXECUTE_WRITECOPY := 0x80
PAGE_GUARD := 0x100
PAGE_NOCACHE := 0x200
PAGE_WRITECOMBINE := 0x400
MEM_COMMIT := 0x1000
MEM_RESERVE := 0x2000
MEM_DECOMMIT := 0x4000
MEM_RELEASE := 0x8000
MEM_FREE := 0x10000
MEM_PRIVATE := 0x20000
MEM_MAPPED := 0x40000
MEM_RESET := 0x80000
MEM_TOP_DOWN := 0x100000
MEM_WRITE_WATCH := 0x200000
MEM_PHYSICAL := 0x400000
MEM_ROTATE := 0x800000
MEM_LARGE_PAGES := 0x20000000
MEM_4MB_PAGES := 0x80000000
PROCESS_TERMINATE := (0x0001)
PROCESS_CREATE_THREAD := (0x0002)
PROCESS_SET_SESSIONID := (0x0004)
PROCESS_VM_OPERATION := (0x0008)
PROCESS_VM_READ := (0x0010)
PROCESS_VM_WRITE := (0x0020)
PROCESS_DUP_HANDLE := (0x0040)
PROCESS_CREATE_PROCESS := (0x0080)
PROCESS_SET_QUOTA := (0x0100)
PROCESS_SET_INFORMATION := (0x0200)
PROCESS_QUERY_INFORMATION := (0x0400)
PROCESS_SUSPEND_RESUME := (0x0800)
PROCESS_QUERY_LIMITED_INFORMATION := (0x1000)
;----------------------------------------------------------------------------------------------
; Method: __New
; Stores the TreeView's Control HWnd in the object for later use
;
; Parameters:
; TVHwnd - HWND of the TreeView control
;
; Returns:
; Nothing
;
__New(TVHwnd){
this.TVHwnd := TVHwnd
}
;----------------------------------------------------------------------------------------------
; Method: GetHandleByText
; Retrieves the currently item with a specific text
;
; Parameters:
; text - Text of the item
; index - Index of item if you do not want to use the first item
;
; Returns:
; Handle of Item
;
GetHandleByText(Text, index := 1) {
hItem := "0" ; Causes the loop's first iteration to start the search at the top of the tree.
i := 0
Loop {
hItem := this.GetNext(hItem, "Full")
if !hItem{ ; No more items in tree.
return false
}
if (this.GetText(hItem)=Text){
i++
if (index = i){
return hItem
}
}
}
return false
}
;----------------------------------------------------------------------------------------------
; Method: SetSelection
; Makes the given item selected and expanded. Optionally scrolls the
; TreeView so the item is visible.
;
; Parameters:
; pItem - Handle to the item you wish selected
; defaultAction - Determines of the default action is activated
; true : Send an Enter (default)
; false : do noting
;
; Returns:
; TRUE if successful, or FALSE otherwise
;
SetSelection(pItem, defaultAction := true){
; ORI SendMessage this.TVM_SELECTITEM, this.TVGN_CARET|this.TVSI_NOSINGLEEXPAND, pItem, , % "ahk_id " this.TVHwnd
; sle118 :
; SendMessage this.TVM_SELECTITEM, this.TVGN_FIRSTVISIBLE, pItem, , % "ahk_id " this.TVHwnd
SendMessage this.TVM_SELECTITEM, this.TVGN_CARET, pItem, , % "ahk_id " this.TVHwnd
if (defaultAction){
Controlsend, , {Enter}, % "ahk_id " this.TVHwnd
}
return ErrorLevel
}
;----------------------------------------------------------------------------------------------
; Method: SetSelectionByText
; Makes the given item selected and expanded. Optionally scrolls the
; TreeView so the item is visible.
;
; Parameters:
; text - Text of the item you wish selected
; defaultAction - Determines of the default action is activated
; true : Send an Enter (default)
; false : do noting
; index - Index of item if you do not want to use the first item
;
; Returns:
; TRUE if successful, or FALSE otherwise
;
SetSelectionByText(text, defaultAction := true, index := 1) {
; hItem := "0" ; Causes the loop's first iteration to start the search at the top of the tree.
hItem := this.GetHandleByText(text, index)
if hItem{
return this.SetSelection(hItem, defaultAction)
}
return false
}
;----------------------------------------------------------------------------------------------
; Method: GetSelection
; Retrieves the currently selected item
;
; Parameters:
; None
;
; Returns:
; Handle to the selected item if successful, Null otherwise.
;
GetSelection(){
SendMessage this.TVM_GETNEXTITEM, this.TVGN_CARET, 0, , % "ahk_id " this.TVHwnd
return ErrorLevel
}
;----------------------------------------------------------------------------------------------
; Method: GetRoot
; Retrieves the root item of the treeview
;
; Parameters:
; None
;
; Returns:
; Handle to the topmost or very first item of the tree-view control
; if successful, NULL otherwise.
;
GetRoot(){
SendMessage this.TVM_GETNEXTITEM, this.TVGN_ROOT, 0, , % "ahk_id " this.TVHwnd
return ErrorLevel
}
;----------------------------------------------------------------------------------------------
; Method: GetParent
; Retrieves an item's parent
;
; Parameters:
; pItem - Handle to the item
;
; Returns:
; Handle to the parent of the specified item. Returns
; NULL if the item being retrieved is the root node of the tree.
;
GetParent(pItem){
SendMessage this.TVM_GETNEXTITEM, this.TVGN_PARENT, pItem, , % "ahk_id " this.TVHwnd
return ErrorLevel
}
;----------------------------------------------------------------------------------------------
; Method: GetChild
; Retrieves an item's first child
;
; Parameters:
; pItem - Handle to the item
;
; Returns:
; Handle to the first Child of the specified item, NULL otherwise.
;
GetChild(pItem){
SendMessage this.TVM_GETNEXTITEM, this.TVGN_CHILD, pItem, , % "ahk_id " this.TVHwnd
return ErrorLevel
}
;----------------------------------------------------------------------------------------------
; Method: GetNext
; Returns the handle of the sibling below the specified item (or 0 if none).
;
; Parameters:
; pItem - (Optional) Handle to the item
;
; flag - (Optional) "FULL" or "F"
;
; Returns:
; This method has the following modes:
; 1. When all parameters are omitted, it returns the handle
; of the first/top item in the TreeView (or 0 if none).
;
; 2. When the only first parameter (pItem) is present, it returns the
; handle of the sibling below the specified item (or 0 if none).
; If the first parameter is 0, it returns the handle of the first/top
; item in the TreeView (or 0 if none).
;
; 3. When the second parameter is "Full" or "F", the first time GetNext()
; is called the hItem passed is considered the "root" of a sub-tree that
; will be transversed in a depth first manner. No nodes except the
; decendents of that "root" will be visited. To traverse the entire tree,
; including the real root, pass zero in the first call.
;
; When all descendants have benn visited, the method returns zero.
;
; Example:
; hItem = 0 ; Start the search at the top of the tree.
; Loop
; {
; hItem := MyTV.GetNext(hItem, "Full")
; if not hItem ; No more items in tree.
; break
; ItemText := MyTV.GetText(hItem)
; MsgBox The next Item is %hItem%, whose text is "%ItemText%".
; }
;
GetNext(pItem = 0, flag = ""){
static Root := -1
if (RegExMatch(flag, "i)^\s*(F|Full)\s*$")) {
if (Root = -1) {
Root := pItem
}
SendMessage this.TVM_GETNEXTITEM, this.TVGN_CHILD, pItem, , % "ahk_id " this.TVHwnd
if (ErrorLevel = 0) {
SendMessage this.TVM_GETNEXTITEM, this.TVGN_NEXT, pItem, , % "ahk_id " this.TVHwnd
if (ErrorLevel = 0) {
Loop {
SendMessage this.TVM_GETNEXTITEM, this.TVGN_PARENT, pItem, , % "ahk_id " this.TVHwnd
if (ErrorLevel = Root) {
Root := -1
return 0
}
pItem := ErrorLevel
SendMessage this.TVM_GETNEXTITEM, this.TVGN_NEXT, pItem, , % "ahk_id " this.TVHwnd
} until ErrorLevel
}
}
return ErrorLevel
}
Root := -1
if (!pItem)
SendMessage this.TVM_GETNEXTITEM, this.TVGN_ROOT, 0, , % "ahk_id " this.TVHwnd
else
SendMessage this.TVM_GETNEXTITEM, this.TVGN_NEXT, pItem, , % "ahk_id " this.TVHwnd
return ErrorLevel
}
;----------------------------------------------------------------------------------------------
; Method: GetPrev
; Returns the handle of the sibling above the specified item (or 0 if none).
;
; Parameters:
; pItem - Handle to the item
;
; Returns:
; Handle of the sibling above the specified item (or 0 if none).
;
GetPrev(pItem){
SendMessage this.TVM_GETNEXTITEM, this.TVGN_PREVIOUS, pItem, , % "ahk_id " this.TVHwnd
return ErrorLevel
}
;----------------------------------------------------------------------------------------------
; Method: Expand
; Expands or collapses the specified tree node
;
; Parameters:
; pItem - Handle to the item
;
; flag - Determines whether the node is expanded or collapsed.
; true : expand the node (default)
; false : collapse the node
;
;
; Returns:
; Nonzero if the operation was successful, or zero otherwise.
;
Expand(pItem, DoExpand = true){
flag := DoExpand ? this.TVE_EXPAND : this.TVE_COLLAPSE
SendMessage this.TVM_EXPAND, flag, pItem, , % "ahk_id " this.TVHwnd
return ErrorLevel
}
;----------------------------------------------------------------------------------------------
; Method: Check
; Changes the state of a treeview item's check box
;
; Parameters:
; pItem - Handle to the item
;
; fCheck - If true, check the node
; If false, uncheck the node
;
; Force - If true (default), prevents this method from failing due to
; the node having an invalid initial state. See IsChecked
; method for more info.
;
; Returns:
; Returns true if if successful, otherwise false
;
; Remarks:
; This method makes pItem the current selection.
;
Check(pItem, fCheck, Force = true){
SavedDelay := A_KeyDelay
SetKeyDelay 30
CurrentState := this.IsChecked(pItem, false)
if (CurrentState = -1)
if (Force) {
ControlSend, , {Space}, % "ahk_id " this.TVHwnd
CurrentState := this.IsChecked(pItem, false)
}
else
return false
if (CurrentState and not fCheck) or (not CurrentState and fCheck )
ControlSend, , {Space}, % "ahk_id " this.TVHwnd
SetKeyDelay %SavedDelay%
return true
}
;----------------------------------------------------------------------------------------------
; Method: GetText
; Retrieves the text/name of the specified node
;
; Parameters:
; pItem - Handle to the item
;
; Returns:
; The text/name of the specified Item. If the text is longer than 127, only
; the first 127 characters are retrieved.
;
; Fix from just me (http://ahkscript.org/boards/viewtopic.php?f=5&t=4998#p29339)
;
GetText(pItem){
this.TVM_GETITEM := A_IsUnicode ? this.TVM_GETITEMW : this.TVM_GETITEMA
WinGet, ProcessId, pid, % "ahk_id " this.TVHwnd
hProcess := this.OpenProcess(this.PROCESS_VM_OPERATION|this.PROCESS_VM_READ
|this.PROCESS_VM_WRITE|this.PROCESS_QUERY_INFORMATION
, false, ProcessId)
; Try to determine the bitness of the remote tree-view's process
ProcessIs32Bit := A_PtrSize = 8 ? False : True
If (A_Is64bitOS) && DllCall("Kernel32.dll\IsWow64Process", "Ptr", hProcess, "UIntP", WOW64)
ProcessIs32Bit := WOW64
Size := ProcessIs32Bit ? 60 : 80 ; Size of a TVITEMEX structure
_tvi := this.VirtualAllocEx(hProcess, 0, Size, this.MEM_COMMIT, this.PAGE_READWRITE)
_txt := this.VirtualAllocEx(hProcess, 0, 256, this.MEM_COMMIT, this.PAGE_READWRITE)
; TVITEMEX Structure
VarSetCapacity(tvi, Size, 0)
NumPut(this.TVIF_TEXT|this.TVIF_HANDLE, tvi, 0, "UInt")
If (ProcessIs32Bit){
NumPut(pItem, tvi, 4, "UInt")
NumPut(_txt , tvi, 16, "UInt")
NumPut(127 , tvi, 20, "UInt")
} Else {
NumPut(pItem, tvi, 8, "UInt64")
NumPut(_txt , tvi, 24, "UInt64")
NumPut(127 , tvi, 32, "UInt")
}
VarSetCapacity(txt, 256, 0)
this.WriteProcessMemory(hProcess, _tvi, &tvi, Size)
SendMessage this.TVM_GETITEM, 0, _tvi, , % "ahk_id " this.TVHwnd
this.ReadProcessMemory(hProcess, _txt, txt, 256)
this.VirtualFreeEx(hProcess, _txt, 0, this.MEM_RELEASE)
this.VirtualFreeEx(hProcess, _tvi, 0, this.MEM_RELEASE)
this.CloseHandle(hProcess)
return txt
}
;----------------------------------------------------------------------------------------------
; Method: EditLabel
; Begins in-place editing of the specified item's text, replacing the text of the
; item with a single-line edit control containing the text. This method implicitly
; selects and focuses the specified item.
;
; Parameters:
; pItem - Handle to the item
;
; Returns:
; Returns the handle to the edit control used to edit the item text if successful,
; or NULL otherwise. When the user completes or cancels editing, the edit control
; is destroyed and the handle is no longer valid.
;
EditLabel(pItem){
this.TVM_EDITLABEL := A_IsUnicode ? this.TVM_EDITLABELW : this.TVM_EDITLABELA
SendMessage this.TVM_EDITLABEL, 0, pItem, , % "ahk_id " this.TVHwnd
return ErrorLevel
}
;----------------------------------------------------------------------------------------------
; Method: GetCount
; Returns the total number of expanded items in the control
;
; Parameters:
; None
;
; Returns:
; Returns the total number of expanded items in the control
;
GetCount(){
SendMessage this.TVM_GETCOUNT, 0, 0, , % "ahk_id " this.TVHwnd
return ErrorLevel
}
;----------------------------------------------------------------------------------------------
; Method: IsChecked
; Retrieves an item's checked status
;
; Parameters:
; pItem - Handle to the item
;
; Force - If true (default), forces the node to return a valid state.
; Since this involves toggling the state of the check box, it
; can have undesired side effects. Make this false to disable
; this feature.
; Returns:
; Returns 1 if the item is checked, 0 if unchecked.
;
; Returns -1 if the checkbox state cannot be determined because no checkbox
; image is currently associated with the node and Force is false.
;
; Remarks:
; Due to a "feature" of Windows, a checkbox can be displayed even if no checkbox image
; is associated with the node. It is important to either check the actual value returned
; or make the Force parameter true.
;
; This method makes pItem the current selection.
;
IsChecked(pItem, Force = true){
SavedDelay := A_KeyDelay
SetKeyDelay 30
this.SetSelection(pItem)
SendMessage this.TVM_GETITEMSTATE, pItem, 0, , % "ahk_id " this.TVHwnd
State := ((ErrorLevel & this.TVIS_STATEIMAGEMASK) >> 12) - 1
if (State = -1 and Force) {
ControlSend, , {Space 2}, % "ahk_id " this.TVHwnd
SendMessage this.TVM_GETITEMSTATE, pItem, 0, , % "ahk_id " this.TVHwnd
State := ((ErrorLevel & this.TVIS_STATEIMAGEMASK) >> 12) - 1
}
SetKeyDelay %SavedDelay%
return State
}
;----------------------------------------------------------------------------------------------
; Method: IsBold
; Check if a node is in bold font
;
; Parameters:
; pItem - Handle to the item
;
; Returns:
; Returns true if the item is in bold, false otherwise.
;
IsBold(pItem){
SendMessage this.TVM_GETITEMSTATE, pItem, 0, , % "ahk_id " this.TVHwnd
return (ErrorLevel & this.TVIS_BOLD) >> 4
}
;----------------------------------------------------------------------------------------------
; Method: IsExpanded
; Check if a node has children and is expanded
;
; Parameters:
; pItem - Handle to the item
;
; Returns:
; Returns true if the item has children and is expanded, false otherwise.
;
IsExpanded(pItem){
SendMessage this.TVM_GETITEMSTATE, pItem, 0, , % "ahk_id " this.TVHwnd
return (ErrorLevel & this.TVIS_EXPANDED) >> 5
}
;----------------------------------------------------------------------------------------------
; Method: IsSelected
; Check if a node is Selected
;
; Parameters:
; pItem - Handle to the item
;
; Returns:
; Returns true if the item is selected, false otherwise.
;
IsSelected(pItem){
SendMessage, this.TVM_GETITEMSTATE, pItem, 0, , % "ahk_id " this.TVHwnd
return (ErrorLevel & this.TVIS_SELECTED) >> 1
}
;----------------------------------------------------------------------------------------------
; Method: GetControlContent
; Returns a text representation of the control content, indented with tabs.
;
; Parameters:
; Level - (Optional) Indention level
; pItem - (Optional) Handle to the starting item
;
; Returns:
; A text representation of the control content
GetControlContent(Level:=0, pItem:=0){
passed := false
ControlText =
loop {
SendMessage this.TVM_GETNEXTITEM, passed ? this.TVGN_NEXT : this.TVGN_CHILD , pItem, , % "ahk_id " this.TVHwnd
if(ErrorLevel != 0){
pItem := ErrorLevel
while (A_Index < Level) {
ControlText .= "`t"
}
ControlText .= this.GetText(pItem) "`n"
; ControlText .= RegexReplace(this.GetText(pItem),"[-\.]","`t",donechanges,1) . "`n"
NextLevel := Level+1
ControlText .= this.GetControlContent(NextLevel,pItem, LowestLevel)
passed=true
} else {
break
}
}
return ControlText
}
;==================================================================================================
;
; Functions
;
;==================================================================================================
;----------------------------------------------------------------------------------------------
; Method: OpenProcess
; Opens an existing local process object.
;
; Parameters:
; DesiredAccess - The desired access to the process object.
;
; InheritHandle - If this value is TRUE, processes created by this process will inherit
; the handle. Otherwise, the processes do not inherit this handle.
;
; ProcessId - The Process ID of the local process to be opened.
;
; Returns: