GDI+ MyButtons

  • Hallo, AutoItler !

    Habe nur ein kleines Problem.
    Wie bekomme ich einen Text auf meine Buttons ???
    Habe schon so einiges ausprobiert und hatte kein Erfolg.

    Möchte keine normalen Buttons aller GUICtrlCreateButton...

    siehe mein Script bisher (alles außer die coole routine von SEuBo!)

    Spoiler anzeigen
    [autoit]


    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <GDIPlus.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global Const $Font = "Arial Black", $FontSize = 10, $FontColor = 0x000000, $BgColorTitle = 0xFFFFFF, $BgColor = 0x777799, $TitleHeight = 20, $DefaultCtrlHeight = 16

    [/autoit] [autoit][/autoit] [autoit]

    $Width = 400
    $Height = 100
    $Title = "ButtonControl"
    $x_Button1 = 8
    $y_Button1 = 30

    [/autoit] [autoit][/autoit] [autoit]

    $hGUI = GUICreate($Title, $Width, $Height, -1, -1, $WS_POPUP)
    GUISetBkColor($BgColor)

    [/autoit] [autoit][/autoit] [autoit]

    $knopt1 = GUICtrLCreatePng($hGUI,@ScriptDir & "\Button.png", $x_Button1, $y_Button1)
    $knopt2 = GUICtrLCreatePng($hGUI,@ScriptDir & "\Button.png", $x_Button1, $y_Button1+17)
    $knopt3 = GUICtrLCreatePng($hGUI,@ScriptDir & "\Button.png", $x_Button1, $y_Button1+34)
    $knopt4 = GUICtrLCreatePng($hGUI,@ScriptDir & "\Button.png", $x_Button1, $y_Button1+51)

    [/autoit] [autoit][/autoit] [autoit]

    $Close = GUICtrlCreatePic("close.bmp", $Width - 16, 7, 9, 9)
    $Minimize = GUICtrlCreatePic("minimize.bmp", $Width - 31, 7, 9, 9)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetFont($FontSize, 400, 0, $Font)
    $TitleBar = GUICtrlCreateLabel($Title, 6, 4, $Width - 42, 17, -1, $GUI_WS_EX_PARENTDRAG)
    GUICtrlSetColor(-1, $FontColor)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlCreateGraphic(0, 0, $Width, $TitleHeight)
    GUICtrlSetBkColor(-1, $BgColorTitle)

    [/autoit] [autoit][/autoit] [autoit]

    GuiRoundCorners($hGUI, 0, 0, 2, 2)
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $knopt1
    MsgBox(0,"","Button1 geklickt!",1)
    Case $knopt2
    MsgBox(0,"","Button2 geklickt!",1)
    Case $knopt3
    MsgBox(0,"","Button3 geklickt!",1)
    Case $knopt4
    MsgBox(0,"","Button4 geklickt!",1)
    Case $Minimize
    GUISetState(@SW_MINIMIZE)
    Case $Close
    ExitLoop
    ; Case $GUI_EVENT_CLOSE
    ; ExitLoop
    EndSwitch
    WEnd
    Exit

    [/autoit] [autoit][/autoit] [autoit]

    Func GuiRoundCorners($h_win, $i_x1, $i_y1, $i_x3, $i_y3)
    $pos = WinGetPos($h_win)
    $ret = DllCall("gdi32.dll", "long", "CreateRoundRectRgn", "long", $i_x1, "long", $i_y1, "long", $pos[2], "long", $pos[3], "long", $i_x3, "long", $i_y3)
    If $ret[0] Then
    $ret2 = DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $h_win, "long", $ret[0], "int", 1)
    If $ret2[0] Then
    Return 1
    Else
    Return 0
    EndIf
    Else
    Return 0
    EndIf
    EndFunc ;==>GuiRoundCorners

    [/autoit] [autoit][/autoit] [autoit]

    Func GUICtrlCreatePng($hWnd, $sPath, $iX, $iY) ; SEuBo
    _GDIPlus_Startup()
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend, $hGUI
    Local $hImage = _GDIPlus_ImageLoadFromFile($sPath), $iWidth = _GDIPlus_ImageGetWidth($hImage), $iHeight = _GDIPlus_ImageGetHeight($hImage)
    $hGUI = GUICreate("", $iWidth, $iHeight, $iX, $iY, 0x80000000, BitOR(0x40, 0x80000), $hWnd)
    $cLabel = GUICtrlCreateLabel("", 0, 0, $iWidth, $iHeight)
    GUICtrlSetBkColor(-1, -2)
    GUISetState(@SW_SHOWNOACTIVATE, $hGUI)
    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate("int X;int Y")
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $iWidth)
    DllStructSetData($tSize, "Y", $iHeight)
    $tSource = DllStructCreate("int X;int Y")
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate("byte Op;byte Flags;byte Alpha;byte Format")
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", 255)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
    GUISwitch($hWnd)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
    Return SetError(0,$hGUI,$cLabel)
    EndFunc ;==>GUICtrlCreatePng

    [/autoit]

    Für eine Hilfe würde ich mich sehr freunen!

    Hier noch mal mein Script mit den Grafik-Dateien: autoit.de/wcf/attachment/12224/

    mfg
    tv_freeze

  • Ich hab die Funktion von SEuBo mal ein wenig modifiziert, wobei ich das ganze lieber in eine extra Funktion gepackt hätte...
    Naja es funktioniert zumindest :D.

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <GDIPlus.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global Const $Font = "Arial Black", $FontSize = 10, $FontColor = 0x000000, $BgColorTitle = 0xFFFFFF, $BgColor = 0x777799, $TitleHeight = 20, $DefaultCtrlHeight = 16

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $Width = 400
    $Height = 100
    $Title = "ButtonControl"
    $x_Button1 = 8
    $y_Button1 = 30

    [/autoit] [autoit][/autoit] [autoit]

    $hGUI = GUICreate($Title, $Width, $Height, -1, -1, $WS_POPUP)
    GUISetBkColor($BgColor)

    [/autoit] [autoit][/autoit] [autoit]

    $knopt1 = GUICtrLCreatePng($hGUI,@ScriptDir & "\Button.png", $x_Button1, $y_Button1, "Test")
    $knopt2 = GUICtrLCreatePng($hGUI,@ScriptDir & "\Button.png", $x_Button1, $y_Button1+17, "Test 2")
    $knopt3 = GUICtrLCreatePng($hGUI,@ScriptDir & "\Button.png", $x_Button1, $y_Button1+34, "AutoIt")
    $knopt4 = GUICtrLCreatePng($hGUI,@ScriptDir & "\Button.png", $x_Button1, $y_Button1+51)

    [/autoit] [autoit][/autoit] [autoit]

    $Close = GUICtrlCreatePic("close.bmp", $Width - 16, 7, 9, 9)
    $Minimize = GUICtrlCreatePic("minimize.bmp", $Width - 31, 7, 9, 9)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetFont($FontSize, 400, 0, $Font)
    $TitleBar = GUICtrlCreateLabel($Title, 6, 4, $Width - 42, 17, -1, $GUI_WS_EX_PARENTDRAG)
    GUICtrlSetColor(-1, $FontColor)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlCreateGraphic(0, 0, $Width, $TitleHeight)
    GUICtrlSetBkColor(-1, $BgColorTitle)

    [/autoit] [autoit][/autoit] [autoit]

    GuiRoundCorners($hGUI, 0, 0, 2, 2)
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $knopt1
    MsgBox(0,"","Button1 geklickt!",1)
    Case $knopt2
    MsgBox(0,"","Button2 geklickt!",1)
    Case $knopt3
    MsgBox(0,"","Button3 geklickt!",1)
    Case $knopt4
    MsgBox(0,"","Button4 geklickt!",1)
    Case $Minimize
    GUISetState(@SW_MINIMIZE)
    Case $Close
    ExitLoop
    ; Case $GUI_EVENT_CLOSE
    ; ExitLoop
    EndSwitch
    WEnd
    Exit

    [/autoit] [autoit][/autoit] [autoit]

    Func GuiRoundCorners($h_win, $i_x1, $i_y1, $i_x3, $i_y3)
    $pos = WinGetPos($h_win)
    $ret = DllCall("gdi32.dll", "long", "CreateRoundRectRgn", "long", $i_x1, "long", $i_y1, "long", $pos[2], "long", $pos[3], "long", $i_x3, "long", $i_y3)
    If $ret[0] Then
    $ret2 = DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $h_win, "long", $ret[0], "int", 1)
    If $ret2[0] Then
    Return 1
    Else
    Return 0
    EndIf
    Else
    Return 0
    EndIf
    EndFunc ;==>GuiRoundCorners

    [/autoit] [autoit][/autoit] [autoit]

    Func GUICtrlCreatePng($hWnd, $sPath, $iX, $iY, $sString = "", $sFont = "Arial", $iFontSize = 5, $FontColor = 0xFF000000) ; SEuBo
    _GDIPlus_Startup()
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend, $hGUI
    Local $hImage = _GDIPlus_ImageLoadFromFile($sPath), $iWidth = _GDIPlus_ImageGetWidth($hImage), $iHeight = _GDIPlus_ImageGetHeight($hImage)
    Local $hImageContext = _GDIPlus_ImageGetGraphicsContext($hImage)

    [/autoit] [autoit][/autoit] [autoit]

    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    $hFont = _GDIPlus_FontCreate($hFamily, $iFontSize)
    $hBrush = _GDIPlus_BrushCreateSolid($FontColor)
    $aMeasure = _GDIPlus_GraphicsMeasureString($hImageContext, $sString, $hFont, _GDIPlus_RectFCreate(), $hFormat)
    DllStructSetData($aMeasure[0], "X", $iWidth / 2 - DllStructGetData($aMeasure[0], "Width") / 2)
    DllStructSetData($aMeasure[0], "Y", $iHeight / 2 - DllStructGetData($aMeasure[0], "Height") / 2)
    _GDIPlus_GraphicsDrawStringEx($hImageContext, $sString, $hFont, $aMeasure[0], $hFormat, $hBrush)

    [/autoit] [autoit][/autoit] [autoit]

    $hGUI = GUICreate("", $iWidth, $iHeight, $iX, $iY, 0x80000000, BitOR(0x40, 0x80000), $hWnd)
    $cLabel = GUICtrlCreateLabel("", 0, 0, $iWidth, $iHeight)
    GUICtrlSetBkColor(-1, -2)
    GUISetState(@SW_SHOWNOACTIVATE, $hGUI)
    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate("int X;int Y")
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $iWidth)
    DllStructSetData($tSize, "Y", $iHeight)
    $tSource = DllStructCreate("int X;int Y")
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate("byte Op;byte Flags;byte Alpha;byte Format")
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", 255)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
    GUISwitch($hWnd)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_GraphicsDispose($hImageContext)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_Shutdown()
    Return SetError(0,$hGUI,$cLabel)
    EndFunc ;==>GUICtrlCreatePng

    [/autoit]
  • Vielen Dank 'name22' für Deine schnelle Hilfe ! :D

    Ja, jetzt funktioniert es prima...
    Das ist, was ich wollte !

    Was meinst Du mit: in eine extra Funktion packen ?
    Kannst Du das auch ? Ich blick durch diese routine nicht so durch, deshalb ja mein Hilfeaufruf...

    mfg
    tv_freeze

  • Ich wollte eigentlich eine neue Funktion machen, aber jetzt wo ich mir das so anschaue ist es nicht wirklich nötig (abgesehen vom Funktionsnamen passt ja alles halbwegs).^^
    Was verstehst du denn nicht? SEuBos Funktion erstellt eine mehrschichtige Child-GUI in der Haupt-GUI und kopiert das Bild mit _WinAPI_UpdateLayeredWindow rein. Mehrschichtige Fenster unterstützen scheinbar Halbtransparenz, deswegen funktioniert das ganze auch mit halbtransparenten png Dateien.