WinGetHandle()
Posted: Wed Feb 22, 2023 4:50 pm
WinGetHandle is a very frequently used function in AutoIt3 and I need it in some programs.
But the function in AutoIt3 also accepts window sizes and regular expressions to search for the title as search criteria. I did not complicate this with additional parameters, since this is rarely used.
But the function in AutoIt3 also accepts window sizes and regular expressions to search for the title as search criteria. I did not complicate this with additional parameters, since this is rarely used.
Code: Select all
EnableExplicit
Structure ResStr
s.s
r.s
slen.i
rlen.i
hwnd.l
match.l
type.l
instance.l
EndStructure
Enumeration
#Title
#Class
EndEnumeration
; Finding Window
Procedure.l enumChildren(hwnd.l, *s.ResStr)
Static n.l = 0
If hwnd
Select *s\type
Case 0
GetWindowText_(hwnd, @*s\r, *s\rlen)
Case 1
GetClassName_(hwnd, @*s\r, *s\rlen)
EndSelect
Select *s\match
Case 0
If *s\r = *s\s
n + 1
If n = *s\instance
n = 0
Debug *s\r
*s\hwnd = hwnd
ProcedureReturn 0
EndIf
EndIf
Case 1
If Left(*s\r, *s\slen) = *s\s
n + 1
If n = *s\instance
n = 0
Debug *s\r
*s\hwnd = hwnd
ProcedureReturn 0
EndIf
EndIf
Case 2
If FindString(*s\r, *s\s, 1, #PB_String_NoCase)
n + 1
If n = *s\instance
n = 0
Debug *s\r
*s\hwnd = hwnd
ProcedureReturn 0
EndIf
EndIf
EndSelect
ProcedureReturn 1
EndIf
n = 0
ProcedureReturn 0
EndProcedure
; type
; 0 - Title
; 1 - Class
; match
; 0 - exact match
; 1 - match from start
; 2 - the word occurs in the title
Procedure.l WinGetHandle(hwnd, text.s, instance = 1, type = 0, match = 0)
Protected s.ResStr
s\rlen = 256
s\r = Space(s\rlen)
s\s = text
s\slen = Len(text)
s\match = match
s\type = type
s\instance = instance
EnumChildWindows_(hwnd, @enumChildren(), @s)
ProcedureReturn s\hwnd
EndProcedure
Define hwnd
; Debug WinGetHandle(0, "PureBasic", 1, #Title)
; Debug WinGetHandle(0, "LTS", 1, #Title, 2)
; hwnd = WinGetHandle(0, "WindowClass_2", 1, #Class, 0) ; PureBasic
; Debug Hex(hwnd)
; If hwnd
; hwnd = WinGetHandle(hwnd, "Scintilla", 1, #Class, 0) ; PureBasic -> Scintilla
; Debug Hex(hwnd)
; EndIf
hwnd = WinGetHandle(0, "Properties", 1, #Title, 1) ; Properties
; hwnd = WinGetHandle(0, "#32770", 1, #Class, 0) ; #32770
Debug Hex(hwnd)
If hwnd
hwnd = WinGetHandle(hwnd, "Button", 4, #Class, 0) ; Properties -> Others
Debug Hex(hwnd)
EndIf
; SendMessage_(hwnd, #BM_CLICK, 0, 0)
; Add a size check
; Define win.RECT
; GetWindowRect_(hwnd, win)
; If class = "#32770" And (win\bottom - win\top) = 111 And (win\right - win\left) = 333
; EndIf