-
Notifications
You must be signed in to change notification settings - Fork 72
/
JEEGuiText.ahk
3727 lines (3284 loc) · 105 KB
/
JEEGuiText.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
;==================================================
;JEEGuiText.ahk
;use this function anywhere in a script
;to include this library at the bottom of the script
JEEGuiText_Load() {
}
;==================================================
;functions from other libraries:
;Acc_ ...
;DirExist
;JEE_DC ...
;JEE_StrRept
;SendMessage
;==================================================
;FUNCTIONS - CONSTANTS
;==================================================
;note: throughout this script the following constants are used:
;PROCESS_QUERY_INFORMATION := 0x400 ;PROCESS_VM_WRITE := 0x20
;PROCESS_VM_READ := 0x10 ;PROCESS_VM_OPERATION := 0x8
;e.g. JEE_DCOpenProcess(0x438, 0, vPID)
;IsWow64Process - The handle must have the PROCESS_QUERY_INFORMATION access right.
;WriteProcessMemory - The handle must have PROCESS_VM_WRITE and PROCESS_VM_OPERATION access to the process.
;ReadProcessMemory - The handle must have PROCESS_VM_READ access to the process.
;MEM_RESERVE := 0x2000 ;MEM_COMMIT := 0x1000
;PAGE_READWRITE := 0x4
;e.g. pBuf := JEE_DCVirtualAllocEx(hProc, 0, vSize, 0x3000, 0x4)
;MEM_RELEASE := 0x8000
;e.g. JEE_DCVirtualFreeEx(hProc, pBuf, 0, 0x8000)
;note: the following 6 dll functions are used repeatedly
;when interacting with external windows/controls
;OpenProcess
;VirtualAllocEx
;WriteProcessMemory
;ReadProcessMemory
;VirtualFreeEx
;CloseHandle
;==================================================
;FUNCTIONS - INTRO
;==================================================
;notes:
;i: work on internal controls only
;r: require remote buffers on external controls (e.g. Edit/Static/Button/ListBox/ComboBox)
;a: uses Acc
;x: uses an AHK command/function
;s: single item selection
;m: multiple items selection
;z: multiple items (but none typically selected)
;note: any 'Get' function for controls with multiple items can return an object
;note: JEE_LVXXXText has columns not just items
;note: all functions get/set all text, as a variable, by default
;note: the user must set DetectHiddenWindows as appropriate, before using each function
;==================================================
;FUNCTIONS - SUMMARY
;==================================================
;function prefixes
;general
;Acc Microsoft Active Accessibility (MSAA)
;Win window
;control specific - one item
;Edit Edit
;Static Static
;Btn Button
;PB msctls_progress32
;TrB msctls_trackbar32
;RE RICHEDIT50W
;DTP SysDateTimePick32
;MonthCal SysMonthCal32
;HotkeyCtl msctls_hotkey32
;LinkCtl SysLink
;Sci Scintilla
;control specific - multiple items
;LB ListBox
;CB ComboBox
;LV SysListView32
;LVH SysHeader32
;TV SysTreeView32
;SB msctls_statusbar32
;TB ToolbarWindow32
;TC SysTabControl32
;further
;CFD Common File Dialog (older-style open/save as prompts)
;CID Common Item Dialog (newer-style open/save as prompts)
;==================================================
;total functions (4+37+17+6+22=86)
;general (4)
;__ JEE_GetSelectedText(vWait:=3)
;__ JEE_AccCtlGetText(hWnd, vSep:="`n")
;__ JEE_WinGetText(hWnd)
;__ JEE_WinSetText(hWnd, vText)
;control specific - one item (37)
;[Edit,Static,Button,msctls_progress32,msctls_trackbar32,RICHEDIT50W,SysDateTimePick32,SysMonthCal32,msctls_hotkey32,SysLink,Scintilla]
;__ JEE_EditGetText(hCtl, vPos1:="", vPos2:="")
;__ JEE_EditSetText(hCtl, vText)
;__ JEE_EditPaste(hCtl, vText, vCanUndo:=1)
;__ JEE_EditGetTextSpecialPlace(hCtl)
;__ JEE_EditSetTextSpecialPlace(hCtl, vText)
;__ JEE_StaticGetText(hCtl)
;__ JEE_StaticSetText(hCtl, vText)
;__ JEE_BtnGetText(hCtl)
;__ JEE_BtnSetText(hCtl, vText)
;__ JEE_PBGetPos(hCtl)
;__ JEE_PBSetPos(hCtl, vPos)
;__ JEE_TrBGetPos(hCtl)
;__ JEE_TrBSetPos(hCtl, vPos)
;x_ JEE_REGetText(hCtl)
;r_ JEE_RESetText(hCtl, vText, vFlags:=0x0, vCP:=0x0)
;i_ JEE_REGetStream(hCtl, vFormat)
;i_ JEE_REGetStreamCallback(dwCookie, pbBuff, cb, pcb)
;i_ JEE_REGetStreamToFile(hCtl, vFormat, vPath)
;i_ JEE_REGetStreamToFileCallback(dwCookie, pbBuff, cb, pcb)
;i_ JEE_RESetStream(hCtl, vFormat, vAddr, vSize)
;i_ JEE_RESetStreamCallback(dwCookie, pbBuff, cb, pcb)
;i_ JEE_RESetStreamFromFile(hCtl, vFormat, vPath)
;i_ JEE_RESetStreamFromFileCallback(dwCookie, pbBuff, cb, pcb)
;r_ JEE_DTPGetDate(hCtl, vLen:=14)
;r_ JEE_DTPSetDate(hCtl, vDate)
;r_ JEE_MonthCalGetDate(hCtl, vLen:=14)
;r_ JEE_MonthCalSetDate(hCtl, vDate)
;a_ JEE_HotkeyCtlGetText(hCtl)
;x_ JEE_HotkeyCtlSetText(hCtl, vKeys)
;a_ JEE_LinkCtlGetText(hCtl, vDoGetFull:=1)
;x_ JEE_LinkCtlSetText(hCtl, vText)
;r_ JEE_SciGetText(hCtl, vEnc:="UTF-8")
;r_ JEE_SciSetText(hCtl, vText)
;r_ JEE_SciPaste(hCtl, vText, vSize:=-1)
;x_ JEE_SciGetTextAlt(hCtl, vEnc:="UTF-8")
;x_ JEE_SciSetTextAlt(hCtl, vText)
;x_ JEE_SciPasteAlt(hCtl, vText)
;control specific - multiple items (17)
;[ListBox,ComboBox,SysListView32,SysHeader32,SysTreeView32,msctls_statusbar32,ToolbarWindow32,SysTabControl32]
;_m JEE_LBGetText(hCtl, vList:=-1, vSep:="`n", vOpt:="")
;_m JEE_LBSetText(hCtl, vText, vList:=-1, vSep:="`n", vOpt:="")
;_s JEE_CBGetText(hCtl, vList:=-1, vSep:="`n", vOpt:="")
;_s JEE_CBSetText(hCtl, vText, vList:=-1, vSep:="`n", vOpt:="")
;rm JEE_LVGetText(hCtl, vList:=-1, vCol:=-1, vSep:="`n", vSepTab:="`t", vOpt:="")
;rm JEE_LVSetText(hCtl, vText, vList:=-1, vCol:=-1, vSep:="`n", vSepTab:="`t", vOpt:="")
;rs JEE_LVHGetText(hCtl, vList:=-1, vSep:="`n", vOpt:="")
;rs JEE_LVHSetText(hCtl, vText, vList:=-1, vSep:="`n", vOpt:="")
;rs JEE_TVGetText(hCtl, vList:=-1, vSep:="`n", vOpt:="", vDirPfx:="")
;rs JEE_TVSetText(hCtl, vText, vList:=-1, vSep:="`n", vOpt:="")
;rz JEE_SBGetText(hCtl, vList:=-1, vSep:="`n", vOpt:="")
;rz JEE_SBSetText(hCtl, vText, vList:=-1, vSep:="`n", vOpt:="")
;rz JEE_TBGetText(hCtl, vList:=-1, vSep:="`n", vOpt:="")
;rz JEE_TBGetTextPool(hCtl, vList:=-1, vSep:="`n", vOpt:="")
;rz JEE_TBSetText(hCtl, vText, vList:=-1, vSep:="`n", vOpt:="")
;rs JEE_TCGetText(hCtl, vList:=-1, vSep:="`n", vOpt:="")
;rs JEE_TCSetText(hCtl, vText, vList:=-1, vSep:="`n", vOpt:="")
;control specific - multiple items further (6)
;__ JEE_LBInsStr(hCtl, vText, vPos:=-1)
;__ JEE_LBDelStr(hCtl, vPos:=-1)
;__ JEE_CBInsStr(hCtl, vText, vPos:=-1)
;__ JEE_CBDelStr(hCtl, vPos:=-1)
;__ JEE_TrayIconRename(hWnd, uID, vWinTitle)
;__ JEE_TrayIconModify(hWnd, uID, uMsg, hIcon, vWinTitle)
;further (22)
;[#32768,#32770,tooltips_class32]
;__ JEE_MenuGetText(hWndOrMenu:="", vSep:="`n", vOpt:="")
;__ JEE_MenuGetTextAll(hWndOrMenu:="", vSep:="`n", vOpt:="")
;__ JEE_MenuItemPosGetText(hMenu, vPos)
;__ JEE_MenuItemIDGetText(hMenu, vID)
;__ JEE_MenuItemPosSetText(hMenu, vText, vPos)
;__ JEE_MenuItemIDSetText(hMenu, vText, vID)
;__ JEE_CFDGetDir(hWnd)
;__ JEE_CFDGetPath(hWnd)
;__ JEE_CFDGetName(hWnd)
;__ JEE_CFDGetDirName(hWnd)
;__ JEE_CFDSetDir(hWnd, vDir)
;__ JEE_CFDSetPath(hWnd, vPath)
;__ JEE_CFDGetPathAlt(hWnd, vSep:="|")
;__ JEE_CFDChoosePath(hWnd, vPath)
;__ JEE_CIDGetDir(hWnd)
;__ JEE_CIDGetPath(hWnd, vSep:="|")
;__ JEE_CIDGetName(hWnd, vSep:="|")
;__ JEE_CIDSetDir(hWnd, vDir)
;__ JEE_CIDSetPath(hWnd, vPath)
;__ JEE_CIDChoosePath(hWnd, vPath)
;__ JEE_ToolTipGetText(hWnd)
;__ JEE_ToolTipSetText(hWnd, vText)
;==================================================
;maximum string lengths
;for the following 8 controls types:
;ListBox,ComboBox,SysListView32,SysHeader32,SysTreeView32,ToolbarWindow32,msctls_statusbar32,SysTabControl32
;based on investigations here:
;GUI: control types and maximum string lengths - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=40292
;- You can display 5460 characters fine (5641 if include the null) in all 8 control types, beyond that, some controls display blanks (listviews/listview headers/treeviews continue to display fine).
;- You can retrieve 65534 (65535 if include the null) characters from one of the items (parts) in a status bar control. The documentation suggests that this is the maximum number of characters you can get, but that you can set a larger number.
;- It is not clear what the actual limits of these control types are.
;==================================================
;information on getting/setting text and structs:
;although getting/setting the text of items
;often requires a struct, you usually only need to
;set the contents of the struct once,
;occasions where indexes in structs need to be
;changed for each item:
;get/set listview/treeview text
;(usually you specify the index in the PostMessage/SendMessage)
;LB_GETTEXTLEN, LB_GETTEXT
;LB_DELETESTRING, LB_INSERTSTRING
;CB_GETLBTEXTLEN, CB_GETLBTEXT
;CB_DELETESTRING, CB_INSERTSTRING
;LVM_GETITEMTEXTW (LVITEM) (iItem (row)/iSubItem (column) specified in struct)
;LVM_SETITEMTEXTW (LVITEM) (iItem (row)/iSubItem (column) specified in struct)
;HDM_GETITEMW (HDITEM)
;HDM_SETITEMW (HDITEM)
;TVM_GETITEMW (TVITEM) (hItem specified in struct)
;TVM_SETITEMW (TVITEM) (hItem specified in struct)
;SB_GETTEXTW
;SB_SETTEXTW
;[note: TBBUTTON structs are used to convert indexes to command IDs]
;TB_GETBUTTONTEXTW or TB_GETSTRINGW
;TB_SETBUTTONINFOW (TBBUTTONINFO)
;TCM_GETITEMW (TCITEM)
;TCM_SETITEMW (TCITEM)
;==================================================
;information on getting text and messages:
;when getting strings:
;LB_GETTEXTLEN - gives size of string
;CB_GETLBTEXTLEN - gives size of string
;LVM_GETITEMTEXTW - gives size of string
;HDM_GETITEMW - doesn't give size of string
;TVM_GETITEMW - doesn't give size of string
;SB_GETTEXT - gives size of string
;TB_GETBUTTONTEXTW / TB_GETSTRINGW - both give size of string
;TCM_GETITEMW - doesn't give size of string
;==================================================
;information on listbox controls:
;when determining the focused item/selected items:
;get focus (single-selection): LB_GETCURSEL/LB_GETCARETINDEX
;get selection (single-selection): LB_GETCURSEL/LB_GETCARETINDEX
;get focus (multi-selection): LB_GETCURSEL/LB_GETCARETINDEX
;get selection (multi-selection): LB_GETSELITEMS
;==================================================
;FUNCTIONS - START
;==================================================
; ;===============
; ;e.g.
; vText := JEE_GetSelectedText()
; ;===============
; ;===============
; ;e.g. get selected text simple alternative
; Clipboard := ""
; SendInput, ^c
; ClipWait, 3
; if ErrorLevel
; {
; MsgBox, % "error: failed to retrieve clipboard text"
; return
; }
; vText := Clipboard
; ;===============
JEE_GetSelectedText(vWait:=3) {
WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, % "ahk_id " hWnd
if (RegExReplace(vCtlClassNN, "\d") = "Edit")
ControlGet, vText, Selected,, % vCtlClassNN, % "ahk_id " hWnd
else
{
ClipSaved := ClipboardAll
Clipboard := ""
SendInput, ^c
ClipWait, % vWait
if ErrorLevel
{
ToolTip, % "ClipWait failed (" A_ThisHotkey ")"
Clipboard := ClipSaved
ClipSaved := ""
Sleep, 1000
ToolTip
Exit ;terminate the thread that launched this function
}
vText := Clipboard
Clipboard := ClipSaved
ClipSaved := ""
}
return vText
}
;==================================================
; ;===============
; ;e.g.
; ;q::
; MouseGetPos,,, hWnd, hCtl, 2
; if (hCtl = "")
; hCtl := hWnd
; WinGetClass, vWinClass, % "ahk_id " hCtl
; vText := JEE_AccCtlGetText(hCtl, "`r`n")
; MsgBox, % "[" vWinClass "]`r`n" vText
; return
; ;===============
;for a list of role types see:
;AccViewer Basic - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=32039
;note: Acc seems able to get text from virtually all controls,
;although it appears that it can't get the raw data for
;secondary columns in AccViewer, although 'Description'
;contains a summary of the secondary columns
;hh_kwd_vlist (e.g. AutoHotkey.chm)
;MSTaskListWClass (e.g. taskbar)
;JEE_AccControlGetText
JEE_AccCtlGetText(hWnd, vSep:="`n") {
WinGetClass, vWinClass, % "ahk_id " hWnd
if (vWinClass ~= "^(ComboLBox|hh_kwd_vlist|ListBox|msctls_statusbar32|MSTaskListWClass|SysHeader32|SysLink|SysListView32|SysTabControl32|SysTreeView32|ToolbarWindow32)$")
{
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
Loop, % oAcc.accChildCount
try vOutput .= oAcc.accName(A_Index) vSep
vOutput := SubStr(vOutput, 1, -StrLen(vSep))
}
else if (vWinClass = "#32768")
{
oAcc := Acc_Get("Object", "1", 0, "ahk_id " hWnd)
Loop, % oAcc.accChildCount
try vOutput .= oAcc.accName(A_Index) vSep
vOutput := SubStr(vOutput, 1, -StrLen(vSep))
}
else if (vWinClass ~= "^(ComboBox|Edit|msctls_hotkey32|msctls_progress32|msctls_trackbar32|RICHEDIT50W|SysDateTimePick32)$")
{
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
vOutput := oAcc.accValue
}
else if (vWinClass ~= "^(Button|Scintilla|Static|tooltips_class32)$")
{
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
vOutput := oAcc.accName
}
else if (vWinClass = "SysMonthCal32")
{
oAcc := Acc_Get("Object", "4.4", 0, "ahk_id " hWnd)
vOutput := oAcc.accName(0)
}
else
{
oAcc := Acc_ObjectFromWindow(hWnd)
try vName := oAcc.accName(0)
try vValue := oAcc.accValue(0)
if !(vName = "") && !(vValue = "")
vOutput := vName vSep vValue
else if !(vName = "")
vOutput := vName
else if !(vValue = "")
vOutput := vValue
}
oAcc := oChild := oChild2 := ""
return vOutput
}
;==================================================
;JEE_WinGetTitle
JEE_WinGetText(hWnd) {
vChars := DllCall("user32\GetWindowTextLength", Ptr,hWnd) + 1
VarSetCapacity(vText, vChars << !!A_IsUnicode, 0)
DllCall("user32\GetWindowText", Ptr,hWnd, Str,vText, Int,vChars)
return vText
}
;==================================================
;JEE_WinSetTitle
JEE_WinSetText(hWnd, vText) {
DllCall("user32\SetWindowText", Ptr,hWnd, Str,vText)
}
;==================================================
JEE_EditGetText(hCtl, vPos1:="", vPos2:="") {
vChars := 1+SendMessage(0xE, 0, 0,, "ahk_id " hCtl) ;WM_GETTEXTLENGTH := 0xE
VarSetCapacity(vText, vChars << !!A_IsUnicode, 0)
SendMessage(0xD, vChars, &vText,, "ahk_id " hCtl) ;WM_GETTEXT := 0xD
VarSetCapacity(vText, -1)
if (vPos1 vPos2 = "")
return vText
if (vPos2 = -1)
vPos2 := ""
if !(vPos2 = "") || (vPos1 ~= "^\d")
{
if (vPos1 = "")
vPos1 := 1
if (vPos2 = "")
vPos2 := StrLen(vText)
return SubStr(vText, vPos1, vPos2-vPos1+1)
}
vCateg := vPos1
VarSetCapacity(vPos1, 4, 0), VarSetCapacity(vPos2, 4, 0)
SendMessage(0xB0, &vPos1, &vPos2,, "ahk_id " hCtl) ;EM_GETSEL := 0xB0
vPos1 := NumGet(&vPos1, 0, "UInt"), vPos2 := NumGet(&vPos2, 0, "UInt")
if (vPos1 = vPos2)
return
else if (vCateg = "sel")
return SubStr(vText, vPos1+1, vPos2-vPos1+1)
else if (vCateg = "abo") ;before selection
return SubStr(vText, 1, vPos1)
else if (vCateg = "bel") ;after + including selection
return SubStr(vText, vPos1+1)
}
;==================================================
JEE_EditSetText(hCtl, vText) {
SendMessage(0xC,, &vText,, "ahk_id " hCtl) ;WM_SETTEXT := 0xC
}
;==================================================
JEE_EditPaste(hCtl, vText, vCanUndo:=1) {
vChars := StrLen(vText)+1
SendMessage(0xC2, vCanUndo, &vText,, "ahk_id " hCtl) ;EM_REPLACESEL := 0xC2
}
;==================================================
;The secret life of GetWindowText – The Old New Thing
;https://blogs.msdn.microsoft.com/oldnewthing/20030821-00/?p=42833
JEE_EditGetTextSpecialPlace(hCtl)
{
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if !(vPID = vScriptPID)
{
vChars := DllCall("user32\GetWindowTextLength", Ptr,hCtl) + 1
VarSetCapacity(vText, vChars << !!A_IsUnicode, 0)
DllCall("user32\GetWindowText", Ptr,hCtl, Str,vText, Int,vChars)
return vText
}
vScript .= "vChars := DllCall(" Chr(34) "user32\GetWindowTextLength" Chr(34) ", Ptr," hCtl ") + 1"
vScript .= "`r`n" "VarSetCapacity(vText, vChars << !!A_IsUnicode, 0)"
vScript .= "`r`n" "DllCall(" Chr(34) "user32\GetWindowText" Chr(34) ", Ptr," hCtl ", Str,vText, Int,vChars)"
vScript .= "`r`n" "FileAppend, % vText, *"
oShell := ComObjCreate("WScript.Shell")
oExec := oShell.Exec(Chr(34) A_AhkPath Chr(34) " /ErrorStdOut *")
oExec.StdIn.Write(vScript)
oExec.StdIn.Close()
vStdOut := oExec.StdOut.ReadAll()
oShell := oExec := ""
return vStdOut
}
;==================================================
JEE_EditSetTextSpecialPlace(hCtl, vText) {
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if !(vPID = vScriptPID)
{
DllCall("user32\SetWindowText", Ptr,hCtl, Str,vText)
return
}
vText := StrReplace(vText, "``", "````")
vText := StrReplace(vText, "`r", "``r")
vText := StrReplace(vText, "`n", "``n")
if !!SubStr(1,0) ;vIsV1
vText := StrReplace(vText, Chr(34), Chr(34) Chr(34))
else
vText := StrReplace(vText, Chr(34), "``" Chr(34))
vScript := "DllCall(" Chr(34) "user32\SetWindowText" Chr(34) ", Ptr," hCtl ", Str," Chr(34) vText Chr(34) ")"
oShell := ComObjCreate("WScript.Shell")
oExec := oShell.Exec(Chr(34) A_AhkPath Chr(34) " /ErrorStdOut *")
oExec.StdIn.Write(vScript)
oExec.StdIn.Close()
oShell := oExec := ""
}
;same as EditGetText
JEE_StaticGetText(hCtl) {
vChars := 1+SendMessage(0xE, 0, 0,, "ahk_id " hCtl) ;WM_GETTEXTLENGTH := 0xE
VarSetCapacity(vText, vChars << !!A_IsUnicode, 0)
SendMessage(0xD, vChars, &vText,, "ahk_id " hCtl) ;WM_GETTEXT := 0xD
VarSetCapacity(vText, -1)
return vText
}
JEE_StaticSetText(hCtl, vText) {
SendMessage(0xC,, &vText,, "ahk_id " hCtl) ;WM_SETTEXT := 0xC
}
;JEE_EditGetTextSimple
JEE_BtnGetText(hCtl) {
vChars := 1+SendMessage(0xE, 0, 0,, "ahk_id " hCtl) ;WM_GETTEXTLENGTH := 0xE
VarSetCapacity(vText, vChars << !!A_IsUnicode, 0)
SendMessage(0xD, vChars, &vText,, "ahk_id " hCtl) ;WM_GETTEXT := 0xD
VarSetCapacity(vText, -1)
return vText
}
;same as EditSetText
JEE_BtnSetText(hCtl, vText) {
SendMessage(0xC,, &vText,, "ahk_id " hCtl) ;WM_SETTEXT := 0xC
}
JEE_PBGetPos(hCtl) {
return SendMessage(0x408,,,, "ahk_id " hCtl) ;PBM_GETPOS := 0x408
}
JEE_PBSetPos(hCtl, vPos) {
SendMessage(0x402, vPos,,, "ahk_id " hCtl) ;PBM_SETPOS := 0x402
}
JEE_TrBGetPos(hCtl) {
return SendMessage(0x400,,,, "ahk_id " hCtl) ;TBM_GETPOS := 0x400
}
JEE_TrBSetPos(hCtl, vPos){
SendMessage(0x422,, vPos,, "ahk_id " hCtl) ;TBM_SETPOSNOTIFY := 0x422
}
JEE_REGetText(hCtl) {
ControlGetText, vText,, % "ahk_id " hCtl
return vText
}
; ;===============
; ;e.g.
; ;tested on WordPad (Windows XP and Windows 7 versions)
; ;q:: ;RichEdit control - set text (RTF)
; ControlGet, hCtl, Hwnd,, RICHEDIT50W1, A
; FormatTime, vDate,, HH:mm dd/MM/yyyy
; vRtf := "{\rtf{\b " vDate "}}"
; JEE_RESetText(hCtl, vRtf)
; return
JEE_RESetText(hCtl, vText, vFlags:=0x0, vCP:=0x0) {
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if (vPID = vScriptPID)
vIsLocal := 1, vPIs64 := (A_PtrSize=8)
;ST_UNICODE := 0x8 ;ST_NEWCHARS := 0x4
;ST_SELECTION := 0x2 ;ST_KEEPUNDO := 0x1
;ST_DEFAULT := 0x0
;CP_ACP := 0 ;Unicode (1200)
VarSetCapacity(SETTEXTEX, 8)
NumPut(vFlags, &SETTEXTEX, 0, "UInt") ;flags
NumPut(vCP, &SETTEXTEX, 4, "UInt") ;codepage
if ((vCP = 1200) = !!A_IsUnicode)
{
vSize := (StrLen(vText)+1) << !!A_IsUnicode
pText := &vText
}
else
{
vSize := StrPut(vText, (vCP = 1200)?"UTF-16":"CP0")
VarSetCapacity(vOutput, vSize)
StrPut(vText, &vOutput, (vCP = 1200)?"UTF-16":"CP0")
pText := &vOutput
}
if vIsLocal
SendMessage(0x461, &SETTEXTEX, pText,, "ahk_id " hCtl) ;EM_SETTEXTEX := 0x461
else
{
if !hProc := JEE_DCOpenProcess(0x438, 0, vPID)
return
if !pBuf := JEE_DCVirtualAllocEx(hProc, 0, 8+vSize, 0x3000, 0x4)
return
JEE_DCWriteProcessMemory(hProc, pBuf, &SETTEXTEX, 8, 0)
JEE_DCWriteProcessMemory(hProc, pBuf+8, pText, vSize, 0)
SendMessage(0x461, pBuf, pBuf+8,, "ahk_id " hCtl) ;EM_SETTEXTEX := 0x461
JEE_DCVirtualFreeEx(hProc, pBuf, 0, 0x8000)
JEE_DCCloseHandle(hProc)
}
}
; ;e.g.
; ;q::
; ControlGet, hCtl, Hwnd,, RICHEDIT50W1, A
; MsgBox, % JEE_REGetStream(hCtl, 0x11)
; MsgBox, % JEE_REGetStream(hCtl, 0x2)
; return
;only works on internal controls
JEE_REGetStream(hCtl, vFormat) {
static pFunc := RegisterCallback("JEE_REGetStreamCallback")
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if !(vPID = vScriptPID)
return
;SFF_SELECTION := 0x8000 ;SFF_PLAINRTF := 0x4000
;SF_USECODEPAGE := 0x20 ;SF_UNICODE := 0x10
;SF_RTFNOOBJS := 0x3 ;SF_RTF := 0x2 ;SF_TEXT := 0x1
vSize := A_PtrSize=8?20:12
VarSetCapacity(EDITSTREAM, vSize, 0)
NumPut(vFormat, &EDITSTREAM, 0, "UPtr") ;dwCookie
NumPut(pFunc, &EDITSTREAM, A_PtrSize=8?12:8, "Ptr") ;pfnCallback
SendMessage(0x44A, vFormat, &EDITSTREAM,, "ahk_id " hCtl) ;EM_STREAMOUT := 0x44A
return JEE_REGetStreamCallback("Get", 0, 0, 0)
}
JEE_REGetStreamCallback(dwCookie, pbBuff, cb, pcb) {
static vTextOut := ""
if (cb > 0)
{
if (dwCookie & 0x10)
vTextOut .= StrGet(pbBuff, cb/2, "UTF-16")
else
vTextOut .= StrGet(pbBuff, cb, "CP0")
return 0
}
if (dwCookie = "Get")
{
vTemp := vTextOut
vTextOut := ""
return vTemp
}
return 1
}
;only works on internal controls
JEE_REGetStreamToFile(hCtl, vFormat, vPath) {
static pFunc := RegisterCallback("JEE_REGetStreamToFileCallback")
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if !(vPID = vScriptPID)
return
if !(oFile := FileOpen(vPath, "rw"))
return 0
;SFF_SELECTION := 0x8000 ;SFF_PLAINRTF := 0x4000
;SF_USECODEPAGE := 0x20 ;SF_UNICODE := 0x10
;SF_RTFNOOBJS := 0x3 ;SF_RTF := 0x2 ;SF_TEXT := 0x1
vSize := A_PtrSize=8?20:12
VarSetCapacity(EDITSTREAM, vSize, 0)
NumPut(oFile.__Handle, &EDITSTREAM, 0, "UPtr") ;dwCookie
NumPut(pFunc, &EDITSTREAM, A_PtrSize=8?12:8, "Ptr") ;pfnCallback
SendMessage(0x44A, vFormat, &EDITSTREAM,, "ahk_id " hCtl) ;EM_STREAMOUT := 0x44A
oFile.Close()
return
}
JEE_REGetStreamToFileCallback(dwCookie, pbBuff, cb, pcb) {
static vTextOut := ""
if (cb > 0)
return !DllCall("kernel32\WriteFile", Ptr,dwCookie, Ptr,pbBuff, UInt,cb, Ptr,pcb, Ptr,0)
return 1
}
; ;e.g.
; ;q::
; ControlGet, hCtl, Hwnd,, RICHEDIT50W1, A
; FormatTime, vDate,, HH:mm dd/MM/yyyy
; vRtf := "{\rtf{\b " vDate "}}"
; VarSetCapacity(vRtf2, 100, 0)
; vSize := StrPut(vRtf, &vRtf2, "CP0")
; JEE_RESetStream(hCtl, 0x4002, &vRtf2, vSize)
; return
;;only works on internal controls
JEE_RESetStream(hCtl, vFormat, vAddr, vSize) {
static pFunc := RegisterCallback("JEE_RESetStreamCallback")
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if !(vPID = vScriptPID)
return
;SFF_SELECTION := 0x8000 ;SFF_PLAINRTF := 0x4000
;SF_USECODEPAGE := 0x20 ;SF_UNICODE := 0x10
;SF_RTFNOOBJS := 0x3 ;SF_RTF := 0x2 ;SF_TEXT := 0x1
;UTF-16 (1200)
VarSetCapacity(EDITSTREAM, A_PtrSize=8?20:12, 0)
NumPut(vAddr, &EDITSTREAM, 0, "UPtr") ;dwCookie
NumPut(pFunc, &EDITSTREAM, A_PtrSize=8?12:8, "Ptr") ;pfnCallback
JEE_RESetStreamCallback("Init", vSize, 0, 0)
return SendMessage(0x449, vFormat, &EDITSTREAM,, "ahk_id " hCtl) ;EM_STREAMIN := 0x449 ;chars read (any CRLFs are counted as single characters)
}
JEE_RESetStreamCallback(dwCookie, pbBuff, cb, pcb) {
static vRemain, vOffset
if (dwCookie = "Init")
{
vRemain := pbBuff, vOffset := 0
return 0
}
if (vRemain <= cb)
{
DllCall("kernel32\RtlMoveMemory", Ptr,pbBuff, Ptr,dwCookie+vOffset, UPtr,vRemain)
NumPut(vRemain, pcb+0, 0, "Ptr")
vOffset += vRemain, vRemain := 0
return 0
}
else
{
DllCall("kernel32\RtlMoveMemory", Ptr,pbBuff, Ptr,dwCookie+vOffset, UPtr,cb)
NumPut(cb, pcb+0, 0, "Ptr")
vOffset += cb, vRemain -= cb
return 0
}
}
;==================================================
; ;===============
; ;e.g.
; ;q::
; ControlGet, hCtl, Hwnd,, RICHEDIT50W1, A
; vPath := A_Desktop "\MyFile.rtf"
; JEE_RESetStreamFromFile(hCtl, 0x4002, vPath)
; return
; ;===============
;only works on internal controls
JEE_RESetStreamFromFile(hCtl, vFormat, vPath)
{
static pFunc := RegisterCallback("JEE_RESetStreamFromFileCallback")
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if !(vPID = vScriptPID)
return
;SFF_SELECTION := 0x8000 ;SFF_PLAINRTF := 0x4000
;SF_USECODEPAGE := 0x20 ;SF_UNICODE := 0x10
;SF_RTFNOOBJS := 0x3 ;SF_RTF := 0x2 ;SF_TEXT := 0x1
;UTF-16 (1200)
if !(oFile := FileOpen(vPath, "r"))
return 0
VarSetCapacity(EDITSTREAM, A_PtrSize=8?20:12, 0)
NumPut(oFile.__Handle, &EDITSTREAM, 0, "UPtr") ;dwCookie
NumPut(pFunc, &EDITSTREAM, A_PtrSize=8?12:8, "Ptr") ;pfnCallback
vChars := SendMessage(0x449, vFormat, &EDITSTREAM,, "ahk_id " hCtl) ;EM_STREAMIN := 0x449
oFile.Close()
return vChars ;chars read (any CRLFs are counted as single characters)
}
;==================================================
JEE_RESetStreamFromFileCallback(dwCookie, pbBuff, cb, pcb)
{
return !DllCall("kernel32\ReadFile", Ptr,dwCookie, Ptr,pbBuff, UInt,cb, Ptr,pcb, Ptr,0)
}
;==================================================
;JEE_DTPGetTime
JEE_DTPGetDate(hCtl, vLen:=14)
{
if !(vLen=14) && !RegExMatch(vLen, "^(4|6|8|10|12|14|17)$")
return
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if (vPID = vScriptPID)
vIsLocal := 1, vPIs64 := (A_PtrSize=8)
vSize := 16
VarSetCapacity(SYSTEMTIME, 16, 0)
;GDT_VALID := 0
if vIsLocal
SendMessage(0x1001, 0, &SYSTEMTIME,, "ahk_id " hCtl) ;DTM_GETSYSTEMTIME := 0x1001
else
if !vIsLocal
{
if !hProc := JEE_DCOpenProcess(0x438, 0, vPID)
return
if !pBuf := JEE_DCVirtualAllocEx(hProc, 0, vSize, 0x3000, 0x4)
return
SendMessage(0x1001, 0, pBuf,, "ahk_id " hCtl) ;DTM_GETSYSTEMTIME := 0x1001
JEE_DCReadProcessMemory(hProc, pBuf, &SYSTEMTIME, vSize, 0)
JEE_DCVirtualFreeEx(hProc, pBuf, 0, 0x8000)
JEE_DCCloseHandle(hProc)
}
Loop, 7
if !(A_Index = 3)
vDate .= Format("{:02}", NumGet(&SYSTEMTIME, A_Index*2-2, "UShort"))
vDate .= Format("{:03}", NumGet(&SYSTEMTIME, 14, "UShort"))
return SubStr(vDate, 1, vLen)
}
;==================================================
;JEE_DTPSetTime
JEE_DTPSetDate(hCtl, vDate)
{
vLen := StrLen(vDate)
if !RegExMatch(vLen, "^(4|6|8|10|12|14|17)$")
return
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if (vPID = vScriptPID)
vIsLocal := 1, vPIs64 := (A_PtrSize=8)
if (vLen < 17)
vDate .= SubStr(19990101000000000, vLen+1) ;17 characters: Y M D H M S M
vDate := RegExReplace(vDate, "(....)(..)(..)(..)(..)(..)(...)", "$1-$2-0-$3-$4-$5-$6-$7")
vSize := 16
VarSetCapacity(SYSTEMTIME, 16, 0)
Loop, Parse, vDate, % "-"
NumPut(A_LoopField, &SYSTEMTIME, (A_Index*2)-2, "UShort")
;GDT_VALID := 0
if vIsLocal
SendMessage(0x1002, 0, &SYSTEMTIME,, "ahk_id " hCtl) ;DTM_SETSYSTEMTIME := 0x1002
else
{
if !hProc := JEE_DCOpenProcess(0x438, 0, vPID)
return
if !pBuf := JEE_DCVirtualAllocEx(hProc, 0, vSize, 0x3000, 0x4)
return
JEE_DCWriteProcessMemory(hProc, pBuf, &SYSTEMTIME, vSize, 0)
SendMessage(0x1002, 0, pBuf,, "ahk_id " hCtl) ;DTM_SETSYSTEMTIME := 0x1002
JEE_DCVirtualFreeEx(hProc, pBuf, 0, 0x8000)
JEE_DCCloseHandle(hProc)
}
}
;==================================================
;JEE_MonthCalGetTime
JEE_MonthCalGetDate(hCtl, vLen:=14)
{
if !(vLen=14) && !RegExMatch(vLen, "^(4|6|8|10|12|14|17)$")
return
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if (vPID = vScriptPID)
vIsLocal := 1, vPIs64 := (A_PtrSize=8)
ControlGet, vCtlStyle, Style,,, % "ahk_id " hCtl
if (vCtlStyle & 0x2) ;MCS_MULTISELECT := 0x2
vSize := 32, vMsg := 0x1005 ;MCM_GETSELRANGE := 0x1005
else
vSize := 16, vMsg := 0x1001 ;MCM_GETCURSEL := 0x1001
VarSetCapacity(ArraySYSTEMTIME, vSize, 0)
if vIsLocal
SendMessage(vMsg, 0, &ArraySYSTEMTIME,, "ahk_id " hCtl) ;MCM_GETCURSEL := 0x1001 ;MCM_GETSELRANGE := 0x1005
else
{
if !hProc := JEE_DCOpenProcess(0x438, 0, vPID)
return
if !pBuf := JEE_DCVirtualAllocEx(hProc, 0, vSize, 0x3000, 0x4)
return
SendMessage(vMsg, 0, pBuf,, "ahk_id " hCtl) ;MCM_GETCURSEL := 0x1001 ;MCM_GETSELRANGE := 0x1005
JEE_DCReadProcessMemory(hProc, pBuf, &ArraySYSTEMTIME, vSize, 0)
JEE_DCVirtualFreeEx(hProc, pBuf, 0, 0x8000)
JEE_DCCloseHandle(hProc)
}
Loop, 7
if !(A_Index = 3)
vDate .= Format("{:02}", NumGet(&ArraySYSTEMTIME, A_Index*2-2, "UShort"))
vDate .= Format("{:03}", NumGet(&ArraySYSTEMTIME, 14, "UShort"))
return SubStr(vDate, 1, vLen)
}
;==================================================
;JEE_MonthCalSetTime
JEE_MonthCalSetDate(hCtl, vDate)
{
vLen := StrLen(vDate)
if !RegExMatch(vLen, "^(4|6|8|10|12|14|17)$")
return
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if (vPID = vScriptPID)
vIsLocal := 1, vPIs64 := (A_PtrSize=8)
ControlGet, vCtlStyle, Style,,, % "ahk_id " hCtl
if (vCtlStyle & 0x2) ;MCS_MULTISELECT := 0x2
vSize := 32, vMsg := 0x1006 ;MCM_SETSELRANGE := 0x1006
else
vSize := 16, vMsg := 0x1002 ;MCM_SETCURSEL := 0x1002
if (vLen < 17)
vDate .= SubStr(19990101000000000, vLen+1) ;17 characters: Y M D H M S M
vDate := RegExReplace(vDate, "(....)(..)(..)(..)(..)(..)(...)", "$1-$2-0-$3-$4-$5-$6-$7")
VarSetCapacity(ArraySYSTEMTIME, vSize, 0)
Loop, Parse, vDate, % "-"
{
NumPut(A_LoopField, &ArraySYSTEMTIME, (A_Index*2)-2, "UShort")
if (vCtlStyle & 0x2)
NumPut(A_LoopField, &ArraySYSTEMTIME, 16+(A_Index*2)-2, "UShort")
}
if vIsLocal
SendMessage(vMsg, 0, &ArraySYSTEMTIME,, "ahk_id " hCtl) ;MCM_SETCURSEL := 0x1002 ;MCM_SETSELRANGE := 0x1006
else
{
if !hProc := JEE_DCOpenProcess(0x438, 0, vPID)
return
if !pBuf := JEE_DCVirtualAllocEx(hProc, 0, vSize, 0x3000, 0x4)
return
JEE_DCWriteProcessMemory(hProc, pBuf, &ArraySYSTEMTIME, vSize, 0)
SendMessage(vMsg, 0, pBuf,, "ahk_id " hCtl) ;MCM_SETCURSEL := 0x1002 ;MCM_SETSELRANGE := 0x1006
JEE_DCVirtualFreeEx(hProc, pBuf, 0, 0x8000)
JEE_DCCloseHandle(hProc)
}
}
;==================================================
JEE_HotkeyCtlGetText(hCtl)
{
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hCtl)
vText := oAcc.accValue(0)
oAcc := ""
return vText
}
;==================================================
;note: the hotkey control doesn't detect the Win key
JEE_HotkeyCtlSetText(hCtl, vKeys)
{
ControlSend,, % vKeys, % "ahk_id " hCtl
}
;==================================================
JEE_LinkCtlGetText(hCtl, vDoGetFull:=1)
{
if vDoGetFull
{
ControlGetText, vText,, % "ahk_id " hCtl
return vText
}
oAcc := Acc_ObjectFromWindow(hCtl)
vText := oAcc.accName(0)
oAcc := ""
return vText
}
;==================================================
; ;===============
; ;e.g.
; ;q::
; ControlGet, hCtl, Hwnd,, SysLink1, A
; vText = <a href="https://autohotkey.com/">https://autohotkey.com/</a>
; JEE_LinkCtlSetText(hCtl, vText)
; return
; ;===============
JEE_LinkCtlSetText(hCtl, vText)
{
ControlSetText,, % vText, % "ahk_id " hCtl
}
;==================================================
; ;===============
; ;e.g.
; ;q::
; ControlGet, hCtl, Hwnd,, Scintilla1, A
; MsgBox, % JEE_SciGetText(hCtl)
; MsgBox, % JEE_SciGetText(hCtl, "CP0")
; MsgBox, % JEE_SciGetText(hCtl, "UTF-16")