GDI+ bild als Button benutzen

  • Halloooo Leute,

    ich hab mich seit gestern mall mit GDI+ bescheftigt.
    Und ich kamm auch gleich ein Problem.
    Ich möchte ein(alle) Bild(er) als button benutzen.

    hier mein aktueller versuch

    Spoiler anzeigen
    [autoit]

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

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    Opt("GUIOnEventMode", 1)

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

    $w = 0
    $h = 0
    $i = 0
    #Region ### START Koda GUI section ### Form=
    $hGUI = GUICreate("Form1", 800, 800, -1, -1,$WS_POPUP)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###
    _GDIPlus_Startup()
    $cDummy1 = GUICtrlCreateDummy()
    While 1
    $i += 1
    $rand = Random (0,1 ,1)

    if $rand == 0 Then
    $rand = "rot.png"
    Else
    $rand = "gruen.png"
    EndIf
    $hBitmap = _GDIPlus_BitmapCreateFromFile ($rand)
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
    _GDIPlus_GraphicsDrawImage ($hGraphic, $hBitmap,$w, $h)
    GUICtrlSetOnEvent(-1, "_test")

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

    $w +=10
    if $w == 800 Then
    $h += 10
    $w = 0
    ElseIf $h == 800 then
    MsgBox (0,0,$i)
    ExitLoop
    EndIf
    WEnd

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

    $cDummy2 = GUICtrlCreateDummy()


    While 1

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

    WEnd

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

    func _test()
    msgbox(0,0,0)
    EndFunc

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

    func _end()
    _GDIPlus_ImageDispose($hBitmap)
    _GDIPlus_Shutdown()
    EndFunc

    [/autoit]

    ich würde mit euch wetten das jemmand mir sagen kann wie ich das machen kann.

    Gruß Nitrama

    PS: die "_ end" funktion geht auch nicht ^^
    PSS: bilder sind im anhang

  • Als erstes mal: Wenn es doch nur Rechtecke sind, why benutzt du dann Bilder?!

    Dann zu deinem OnEvent: Du musst erst OnEventMode auf 1 stellen, dann kannst du GUISetOnEvent benutzen.

    Das ganze Script ist total unübesichtlich und nicht sehr ut gecodet tut mir Leid...

    Aber da es dein erstes GDI+ versuch ist ist es nicht sehr schlimm, so würde ich es machen:

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>

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

    Global $nWidth = 500
    Global $nHeight = 500

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

    $hWnd = GUICreate("Beispiel für Nitrama", $nWidth, $nHeight)
    GUISetOnEvent(-3, "_Exit")
    GUISetState()

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

    _GDIPlus_Startup()
    $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hWnd)

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

    For $i = 1 To 100
    For $j = 1 To 100
    If Random(0, 1, 1) = 0 Then
    $hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
    Else
    $hBrush = _GDIPlus_BrushCreateSolid(0xFF00FF00)
    EndIf
    _GDIPlus_GraphicsFillRect($hGraphics, $i * ($nWidth / 100), $j * ($nHeight / 100), $nWidth / 100, $nHeight / 100, $hBrush)
    _GDIPlus_BrushDispose ($hBrush)
    Next
    Next

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

    While 1

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

    WEnd

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

    Func _Exit()
    _GDIPlus_GraphicsDispose ($hGraphics)
    _GDIPlus_Shutdown()
    Exit
    EndFunc ;==>_Exit

    [/autoit]

    mfg BB

    "IF YOU'RE GOING TO KILL IT
    OPEN SOURCE IT!"

    by Phillip Torrone

    Zitat von Shoutbox

    [Heute, 11:16] Andy: ....böseböseböseböse....da erinnere ich mich daran, dass man den Puschelschwanz eines KaRnickels auch "Blume" nennt....ob da eins zum anderen passt? :rofl: :rofl: :rofl: :rofl:

    https://autoit.de/index.php?page…leIt#post251138

    Neon Snake

  • ich weiß das es kacke ist ^^

    die bilder hab ich genommen weil ich später mal ein kleines spiel machen möchte
    und deswegen die Bilder.^^

    Aber wie kann ich jetzt die bilder als Button benutzen?

  • Du kannst auch ein Bild laden und es in den Button senden. Siehe hier: File to Base64 String Code Generator v1.03 Build 2011-11-21 (1. Beispiel).


    Oder:

    Spoiler anzeigen
    [autoit]


    ;By Malkey
    #include <GuiConstantsEx.au3>
    #include <GDIPlus.au3>
    #include <WindowsConstants.au3>

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

    Opt("MouseCoordMode", 2) ;1=absolute, 0=relative, 2=client

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

    Global $hGUI, $hGraphicGUI, $hBMPBuff

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

    _Main()
    Func _Main()
    Local $hBMP, $hBitmap, $hGraphic, $hImage, $iX, $iY, $hClone, $t, $aMPos
    Local $GuiSizeX = 400, $GuiSizeY = 300
    Local $pngFile = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir") _
    & "\Examples\GUI\Advanced\Images\Button.png"

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

    ; Create GUI
    $hGUI = GUICreate("GDI+", $GuiSizeX, $GuiSizeY)
    ;GUISetBkColor(0xFFFFEF)
    GUISetState()

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

    _GDIPlus_Startup()

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

    $hBitmap = _GDIPlus_ImageLoadFromFile($pngFile)
    $hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)

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

    _GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap, 0, 0)
    GUIRegisterMsg(0xF, "MY_PAINT"); Register PAINT-Event 0x000F = $WM_PAINT (WindowsConstants.au3)
    GUIRegisterMsg(0x85, "MY_PAINT") ; $WM_NCPAINT = 0x0085 (WindowsConstants.au3)Restore after Minimize.
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)

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

    Do
    $aMPos = MouseGetPos()
    If ispressed("01") Then
    Select
    Case _WinAPI_PtInRectEx($aMPos[0], $aMPos[1], 5, 5, 112, 56)
    MsgBox(0, "", " Top button clicked", 1)
    Case _PointInEllipse($aMPos[0], $aMPos[1], 122, 100, 132, 45)
    MsgBox(0, "", "Middle elliptic button clicked", 1)
    Case _WinAPI_PtInRectEx($aMPos[0], $aMPos[1], 5, 192, 112, 243)
    MsgBox(0, "", "Lowest button clicked", 1)
    EndSelect
    EndIf
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

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

    ; Clean up resources
    _GDIPlus_GraphicsDispose($hGraphicGUI)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_BitmapDispose($hBMPBuff)
    _GDIPlus_Shutdown()

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

    EndFunc ;==>_Main

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

    ; ($xPt, $yPt) - x, y position of the point to check
    ; $xTL, $yTL, Top left x Pos, top left Y position of the rectangle encompassing the ellipse.
    ; $w, $h - The width an height of ellipse
    ; http://www.autoitscript.com/forum/index.ph…ndpost&p=639786
    ;
    Func _PointInEllipse($xPt, $yPt, $xTL, $yTL, $w, $h)
    Local $bInside = False, $a = $w / 2, $b = $h / 2
    Local $c1X, $c2X, $dist, $xc = $xTL + $a, $yc = $yTL + $b
    $c1X = $xc - ($a ^ 2 - $b ^ 2) ^ (1 / 2); 1st focal point x position
    $c2X = $xc + ($a ^ 2 - $b ^ 2) ^ (1 / 2); 2nd focal point x position
    $dist = (($xPt - $c1X) ^ 2 + ($yPt - $yc) ^ 2) ^ 0.5 + (($xPt - $c2X) ^ 2 + ($yPt - $yc) ^ 2) ^ 0.5
    If $dist <= $w Then $bInside = Not $bInside
    Return $bInside
    EndFunc ;==>_PointInEllipse

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

    ; ($iX, $iY) - x, y position of the point to check
    ; ($iLeft, $iTop) - x, y position of the top left corner of rectangle
    ; ($iRight, $iBottom) - x, y position of the bottom right corner of rectangle
    ; http://www.autoitscript.com/forum/index.ph…ndpost&p=639786
    ;
    Func _WinAPI_PtInRectEx($iX, $iY, $iLeft, $iTop, $iRight, $iBottom)
    Local $aResult
    Local $tRect = DllStructCreate($tagRECT)
    DllStructSetData($tRect, "Left", $iLeft)
    DllStructSetData($tRect, "Top", $iTop)
    DllStructSetData($tRect, "Right", $iRight)
    DllStructSetData($tRect, "Bottom", $iBottom)
    $aResult = DllCall("User32.dll", "int", "PtInRect", "ptr", DllStructGetPtr($tRect), "int", $iX, "int", $iY)
    If @error Then Return SetError(@error, 0, False)
    Return $aResult[0] <> 0
    EndFunc ;==>_WinAPI_PtInRectEx

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

    ; Copied from ...\Include\Misc.au3 File
    Func IsPressed($sHexKey)
    Local $a_R = DllCall('user32.dll', "int", "GetAsyncKeyState", "int", '0x' & $sHexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
    EndFunc ;==>IsPressed

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

    ;Func to redraw on PAINT MSG
    Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    _WinAPI_RedrawWindow($hGUI, "", "", BitOR($RDW_INVALIDATE, $RDW_FRAME, $RDW_ALLCHILDREN)) ;
    Return $GUI_RUNDEFMSG
    EndFunc ;==>MY_PAINT

    [/autoit]

    Oder:

    Spoiler anzeigen
    [autoit]


    ;By smashly
    #include <GuiConstantsEx.au3>
    #include <GDIPlus.au3>
    #include <WinAPI.au3>

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

    _Main()

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

    Func _Main()
    Local $hGUI, $iPicButton, $hGraphic, $hImage, $hBitmap1, $hGraphic1, $hBMP1, $iMsg
    Local $pngFile = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir") _
    & "\Examples\GUI\Advanced\Images\Button.png"

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

    $hGUI = GUICreate("GDI+", 400, 300)
    GUISetBkColor(0x000000, $hGUI)
    $iPicButton = GUICtrlCreatePic("", 10, 10, 82, 36)
    GUISetState()

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

    _GDIPlus_Startup()

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

    $hImage = _GDIPlus_ImageLoadFromFile($pngFile)
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

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

    $hBitmap1 = _GDIPlus_BitmapCreateFromGraphics(82, 36, $hGraphic)
    $hGraphic1 = _GDIPlus_ImageGetGraphicsContext($hBitmap1)
    _GDIPlus_GraphicsDrawImageRectRect($hGraphic1, $hImage, 11, 145, 82, 36, 0, 0, 82, 36)
    $hBMP1 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap1)

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

    _GDIPlus_GraphicsDispose($hGraphic1)
    _GDIPlus_BitmapDispose($hBitmap1)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

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

    _WinAPI_DeleteObject(GUICtrlSendMsg($iPicButton, 0x0172, 0, $hBMP1))

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

    While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
    Case $GUI_EVENT_CLOSE
    _WinAPI_DeleteObject($hBMP1)
    ExitLoop
    Case $iPicButton
    MsgBox(64, "Picture Clicked", "Yep picture has been clicked")
    EndSwitch
    WEnd
    EndFunc ;==>_Main

    [/autoit]


    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

    Einmal editiert, zuletzt von UEZ (27. November 2011 um 13:03)

  • ich weiß net ob das sooo elenagt ist, aber ich löse das so:

    Spoiler anzeigen
    [autoit]

    #include<GDIPlus.au3>
    $hGui = GUICreate("GDI+ Button", 200, 200)
    $Button = GUICtrlCreateLabel("", 30, 40, 100, 40)
    GUISetState()
    _GDIPlus_Startup()
    $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGui) ; ================
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics(200, 200, $hGraphics) ; Buffer erstellen
    $hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap) ; ================
    $hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000) ; --> Rot
    _GDIPlus_GraphicsFillRect($hBuffer, 30, 40, 100, 40, $hBrush) ; Pseudo Button
    _GDIPlus_GraphicsDrawString($hBuffer, "Button", 40, 50, "Courier New", 12)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 200, 200)
    while 1
    $msg = GUIGetMsg()
    Select
    case $msg = -3
    Exit
    case $msg = $Button
    MsgBox(0, "", "Sie habend en Button gedrückt")
    EndSelect
    WEnd

    [/autoit]

    ich mache ein Label und zeichne darüber den GDIPlus Button ( also das Feld ). Es wird lediglich das Label mit GUIGetMsg() abgefragt ( also ob draufgeklickt wurde ).

    mfg
    Hauke

  • Ich weiß nicht, ob es das ist , was du willst , aber ich hab hier noch was altes gefunden :)

    Spoiler anzeigen
    [autoit]

    #include <GuiConstants.au3>
    #include <GdiPlus.au3>;das include
    #include <Misc.au3>

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

    OnAutoItExitRegister("_end") ; die Funktion _end am Ende des Scriptes ausführen
    _GDIPlus_Startup() ;Gdi starten

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

    Global $x = 100;x und y deklarieren
    Global $y = 100

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

    Global $GUI_Width = 300
    Global $GUI_Height = 280

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

    ;----Bilder laden---------------------
    Global $button = _GDIPlus_ImageLoadFromFile(@ScriptDir & "/Button-Close.png")
    Global $bk = _GDIPlus_ImageLoadFromFile(@ScriptDir & "/bck.png")

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

    $Gui = GUICreate("GDI+ Button Sample", $GUI_Width, $GUI_Height, 300,100)
    ;~ $verhaelnis = $height / $width macht _GDIPlus_ButtonCreate
    $graphic = _GDIPlus_GraphicsCreateFromHWND($Gui);Frontbuffer
    $bitmap = _GDIPlus_BitmapCreateFromGraphics($GUI_Width, $GUI_Height, $graphic)
    $buffer = _GDIPlus_ImageGetGraphicsContext($bitmap);Backbuffer
    GUISetState(@SW_SHOW)
    $Button1 = _GDIPlus_ButtonCreate($buffer, $x, $y, $button, "Button1")

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

    While 1

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

    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch

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

    _Draw()

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

    WEnd

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

    Func _end();die Exit funktion
    _GDIPlus_GraphicsDispose($graphic);Grafik Objekt freigeben
    _GDIPlus_GraphicsDispose($buffer)
    _GDIPlus_BitmapDispose($bitmap)
    ;Bilder freigeben
    _GDIPlus_ImageDispose($button)
    _GDIPlus_ImageDispose($bk)
    _GDIPlus_Shutdown();Ressourcen freigeben
    EndFunc

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

    Func _Draw()
    _GDIPlus_GraphicsClear($buffer);Inhalt des Backbuffer`s wird gelöscht
    _GDIPlus_GraphicsDrawImageRect($buffer,$bk,0,0,$GUI_Width, $GUI_Height)
    _GDIPlus_ButtonCheck($Button1)
    _GDIPlus_GraphicsDrawImage($graphic, $bitmap, 0, 0) ;Backbuffer wird an den Frontbuffer übergeben
    ;Sleep(30)
    EndFunc

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

    Func Button1()
    MsgBox(0, "", "Button1 wurde gedrückt!")
    EndFunc

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

    Func _GDIPlus_ButtonCreate($hGraphics, $x, $y, $hImage, $func = "")
    ; by yxyx
    Local $ImgHeight = _GDIPlus_ImageGetHeight($hImage)
    Local $ImgWidth = _GDIPlus_ImageGetWidth($hImage)
    Local $aInfo[7] = [$hGraphics, $x, $y, $hImage, $ImgWidth, $ImgHeight]
    If $func <> "" Then $aInfo[6] = $func
    Return $aInfo
    EndFunc

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

    Func _GDIPlus_ButtonCheck($data)
    ; by yxyx
    Local $aMouseInfo = GUIGetCursorInfo()
    _GDIPlus_GraphicsDrawImage($data[0], $data[3], $data[1], $data[2])
    If not IsArray($aMouseInfo) Then Return
    If $aMouseInfo[0] > $data[1] and $aMouseInfo[0] < $data[1] + $data[4] and $aMouseInfo[1] > $data[2] and $aMouseInfo[1] < $data[2] + $data[5] and $aMouseInfo[2] = 1 Then
    Call($data[6])
    Sleep(200)
    EndIf
    EndFunc

    [/autoit]
  • Moin,

    Vor geraumer Zeit habe ich auch mal sowas gebastelt. (hat sich inzwischen überholt, aber für den Anfang sicherlich gut)

    Aha, ein Spoiler


    Klick Mich

    Vorteile:
    - Die Buttons sind leicht einzupflegen und abzufragen (eigene msg äquivalent zu guigetmsg)
    - Alles ist komplett animierbar (wie bei .gif Grafiken in Einzelbildern)
    - Man kann 8 Bilder pro Zustand (insg also 24 Bilder) nutzen.

    Nachteile:
    - Man muss jedes Bild selbst erstellen
    - Buttons mit Text müssen auch als Bilder erstellt werden (d.H. ändern der Buttonbeschriftung = Ändern der Bilder)

    Weiteres:
    - Ein nicht animierter Button braucht logischerweise auch nur 3 Bilder statt 24.


    lg
    M