Es ist mir nicht gelungen einen farbigen Button mit OnmouseOver-Effekt UND transparentem .ico zu erstellen.
Wahrscheinlich wegen des GUI-Bugs werden die transparenten Teile eines .ico Files immer in der Farbe der GUI-BG-Color dargestellt.
Ein möglicher Weg dies (mit einfachen Mitteln) zu umgehen ist es die ButtonFarbe unter dem Icon gleichzeitig mit der GUI-BG-Color zu wechseln.
Das klappt prima, solange ich nur einen Button habe. Habe ich mehrere Buttons brauche ich für jeden Button eine subGUI ...
Das habe ich hier mal umgesetzt. Macht dies Methode wirklich Sinn?
Oder gibt es da inzwischen leichtere Workarounds?
(ich habe stundenlang gesucht, aber just diese Problemlösung des Icons nicht gefunden)
Im Beispiel habe ich 24 Buttons, weil ich die Kapazität von GUIGetMsg() testen wollte, scheint aber zu gehen.
Beim Clicken wird nur in die Console geloggt (keine Action).
Außerdem wäre mein nächstes Ziel dass die GUI beim Aufbau nicht so sehr flackert (habe bisher - offensichtlich - nicht viel mit GUIs gearbeitet).
Programmierstil und Ordentlichkeit sind furchtbar
sorry ist work in prgoress!
IconButtonHoverEffect.au3:
#include <GUIConstants.au3>
#include <Array.au3>
; This script creates colored button-like-control-set with states with proper icon
; ------------------------------------------------------------------------------------------------------------------- BG + Text/ButtonStyle Setup
Global $_Buttons[1][21]
$_Buttons[0][0] = 0
Global $_ButtonGuiOverlay = 0x999999 ; use this overlay to hide the true bg-color as it changes
Global $_ButtonStyle[1][10] = [ _ ;FontCol,Size,Weight,Attribute,Fontname,winhandle,quality,LableStyle,LabelStyleX
[ 0xfffffff, 12, 400, Default, "Tahoma", Default, Default, Default, Default]]
; ------------------------------------------------------------------------------------------------------------------- Create GUI & Buttons
Local $hGUI = GUICreate("ColorChangeIconButton Test", 800, 870, -1, -1) ; global $hGUI (Main)
;~ GUISetState(@SW_HIDE, $hGUI)
GUISetBkColor($_ButtonGuiOverlay, $hGUI)
GUISetState(@SW_SHOW)
Local $plate = GUICtrlCreateLabel("", 0,0, 800, 870) ; main Surface of GUI (used to get onmouseover for hover-out
GUICtrlSetBkColor($plate, $_ButtonGuiOverlay)
GUISetState(@SW_SHOW, $hGUI) ; no idea how to make this not flicker on startup!
Local $butt1 = _ButtonCreate($hGUI, $plate, "", "Test", 100, 100, 50, 50, 0xcccccc, 0x00cccc, 0x00ffff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn") ; add userfunction???
Local $butt2 = _ButtonCreate($hGUI, $plate, "", "", 200, 100, 50, 50, 0xcccccc, 0x00cccc, 0x00ffff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt3 = _ButtonCreate($hGUI, $plate, "", "", 300, 100, 50, 50, 0x2277ff, 0x114477, 0xff00ff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt4 = _ButtonCreate($hGUI, $plate, "", "", 400, 100, 50, 50, 0x777777, 0x007777, 0xffff00, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt5 = _ButtonCreate($hGUI, $plate, "", "", 100, 200, 50, 50, 0x2277ff, 0x114477, 0xff00ff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt6 = _ButtonCreate($hGUI, $plate, "", "", 300, 200, 50, 50, 0x777777, 0x007777, 0xffff00, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt7 = _ButtonCreate($hGUI, $plate, "", "", 400, 200, 50, 50, 0x777777, 0x007777, 0xffff00, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt8 = _ButtonCreate($hGUI, $plate, "", "Test", 200, 200, 50, 50, 0xcccccc, 0x00cccc, 0x00ffff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt9 = _ButtonCreate($hGUI, $plate, "", "", 100, 300, 50, 50, 0x777777, 0x007777, 0xffff00, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt10 = _ButtonCreate($hGUI, $plate, "", "", 400, 300, 50, 50, 0x777777, 0x007777, 0xffff00, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt11 = _ButtonCreate($hGUI, $plate, "", "", 200, 300, 50, 50, 0xcccccc, 0x00cccc, 0x00ffff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt12 = _ButtonCreate($hGUI, $plate, "", "", 300, 300, 50, 50, 0x2277ff, 0x114477, 0xff00ff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt13 = _ButtonCreate($hGUI, $plate, "", "", 100, 400, 50, 50, 0xcccccc, 0x00cccc, 0x00ffff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt14 = _ButtonCreate($hGUI, $plate, "", "Test", 200, 400, 50, 50, 0xcccccc, 0x00cccc, 0x00ffff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt15 = _ButtonCreate($hGUI, $plate, "", "", 300, 400, 50, 50, 0x2277ff, 0x114477, 0xff00ff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt16 = _ButtonCreate($hGUI, $plate, "", "", 400, 400, 50, 50, 0x777777, 0x007777, 0xffff00, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt17 = _ButtonCreate($hGUI, $plate, "", "", 100, 500, 50, 50, 0x2277ff, 0x114477, 0xff00ff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt18 = _ButtonCreate($hGUI, $plate, "", "", 300, 500, 50, 50, 0x2277ff, 0x114477, 0xff00ff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt19 = _ButtonCreate($hGUI, $plate, "", "", 400, 500, 50, 50, 0x777777, 0x007777, 0xffff00, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt20 = _ButtonCreate($hGUI, $plate, "", "", 200, 500, 50, 50, 0xcccccc, 0x00cccc, 0x00ffff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt21 = _ButtonCreate($hGUI, $plate, "", "", 100, 600, 50, 50, 0x777777, 0x007777, 0xffff00, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt22 = _ButtonCreate($hGUI, $plate, "", "", 400, 600, 50, 50, 0x777777, 0x007777, 0xffff00, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt23 = _ButtonCreate($hGUI, $plate, "", "", 200, 600, 50, 50, 0xcccccc, 0x00cccc, 0x00ffff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
Local $butt24 = _ButtonCreate($hGUI, $plate, "", "", 300, 600, 50, 50, 0x2277ff, 0x114477, 0xff00ff, "C:\Windows\System32\shell32.dll", 4, 32, 32, 9, 9, 0, "UserFn")
;_ButtonSetState(@SW_SHOW)
GUISetState(@SW_SHOW, $hGUI)
; ------------------------------------------------------------------------------------------------------------------- GUIGetMsg Handler
GUISwitch($hGUI)
While 1
;Local $state = -1
Local $gm = GUIGetMsg()
Switch $gm
Case $GUI_EVENT_CLOSE ; add custom stuff on hGUI here!
CloseProgram()
;~ Case $GUI_EVENT_PRIMARYUP ; not reliable, play for some ms only
;~ $state = 0
;~ Case $GUI_EVENT_PRIMARYDOWN
;~ Local $state = 1
Case Else
_ButtonGetEvent($gm)
EndSwitch
Sleep(10)
WEnd
CloseProgram()
; use sth this to add actions to the button states
Func UserFn($id, $bID, $_Event, $userA)
_log("UserFn --------------------------------------------")
Switch $_Event
Case "Static"
;
Case "Hover"
;
Case "Press"
If IsArray($userA) Then
;
Else
;MsgBox(0, "$_Buttons", "$_Buttons[" & $id & "][0] = " & $bID & " .. was pressed!") ; dont use msgbox when button is down!
_log("do something...")
EndIf
EndSwitch
EndFunc
; ------------------------------------------------------------------------------------------------------------------- Functions --->
; Catch GuiGetMsg() And GuiGetCursorInfo() to create events
Func _ButtonGetEvent($gm)
For $i = 1 To $_Buttons[0][0]
Local $bL = $_Buttons[$i][0]
Local $bI = $_Buttons[$i][1]
If $gm > 0 Then
If $gm = $bL Or $gm = $bI Then ; check if buttons(label or icon) are Presssed
_ButtonHandle($_Buttons[$i][0], "Press")
;ExitLoop ;only 1 event?
EndIf
Else
Local $gui = $_Buttons[$i][19]
Local $info = GUIGetCursorInfo($gui) ; check cursor for hover
If ($info[4] = $bL Or $info[4] = $bI) Then ;And $state <> 1 Then
_ButtonHandle($_Buttons[$i][0], "Hover")
ExitLoop
ElseIf ($info[4] <> $bL And $info[4] <> $bI) And $_Buttons[$i][20] = "Hover" Then
_ButtonHandle($_Buttons[$i][0], "Static")
ExitLoop
EndIf
EndIf
Next
EndFunc
; Destroy a Button
Func _ButtonDestroy($i, $index=False) ;?
If Not $index Then
$i = _ArraySearch($_Buttons, $i, 1, 0, 0, 0, 1, 0)
If $i < 0 Then
MsgBox(64, "Error", "No Index")
Return False
EndIf
EndIf
_ArrayDelete($_Buttons, String($i))
$_Buttons[0][0] -= 1 ;UBound($_Buttons+1)
;_ArrayDisplay($_Buttons)
EndFunc
; Create "Button" as a LabelControl and save data for handling
; MainGUI, SurroundingSurfaceHnd, optButtonText, optTooltipText, x, y, w, h, RegularColor, HoverColor, PressColor, IconFile, IconIndex, IconWidth, IconHight, xOffIcon, yOffIcon, ButtonSyle, userfn, userdata
Func _ButtonCreate($hGUI, $hSurface, $strText, $strTooltip, $xPos, $yPos, $bWidth, $bHight, $colStatic, $colHover, $colPress, $iconFile, $iconIndex, $iconSizeX, $iconSizeY, $iconXoff, $iconYoff, $bStyle, $UserFn, $UserData=-1)
Local $ctrlID = -1, $icon[3]
Local $hChild = GUICreate("", $bWidth, $bHight, $xPos, $yPos, $WS_POPUP, $WS_EX_MDICHILD, $hGUI)
GUISwitch($hChild)
GUISetState(@SW_DISABLE, $hChild)
GUISetState(@SW_HIDE, $hChild)
; FontSetup
GUISetFont($_ButtonStyle[$bStyle][1], $_ButtonStyle[$bStyle][2], $_ButtonStyle[$bStyle][3], $_ButtonStyle[$bStyle][4], $hChild, $_ButtonStyle[$bStyle][6])
$ctrlID = GUICtrlCreateLabel($strText, 0, 0, $bWidth, $bHight)
GUICtrlSetColor($ctrlID, $_ButtonStyle[$bStyle][0])
If $strTooltip <> "" Then GUICtrlSetTip ( $ctrlID, $strTooltip, "", $TIP_NOICON, $TIP_CENTER)
;
GUISetBkColor($colStatic, $hChild) ;set state static
GUICtrlSetBkColor($ctrlID, $colStatic)
$icon[0] = GUICtrlCreateIcon($iconFile, $iconIndex, 0+$iconXoff, 0+$iconYoff, $iconSizeX, $iconSizeY, Default)
GUISetState(@SW_ENABLE, $hChild)
GUISetState(@SW_SHOW, $hChild)
$_Buttons[0][0] += 1 ; store data for handling
ReDim $_Buttons[ $_Buttons[0][0]+1 ][25]
$_Buttons[ $_Buttons[0][0] ][0] = $ctrlID
$_Buttons[ $_Buttons[0][0] ][1] = $icon[0]
;~ $_Buttons[ $_Buttons[0][0] ][2] = $icon[1]
;~ $_Buttons[ $_Buttons[0][0] ][3] = $icon[2]
$_Buttons[ $_Buttons[0][0] ][4] = $hGUI
$_Buttons[ $_Buttons[0][0] ][5] = $hSurface
$_Buttons[ $_Buttons[0][0] ][6] = $strText
$_Buttons[ $_Buttons[0][0] ][7] = $strTooltip
$_Buttons[ $_Buttons[0][0] ][8] = $xPos
$_Buttons[ $_Buttons[0][0] ][9] = $yPos
$_Buttons[ $_Buttons[0][0] ][10] = $bWidth
$_Buttons[ $_Buttons[0][0] ][11] = $bHight
$_Buttons[ $_Buttons[0][0] ][12] = $colStatic
$_Buttons[ $_Buttons[0][0] ][13] = $colHover
$_Buttons[ $_Buttons[0][0] ][14] = $colPress
$_Buttons[ $_Buttons[0][0] ][15] = $iconIndex
$_Buttons[ $_Buttons[0][0] ][16] = $iconSizeX
$_Buttons[ $_Buttons[0][0] ][17] = $iconXoff
$_Buttons[ $_Buttons[0][0] ][18] = $iconYoff
$_Buttons[ $_Buttons[0][0] ][19] = $hChild
$_Buttons[ $_Buttons[0][0] ][20] = $bStyle
$_Buttons[ $_Buttons[0][0] ][21] = $iconSizeY
$_Buttons[ $_Buttons[0][0] ][22] = $UserFn
$_Buttons[ $_Buttons[0][0] ][23] = $UserData
;MsgBox(0,"done","")
Return $ctrlID
EndFunc
; Handle Button States
Func _ButtonSetState($Flag) ;?
_log("Set " & $_Buttons[0][0] & " Flags")
GUISetState(@SW_HIDE, $hGUI)
For $i = 1 To $_Buttons[0][0]
Local $gui = $_Buttons[ $i ][19]
GUISetState($Flag, $gui)
Next
GUISetState(@SW_SHOW, $hGUI)
EndFunc
; Handle Button Colors due to events
Func _ButtonHandle($bID, $_Event)
Local $id = _ArraySearch($_Buttons, $bID, 1, 0, 0, 0, 1, 0)
If $id > 0 And $_Buttons[$id][20] <> $_Event Then
_log("_ButtonHandle( " & $bID & " , " & $_Event & ") - Button found: " & $id)
Switch $_Event
Case "Hover"
GUISetBkColor($_Buttons[$id][13], $_Buttons[$id][19])
GUICtrlSetBkColor($_Buttons[$id][0], $_Buttons[$id][13])
GUISetState(@SW_SHOW, $_Buttons[$id][19])
Case "Press"
GUISetBkColor($_Buttons[$id][14], $_Buttons[$id][19])
GUICtrlSetBkColor($_Buttons[$id][0], $_Buttons[$id][14])
GUISetState(@SW_SHOW, $_Buttons[$id][19])
Local $fn = $_Buttons[$id][22]
Local $data = $_Buttons[$id][23]
_log("Calling: " & $fn & ", " & $id & ", " & $bID)
Call($fn, $id, $bID, $_Event, $data)
Case "Static"
GUISetBkColor($_Buttons[$id][12], $_Buttons[$id][19])
GUICtrlSetBkColor($_Buttons[$id][0], $_Buttons[$id][12])
GUISetState(@SW_SHOW, $_Buttons[$id][19])
EndSwitch
EndIf
$_Buttons[$id][20] = $_Event
EndFunc
Func CloseProgram()
GUISetState(@SW_HIDE, $hGUI)
For $i = 1 To $_Buttons[0][0]
GUISetState(@SW_HIDE, $_Buttons[$i][20])
GUIDelete($_Buttons[$i][20])
Next
GUIDelete($hGUI)
Exit 0
EndFunc
Func _log($str)
ConsoleWrite($str & @CRLF)
EndFunc
Alles anzeigen
Freu mich
über Feedback!