Hi, ja, ohne WinAPi/GDI+ wird es schwierig. Ich denke, dein Workaround im ersten Beispiel funktionierte schon recht gut. Da hatte ich auf die schnelle auch keine bessere Lösung gesehen.
Da du dir die Mühe mit dem weiteren Bespiel gemacht hast gehe ich mal davon aus, dass du eine andere/bessere Lösung möchtest.
Ich hab also mal gebastelt und eine recht elegante Lösung gefunden, denke ich. Ich verpacke das ganze in eine eigene GuiCtrlCreateIconEx -Funktion. Intern wird dabei ein GuiCtrlCreateGraphic verwendet, bei dem die Pixel entsprechend gesetzt werden.
Ich denke die Performance könnte noch verbessert werden (z.B. GDIPlus global laden/entladen (_GDIPlus_StartUp/Shutdown) oder eine ganz andere Methode zu verwenden mit z.B. GuiCtrlCreatePic).
Die Verwendung ist aber mit einem Funktionsaufruf recht elegant und es kann danach wie ein normales AutoIt-Ctrl verwendet werden (Es ist halt nur ein GraphicCtrl).
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <GDIPlus.au3>
#include <WinAPIShellEx.au3>
#include <WinAPISysWin.au3>
Example()
Func Example()
Local $bg_color = 0xf0f0f0
Global $hGUI = GUICreate("GuiTest", 300, 200)
; Create a button control. - from examples working perfectly with trans icon on Default Color (No GUISetBkColor!) but has no color!
Local $idButton_1 = GUICtrlCreateButton("", 30, 30, 48, 48, $BS_ICON)
GUICtrlSetTip ( $idButton_1, "DefBtn", "")
Local $idIcon_1 = GUICtrlSetImage($idButton_1, "C:\Windows\System32\imageres.dll", -82, 1)
; The same but with color => not showing .ico file
Local $idButton_2 = GUICtrlCreateButton("ColBtn", 100, 30, 48, 48, $BS_ICON)
GUICtrlSetTip ( $idButton_2, "no ico!", "")
GUICtrlSetBkColor($idButton_2, 0x666666)
Local $idIcon_2 = GUICtrlSetImage($idButton_2, "C:\Windows\System32\imageres.dll", -82, 1)
; Createing a color label with the icon as pseudo-button works ONLY if GUI-BG-color is same as the icons background
Local $idButton_3 = GUICtrlCreateLabel("", 170, 30, 48, 48)
GUICtrlSetTip ( $idButton_3, "bad ico", "")
GUICtrlSetBkColor($idButton_3, 0x666666)
Local $idIcon_3 = GUICtrlCreateIcon("C:\Windows\System32\imageres.dll", -82, 178,38, 32,32)
Local $idIcon = GuiCtrlCreateIconEx("C:\Windows\System32\imageres.dll", -82, 30, 80, 48, 48, 32,32)
GUICtrlSetBkColor(-1, 0x666666)
; Press this button to "solve" the problem
Local $idButton_Switch = GUICtrlCreateButton("See the bug!", 210, 170, 85, 25)
GUISetState(@SW_SHOW, $hGUI)
Local $iPID = 0
; Loop until the user exits.
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $idButton_Switch
If $bg_color = 0xf0f0f0 Then
$bg_color = 0x666666
GUISetBkColor($bg_color, $hGUI)
$msg = "nice ico"
ElseIf $bg_color = 0x666666 Then
$bg_color = 0xf0f0f0
GUISetBkColor($bg_color, $hGUI)
$msg = "bad ico"
EndIf
Case $idButton_1
; Run Notepad with the window maximized.
MsgBox(0,"Default Button", "ico is transparent, but surface has fix color!")
Case $idButton_2
; Run Notepad with the window maximized.
MsgBox(0,"Default Button with Color", "Color set with GUICtrlSetBkColor() prevents ico")
Case $idButton_3
; Run Notepad with the window maximized.
MsgBox(0,"Label-Button", "Color will only work with transparency if gui-bg-bol matches the Label!")
Case $idIcon
; Run Notepad with the window maximized.
MsgBox(0,"IconEx-Button", "Works!")
EndSwitch
WEnd
; Delete the previous GUI and all controls.
GUIDelete($hGUI)
; Close the Notepad process using the PID returned by Run.
If $iPID Then ProcessClose($iPID)
EndFunc ;==>Example
Func GuiCtrlCreateIconEx($sFilename, $iIconIndex, $iLeft, $iTop, $iWidth = Default, $iHeight = Default, $iIconWidth = Default, $iIconHeight = Default, $iStyle = Default)
If $iIconWidth = Default Then $iIconWidth = $iWidth
If $iIconHeight = Default Then $iIconHeight = $iHeight
Local $idGraphic = GUICtrlCreateGraphic($iLeft, $iTop, $iWidth, $iHeight, $iStyle)
_GDIPlus_Startup()
Local $hIcon = _WinAPI_ShellExtractIcon($sFilename, $iIconIndex, $iIconWidth, $iIconHeight)
Local $hBitmap = _GDIPlus_BitmapCreateFromHICON($hIcon)
Local $iColorPrev = -1, $iImgWidth=_GDIPlus_ImageGetWidth($hBitmap), $iImgHeight = _GDIPlus_ImageGetHeight($hBitmap)
Local $iLeftSpace = (_WinAPI_GetClientWidth(GUICtrlGetHandle($idGraphic))-$iImgWidth)/2, $iTopSpace = (_WinAPI_GetClientHeight(GUICtrlGetHandle($idGraphic))-$iImgHeight)/2
For $x=0 To $iImgWidth
For $y=0 To $iImgHeight
Local $iColor = _GDIPlus_BitmapGetPixel($hBitmap, $x, $y)
If BitAND($iColor, 0xFF000000)<>0 Then
$iColor = BitAND($iColor, 0xFFFFFF)
If $iColor<>$iColorPrev Then
GUICtrlSetGraphic($idGraphic, $GUI_GR_COLOR, $iColor, $iColor)
$iColorPrev = $iColor
EndIf
GUICtrlSetGraphic($idGraphic, $GUI_GR_PIXEL, $iLeftSpace+$x, $iTopSpace+$y, 40, 30, 270)
EndIf
Next
Next
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()
return $idGraphic
EndFunc
Alles anzeigen
Ich hoffe das hilft dir weiter ![]()