Jump to content

Creating Buttons with GDI+


Recommended Posts

Hi, i am trying create buttons with GDI, i need help about how can i interact to change color of the button when mouse hover. I think who have a better way than monitor mousecoords and if mouse about "some" coord x,y then change blabla

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

Example()

Func Example()
    Local $hGUI, $hGraphic

    ; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    GUISetState(@SW_SHOW)

    ; Fill a rectangle
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hBrush = _GDIPlus_BrushCreateSolid(0xFF43A6DF)
    $hBrushFont = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    $hFormat = _GDIPlus_StringFormatCreate()
    _GDIPlus_GraphicsFillRect($hGraphic, 10, 10, 100, 100, $hBrush)
    $hFamily = _GDIPlus_FontFamilyCreate("Segoe UI Light")
    $hFont = _GDIPlus_FontCreate($hFamily, 20, 2)
    $tLayout = _GDIPlus_RectFCreate(10, 10, 100, 100)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, "Hello world", $hFont, $tLayout, $hFormat, $hBrushFont)
    ; Loop until the user exits.
    Do
    #cs
    $aCI = GUIGetCursorInfo($hGUI)
    If $aCI[4] = $hFont Then
        MsgBox(0,"","")
    EndIf
    #ce
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Clean up resources
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example
Edited by GordonFreeman
Link to comment
Share on other sites

Use WM_PAINT to draw on the GUI, orelse with your code try to minimize + restore you will find that all the graphics disappear.

For your question check the function CheckPointer in the example.

Example

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

Global $hGraphic, $hBrush, $hBrushFont, $hFont, $hFormat, $hFamily, $hGUI, $tRect_Coords[4] ;would be used in two functions therefore declared as global.
Global $iTheme = 0

Example()

Func Example()

    ; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)

    ; Fill a rectangle
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hBrush = _GDIPlus_BrushCreateSolid(0xAA43A6DF)
    $hBrushFont = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate("Segoe UI Light")
    $hFont = _GDIPlus_FontCreate($hFamily, 20, 2)
    $tRect_Coords[0] = 10
    $tRect_Coords[1] = 10
    $tRect_Coords[2] = 100
    $tRect_Coords[3] = 100

    ;Register for painting
    GUIRegisterMsg($WM_PAINT, "WM_PAINT") ;$WM_PAINT

    GUISetState()

    ; Loop until the user exits.
    Do
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $GUI_EVENT_PRIMARYDOWN
                If CheckPointer($tRect_Coords) Then SetTheme(1)

            Case $GUI_EVENT_PRIMARYUP
                SetTheme(0)

            Case $GUI_EVENT_MOUSEMOVE

                If GetTheme() = 1 Then ContinueLoop

                If CheckPointer($tRect_Coords) Then
                    SetTheme(2)
                Else
                    SetTheme(0)
                EndIf

        EndSwitch

    Until 0

    ; Clean up resources
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_BrushDispose($hBrushFont)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

Func WM_PAINT($hGUI, $iMsg, $wParam, $lParam)

    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)

    ;Your Code must lie below
    ;Paint the string

    _GDIPlus_GraphicsClear($hGraphic, 0xFFF0F0F0)
    _GDIPlus_GraphicsFillRect($hGraphic, $tRect_Coords[0], $tRect_Coords[1], $tRect_Coords[2], $tRect_Coords[3], $hBrush)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, "Hello world", $hFont, _GDIPlus_RectFCreate($tRect_Coords[0], $tRect_Coords[1], $tRect_Coords[2], $tRect_Coords[3]), $hFormat, $hBrushFont)


    ;End of your code

    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)

    Return 0;'GUI_RUNDEFMSG'
EndFunc   ;==>WM_PAINT

Func CheckPointer(ByRef $aiCoords_ClientRel)
    Return _WinAPI_PtInRectEx(_WinAPI_GetMousePosX(True, $hGUI), _WinAPI_GetMousePosY(True, $hGUI), $aiCoords_ClientRel[0], $aiCoords_ClientRel[1], $aiCoords_ClientRel[2] + $aiCoords_ClientRel[0], $aiCoords_ClientRel[3] + $aiCoords_ClientRel[1])
EndFunc   ;==>CheckPointer

Func GetTheme()
    Return $iTheme
EndFunc   ;==>GetTheme

Func SetTheme($Theme, $f_Redraw = true)

    If GetTheme() = $Theme Then Return 1

    If $Theme = 0 Then ;Idle
        _GDIPlus_BrushSetSolidColor($hBrush, 0xAA43A6DF)
        _GDIPlus_BrushSetSolidColor($hBrushFont, 0xFFFFFFFF)

        ;Default Dimensions
        $tRect_Coords[0] = 10
        $tRect_Coords[1] = 10
        $tRect_Coords[2] = 100
        $tRect_Coords[3] = 100

    ElseIf $Theme = 1 Then ;MouseDown
        _GDIPlus_BrushSetSolidColor($hBrush, 0xFF3685B2)
        _GDIPlus_BrushSetSolidColor($hBrushFont, 0xFFFFFFFF)

        ;Compress a Bit
        $tRect_Coords[0] = 12
        $tRect_Coords[1] = 12
        $tRect_Coords[2] = 96
        $tRect_Coords[3] = 96

    ElseIf $Theme = 2 Then ;MouseOver
        _GDIPlus_BrushSetSolidColor($hBrush, 0xBB7BC1E9)
        _GDIPlus_BrushSetSolidColor($hBrushFont, 0xFFFFFFFF)

        ;Enlarge a Bit
        $tRect_Coords[0] = 8
        $tRect_Coords[1] = 8
        $tRect_Coords[2] = 104
        $tRect_Coords[3] = 104

    Else
        Return SetError(1, 0, 0)
    EndIf

    $iTheme = $Theme

    ConsoleWrite("CurTheme: " & $iTheme & @CRLF)
    If $f_Redraw Then _WinAPI_RedrawWindow($hGUI, 0, 0, BitOR($RDW_INTERNALPAINT, $RDW_ERASE))

endfunc   ;==>SetTheme 

Thumbs up if it helped.

Regards :)

Phoenix XL

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...