- Offizieller Beitrag
Manchmal möchte ich abhängig vom Gui-Ctrl in einer Funktion unterschiedlich reagieren (z.B. bei registriertem $WM_COMMAND auf $EN_CHANGE). Da z.B. Edit und Input beide der Klasse "Edit" angehören, kann ich Aktionen, die gleichermaßen auf alle Input aber nicht auf Edit angewendet werden sollen nur durch Speichern der zugelassenen Ctrl-ID in einem Array realisieren, welches ich dann durchlaufe und auf Übereinstimmung mit der gerade aktiven ID prüfe. Es würde mir besser gefallen, wenn ich allgemein auf den Control-Typ (z.B. Edit oder Input) prüfen könnte.
Mit der GuiCtrlInfo.au3 stelle ich die Funktion _GuiCtrlGetInfo(ID) bereit, die anhand der übergebenen Ctrl-ID nicht nur die Klasse, sondern bei derselben Klasse für unterschiedliche AutoIt-Control auch deren AutoIt-Namen ermittelt. Davon betroffen sind folgende Ctrl:
GUICtrlCreateButton() Button
GUICtrlCreateCheckbox() Button
GUICtrlCreateGroup() Button
GUICtrlCreateRadio() Button
GUICtrlCreateEdit() Edit
GUICtrlCreateInput() Edit
GUICtrlCreateGraphic() Static
GUICtrlCreateIcon() Static
GUICtrlCreateLabel() Static
GUICtrlCreatePic() Static
Die Funktion gibt eine Struktur zurück mit folgendem Inhalt:
.ID GuiControl-ID
.hWnd GuiControl-Handle
.Class Classname
.Au3Name AutoIt-Name (der Teil nach GuiCtrlCreate..)
.Au3Type Konstante für den AutoIt-Namen
.Style GuiControl-Style
.ExStyle GuiControl-ExStyle
Das Unterscheiden der Control anhand der Styles sollte funktionieren. Bin mir aber noch nicht sicher, ob es auch bei den verschiedensten Style-Kombinationen klappt. Bin da für Rückmeldungen dankbar.
Was mir aufgefallen ist:
Ein GuiCtrlCreateLabel() ist von einem GuiCtrlCreateGraphic() anhand Klasse und Style nicht zu unterscheiden - sie sind identisch. Ist mir völlig unklar, wie das intern differenziert wird. Ich habe jetzt als Notlösung einfach mit Grafikfunktion einen Pixel im nicht sichtbaren Bereich des Controls gezeichnet. Ist die Funktion erfolgreich, ist es ein Graphic, wenn nicht - ein Label.
Es werden ausschliesslich durch Native AutoIt Funktionen erstellte Ctrl-ID geprüft (GuiCtrlCreateObj ist somit nicht dabei).
UDF
;-- TIME_STAMP 2021-02-13 12:19:09 v 0.1
#include-once
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
; CONSTANTS: (G)ui(C)reated(C)ontrol ordered by Classname
; FUNCTION CLASS
Global Const $GCC_NONE = 0
Global Const $GCC_BUTTON = 1 ; GUICtrlCreateButton() Button
Global Const $GCC_CHECKBOX = 2 ; GUICtrlCreateCheckbox() Button
Global Const $GCC_GROUP = 3 ; GUICtrlCreateGroup() Button
Global Const $GCC_RADIO = 4 ; GUICtrlCreateRadio() Button
Global Const $GCC_COMBO = 5 ; GUICtrlCreateCombo() ComboBox
Global Const $GCC_EDIT = 6 ; GUICtrlCreateEdit() Edit
Global Const $GCC_INPUT = 7 ; GUICtrlCreateInput() Edit
Global Const $GCC_LIST = 8 ; GUICtrlCreateList() ListBox
Global Const $GCC_PROGRESS = 9 ; GUICtrlCreateProgress() msctls_progress32
Global Const $GCC_SLIDER = 10 ; GUICtrlCreateSlider() msctls_trackbar32
Global Const $GCC_UPDOWN = 11 ; GUICtrlCreateUpdown() msctls_updown32
Global Const $GCC_GRAPHIC = 12 ; GUICtrlCreateGraphic() Static
Global Const $GCC_ICON = 13 ; GUICtrlCreateIcon() Static
Global Const $GCC_LABEL = 14 ; GUICtrlCreateLabel() Static
Global Const $GCC_PIC = 15 ; GUICtrlCreatePic() Static
Global Const $GCC_AVI = 16 ; GUICtrlCreateAvi() SysAnimate32
Global Const $GCC_DATE = 17 ; GUICtrlCreateDate() SysDateTimePick32
Global Const $GCC_LISTVIEW = 18 ; GuiCtrlCreateListView() SysListView32
Global Const $GCC_MONTHCAL = 19 ; GUICtrlCreateMonthCal() SysMonthCal32
Global Const $GCC_TAB = 20 ; GUICtrlCreateTab() SysTabControl32
Global Const $GCC_TREEVIEW = 21 ; GUICtrlCreateTreeView() SysTreeView32
Global $__gaGCC_CLASSES[][2] = [['None_Class_detected',''], _ ; [[Class,Au3-Name (part from "GuiCtrlCreate..")]]
['Button','Button'],['Button','Checkbox'],['Button','Group'],['Button','Radio'],['ComboBox','Combo'], _
['Edit','Edit'],['Edit','Input'],['ListBox','List'],['msctls_progress32','Progress'],['msctls_trackbar32','Slider'], _
['msctls_updown32','Updown'],['Static','Graphic'],['Static','Icon'],['Static','Label'],['Static','Pic'], _
['SysAnimate32','Avi'],['SysDateTimePick32','Date'],['SysListView32','ListView'],['SysMonthCal32','MonthCal'], _
['SysTabControl32','Tab'],['SysTreeView32','TreeView']]
Func _GuiCtrlGetInfo($_ID)
Local $tInfo = DllStructCreate('int ID;hwnd hWnd;char Class[32];char Au3Name[32];int Au3Type;int Style;int ExStyle;')
$tInfo.ID = $_ID
$tInfo.hWnd = GUICtrlGetHandle($_ID)
Local $aResult = DllCall("user32.dll", "int", "GetClassName", "hwnd", $tInfo.hWnd, "str", "", "int", 4096)
If @error Or Not $aResult[0] Then
$tInfo.Class = "None_Class_detected"
Return SetError(@error, @extended, $tInfo)
Else
$tInfo.Class = StringStripWS($aResult[2], 2)
EndIf
Local $aStyle = _CtrlGetStyle($tInfo.hWnd)
$tInfo.Style = $aStyle[0]
$tInfo.ExStyle = $aStyle[1]
Switch $tInfo.Class
Case "Button"
If BitAND($tInfo.Style, $BS_GROUPBOX) = $BS_GROUPBOX Then
$tInfo.Au3Type = $GCC_GROUP
ElseIf BitAND($tInfo.Style, $BS_3STATE) = $BS_3STATE Or _
BitAND($tInfo.Style, $BS_AUTO3STATE) = $BS_AUTO3STATE Or _
BitAND($tInfo.Style, $BS_AUTOCHECKBOX) = $BS_AUTOCHECKBOX Or _
BitAND($tInfo.Style, $BS_CHECKBOX) = $BS_CHECKBOX Then
$tInfo.Au3Type = $GCC_CHECKBOX
ElseIf BitAND($tInfo.Style, $BS_RADIOBUTTON) = $BS_RADIOBUTTON Or _
BitAND($tInfo.Style, $BS_AUTORADIOBUTTON) = $BS_AUTORADIOBUTTON Then
$tInfo.Au3Type = $GCC_RADIO
Else
$tInfo.Au3Type = $GCC_BUTTON
EndIf
Case "Edit"
If BitAND($tInfo.Style, $ES_AUTOVSCROLL) = $ES_AUTOVSCROLL Then
$tInfo.Au3Type = $GCC_EDIT
Else
$tInfo.Au3Type = $GCC_INPUT
EndIf
Case "Static"
If BitAND($tInfo.Style, $SS_ICON) = $SS_ICON Then
$tInfo.Au3Type = $GCC_ICON
ElseIf BitAND($tInfo.Style, $SS_BITMAP) = $SS_BITMAP Or _
BitAND($tInfo.Style, $SS_ENHMETAFILE) = $SS_ENHMETAFILE Then
$tInfo.Au3Type = $GCC_PIC
; Label and Graphic can have as only style: $SS_NOTIFY
ElseIf BitAND($tInfo.Style, $SS_NOTIFY) = $SS_NOTIFY Then
; try to use a graphic function - if fails: it's a label
Local $ret = GUICtrlSetGraphic($tInfo.ID, $GUI_GR_PIXEL, -1, -1)
If $ret = 1 Then
$tInfo.Au3Type = $GCC_GRAPHIC
Else
$tInfo.Au3Type = $GCC_LABEL
EndIf
EndIf
Case Else
For $i = 0 To UBound($__gaGCC_CLASSES) -1
If $__gaGCC_CLASSES[$i][0] = $tInfo.Class Then
$tInfo.Au3Type = $i
ExitLoop
EndIf
Next
EndSwitch
$tInfo.Au3Name = $__gaGCC_CLASSES[$tInfo.Au3Type][1]
Return $tInfo
EndFunc
; #FUNCTION# =======================================================================================
; Name...........: _CtrlGetStyle
; Description ...: Retrieves the Styles/ExStyles value(s) of the control.
; Syntax.........: _CtrlGetStyle($iControlID)
; Parameters ....: $iControlID - A valid control ID.
; Requirement(s).: v3.3.2.0 or higher
; Return values .: Success - Returns an Array[2] = [Style, ExStyle] with the Styles/ExStyles value(s).
; Failure - Returns an Array with -1 as the 0 & 1 Index's.
; Author ........: guinness & additional information from Melba23.
; Example........; Yes
;===================================================================================================
Func _CtrlGetStyle($iControlID)
Local $aArray[2] = [-1, -1], $aExStyle, $aStyle, $hControl = $iControlID
If Not IsHWnd($hControl) Then $hControl = GUICtrlGetHandle($iControlID)
$aStyle = DllCall("user32.dll", "long", "GetWindowLong", "hwnd", $hControl, "int", 0xFFFFFFF0)
If Not @error Then
$aArray[0] = $aStyle[0]
EndIf
$aExStyle = DllCall("user32.dll", "long", "GetWindowLong", "hwnd", $hControl, "int", 0xFFFFFFEC)
If Not @error Then
$aArray[1] = $aExStyle[0]
EndIf
Return $aArray
EndFunc ;==>_CtrlGetStyle
Alles anzeigen
Beispiel
;-- TIME_STAMP 2021-02-13 12:16:22
#include 'GuiCtrlInfo.au3'
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
$hGui = GUICreate('Test Ctrl', 800, 600)
; class: Button ID
$button = GUICtrlCreateButton('Button', 10, 40) ; 3
$check = GUICtrlCreateCheckbox('check', 100, 40) ; 4
$group = GUICtrlCreateGroup('group', 370, 70, 120, 60) ; 5
$radio = GUICtrlCreateRadio('radio', 390, 310, 60) ; 6
; class: Edit
$edit = GUICtrlCreateEdit('Edit', 200, 70, 150, 50) ; 7
$input = GUICtrlCreateInput('input', 640, 70, 100, 25) ; 8
; class: Static
$graphic = GUICtrlCreateGraphic(510, 70, 60, 60) ; 9
GUICtrlSetBkColor(-1, 0x0000ff)
$icon = GUICtrlCreateIcon("shell32.dll", 10, 590, 70) ;10
$label = GUICtrlCreateLabel('', 10, 140, 60) ;11
$pic = GUICtrlCreatePic("C:\Program Files (x86)\AutoIt3\Examples\GUI\mslogo.jpg", 10, 310, 200, 50);12
$avi = GUICtrlCreateAvi(@SystemDir & "\shell32.dll", 165, 250, 450, 300) ;13
GUICtrlSetState($avi, $GUI_AVISTART)
$combo = GUICtrlCreateCombo('combo', 200, 40, 150) ;14
$date = GUICtrlCreateDate("1953/04/25", 10, 70, 185, 20) ;15
$list = GUICtrlCreateList('list', 100, 140, 120, 30) ;16
$lv = GuiCtrlCreateListView('list|view', 250, 140, 200, 50) ;17
$monthCal = GUICtrlCreateMonthCal("1953/03/25", 610, 140, 180, 165) ;18
$oIE = ObjCreate("Shell.Explorer.2") ;KEINE ID - aber IsObj() = 1
$gcObj = GUICtrlCreateObj($oIE, 10, 200, 590, 100) ;19, ID - keine Class - aber IsObj() = 0
$progress = GUICtrlCreateProgress(250, 310, 120, 25) ;20
GUICtrlSetData(-1, 35)
$slider = GUICtrlCreateSlider(460, 310, 100, 25) ;21
GUICtrlSetData(-1, 75)
$tab = GUICtrlCreateTab(10, 450, 200, 60) ;22
$tv = GUICtrlCreateTreeView(610, 310, 180, 150, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
;23
$inputUD = GUICtrlCreateInput("2", 10, 610, 50, 20) ;24
$ud = GUICtrlCreateUpdown($inputUD) ;25
GUISetState()
Local $t, $aPre[] = ['>> ', '+> '], $ip = 1, $classLast = ''
ConsoleWrite(StringFormat('-> ID HWND CLASSNAME AU3NAME AU3TYPE STYLE EXSTYLE') & @CRLF)
For $i = $button To $ud
$t = _GuiCtrlGetInfo($i)
If $t.Class <> $classLast Then
$ip = Abs($ip-1)
$classLast = $t.Class
EndIf
ConsoleWrite(StringFormat('%s %-6s%-13s%-23s%-14s%-9s0x%-11s0x%s\n', $aPre[$ip], $t.ID, $t.hWnd, $t.Class, $t.Au3Name, $t.Au3Type, Hex($t.Style,8), Hex($t.ExStyle,8)))
Next
Do
Until GUIGetMsg() = -3
Alles anzeigen
Ausgabe Beispielcode
-> ID HWND CLASSNAME AU3NAME AU3TYPE STYLE EXSTYLE
>> 3 0x006F070E Button Button 1 0x50030000 0x00000000
>> 4 0x00050322 Button Checkbox 2 0x50010003 0x00000000
>> 5 0x0006031C Button Group 3 0x50020007 0x00000000
>> 6 0x004B080A Button Radio 4 0x50010009 0x00000000
+> 7 0x006A068E Edit Edit 6 0x503110C4 0x00000200
+> 8 0x001B07A2 Edit Input 7 0x50010080 0x00000200
>> 9 0x0048034C Static Graphic 12 0x50000100 0x00000000
>> 10 0x00B304BE Static Icon 13 0x50010103 0x00000000
>> 11 0x009805A6 Static Label 14 0x50000100 0x00000000
>> 12 0x008B086E Static Pic 15 0x5000010E 0x00000000
+> 13 0x0026034A SysAnimate32 Avi 16 0x50000002 0x00000000
>> 14 0x000E0352 ComboBox Combo 5 0x50010242 0x00000000
+> 15 0x007307FA SysDateTimePick32 Date 17 0x52010004 0x00000000
>> 16 0x00A8085A ListBox List 8 0x50810003 0x00000200
+> 17 0x01EB0562 SysListView32 ListView 18 0x5001000D 0x00000220
>> 18 0x007D069C SysMonthCal32 MonthCal 19 0x50010000 0x00000200
+> 19 0x00000000 None_Class_detected 0 0x00000000 0x00000000
>> 20 0x00F10552 msctls_progress32 Progress 9 0x50000000 0x00000000
+> 21 0x00890630 msctls_trackbar32 Slider 10 0x50000001 0x00000000
>> 22 0x00320738 SysTabControl32 Tab 20 0x54014040 0x00000000
+> 23 0x0096078A SysTreeView32 TreeView 21 0x50010037 0x00000200
>> 24 0x008C0528 Edit Input 7 0x50010080 0x00000200
+> 25 0x00220878 msctls_updown32 Updown 11 0x50000106 0x00000000
Alles anzeigen