Jump to content

Recommended Posts

Posted (edited)

Hello everyone!

I found a working example of a _GDIPlus button on the forum. But how do I make rounded edges on the button? (square)

Thanks in advance!

sorry for my english please!

 

#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

 

Edited by SEKOMD
Posted (edited)

It's not the solution for the _GDIPlus problem,
but it does make buttons with rounded corners.

Spoiler
#include <GUIConstants.au3>
#include <Misc.au3>

Local $hDLL = DllOpen("user32.dll") ; _IsPressed

Global $hGUI = GUICreate("GUI")
GUISetStyle(-1, $WS_EX_COMPOSITED)
GUISetBkColor(0x0D1117)

Global $id_1 = _CreateButton("INPUT1", 20, 20, 150, 30, "0xFFD800", 15)
Global $id_2 = _CreateButton("INPUT2", 20, 60, 150, 30, "0xB6FF00")

Global $id_3 = _CreateButton("INPUT3", 20, 100, 150, 35, "0x00FFFF", 10)
Global $id_4 = _CreateButton("INPUT4", 20, 145, 150, 35, "0x0094FF")

Global $id_5 = _CreateButton("INPUT5", 20, 190, 150, 40, "0xFF0000", 10)
Global $id_6 = _CreateButton("INPUT6", 20, 240, 150, 40, "0xFF80EE")

GUISetState()


While True
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $id_1
            ; to react to in mouse up
            While _IsPressed("01", $hDLL)     ; 01 Left mouse button
                Sleep(100)
            WEnd
            ConsoleWrite("$id_1" & @CRLF)

        Case $id_2
            ; to react to in mouse up
            While _IsPressed("01", $hDLL)     ; 01 Left mouse button
                Sleep(100)
            WEnd
            ConsoleWrite("$id_2" & @CRLF)

        Case $id_3
            ConsoleWrite("$id_3" & @CRLF)
        Case $id_4
            ConsoleWrite("$id_4" & @CRLF)
        Case $id_5
            ConsoleWrite("$id_5" & @CRLF)
        Case $id_6
            ConsoleWrite("$id_6" & @CRLF)

    EndSwitch
WEnd
DllClose($hDLL)

;----------------------------------------------------------------------------------------
Func _CreateButton($Text, $Left, $Top, $Width, $Height, $Color, $Corner = 10)
    ; $Corner Valid value [0=Rectangle] to [($Height/2)=Round],  Default=($Height/4)
    ; Validate parameters
    If $Corner < 0 Or $Corner = Default Then $Corner = $Height / 4
    If $Corner > $Height / 2 Then $Corner = $Height / 2
    If $Width <= 0 Or $Height <= 0 Then
        Return SetError(1, 0, 0)
    EndIf
    ; graphic
    GUICtrlCreateGraphic($Left, $Top, $Width, $Height)
    GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $Color, $Color)
    ; body
    GUICtrlSetGraphic(-1, $GUI_GR_RECT, $Corner, 0, $Width - ($Corner * 2), $Height) ; outer part
    GUICtrlSetGraphic(-1, $GUI_GR_RECT, 0, $Corner, $Width, $Height - ($Corner * 2)) ; inner  part
    ; corners
    GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 0, 0, $Corner * 2, $Corner * 2) ; Top-left
    GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, $Width - $Corner * 2, 0, $Corner * 2, $Corner * 2) ; Top-right
    GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 0, $Height - $Corner * 2, $Corner * 2, $Corner * 2) ; Bottom-left
    GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, $Width - $Corner * 2, $Height - $Corner * 2, $Corner * 2, $Corner * 2) ; Bottom-right
    GUICtrlSetState(-1, $GUI_DISABLE)

    $idCtrl = GUICtrlCreateLabel($Text, $Left + $Corner, $Top + ($Height * 0.2), $Width - ($Corner * 2), $Height * 0.6, BitOR($SS_NOTIFY, $SS_CENTER), $WS_EX_COMPOSITED)
    GUICtrlSetFont($idCtrl, $Height * 0.4, 400)
    GUICtrlSetBkColor($idCtrl, $Color)

    Return $idCtrl
EndFunc   ;==>_CreateButton
;----------------------------------------------------------------------------------------

 

with the code from
212533-input-control-with-label-and-rounded-corners

Edited by ioa747

I know that I know nothing

Posted

Here how you could draw a rounded button with GDI+ :

#include <GDIPlus.au3>
#include <GUIConstants.au3>

Example()

Func Example()
  Local $hGUI = GUICreate("GDI+", 500, 300)
  GUISetState(@SW_SHOW)

  _GDIPlus_Startup()
  Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
  _GDIPlus_GraphicsSetSmoothingMode($hGraphic, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ; antialiasing
  _GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF)

  Local $hBrush = _GDIPlus_BrushCreateSolid(0x7F8800AA)
  Local $hPen = _GDIPlus_PenCreate(0xFF8800AA, 2)
  Local $hPen2 = _GDIPlus_PenCreate(0xFFFF0000, 2)

  Local $aPoints[4][2] = [[3]]

  Local $hPath = _GDIPlus_PathCreate()

  $aPoints[1][0] = 0
  $aPoints[1][1] = 35
  $aPoints[2][0] = 5
  $aPoints[2][1] = 5
  $aPoints[3][0] = 35
  $aPoints[3][1] = 0

  _GDIPlus_PathAddCurve($hPath, $aPoints)
  _GDIPlus_PathAddLine($hPath, 35, 0, 150, 0)

  $aPoints[1][0] = 150
  $aPoints[1][1] = 0
  $aPoints[2][0] = 180
  $aPoints[2][1] = 5
  $aPoints[3][0] = 185
  $aPoints[3][1] = 35

  _GDIPlus_PathAddCurve($hPath, $aPoints)

  $aPoints[1][0] = 185
  $aPoints[1][1] = 35
  $aPoints[2][0] = 180
  $aPoints[2][1] = 65
  $aPoints[3][0] = 150
  $aPoints[3][1] = 70

  _GDIPlus_PathAddCurve($hPath, $aPoints)
  _GDIPlus_PathAddLine($hPath, 150, 70, 35, 70)

  $aPoints[1][0] = 35
  $aPoints[1][1] = 70
  $aPoints[2][0] = 5
  $aPoints[2][1] = 65
  $aPoints[3][0] = 0
  $aPoints[3][1] = 35

  _GDIPlus_PathAddCurve($hPath, $aPoints)

  _GDIPlus_GraphicsFillPath($hGraphic, $hPath, $hBrush)
  _GDIPlus_GraphicsDrawPath($hGraphic, $hPath, $hPen)

  Local $hBrushFont = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
  Local $hFormat = _GDIPlus_StringFormatCreate()
  Local $hFamily = _GDIPlus_FontFamilyCreate("Segoe UI Light")
  Local $hFont = _GDIPlus_FontCreate($hFamily, 18, 1)
  _GDIPlus_GraphicsDrawStringEx($hGraphic, "Hello world !", $hFont, _GDIPlus_RectFCreate(20, 20, 160, 40), $hFormat, $hBrushFont)

  Do
  Until GUIGetMsg() = $GUI_EVENT_CLOSE

  _GDIPlus_FontDispose($hFont)
  _GDIPlus_FontFamilyDispose($hFamily)
  _GDIPlus_BrushDispose($hBrushFont)
  _GDIPlus_StringFormatDispose($hFormat)
  _GDIPlus_PathDispose($hPath)
  _GDIPlus_BrushDispose($hBrush)
  _GDIPlus_PenDispose($hPen)
  _GDIPlus_PenDispose($hPen2)
  _GDIPlus_GraphicsDispose($hGraphic)
  _GDIPlus_Shutdown()
EndFunc   ;==>Example

I hardcoded everything since I am not sure if this is what you want.

Posted (edited)

a little different

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

Global $hGraphic, $hBrush, $hBrushFont, $hFont, $hFormat, $hFamily, $hGUI, $tRect_Coords[4]
Global $iTheme = 0

Example()

;---------------------------------------------------------------------------------------
Func Example()

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

    ; Initialize GDI+
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

    $hBrush = _GDIPlus_BrushCreateSolid(0xAA43A6DF) ; Blue
    $hBrushFont = _GDIPlus_BrushCreateSolid(0xFFFFFFFF) ; White
    $hFormat = _GDIPlus_StringFormatCreate()
    _GDIPlus_StringFormatSetAlign($hFormat, 1) ; Horizontal alignment
    _GDIPlus_StringFormatSetLineAlign($hFormat, 1) ; Vertical alignment
    $hFamily = _GDIPlus_FontFamilyCreate("Segoe UI Light")
    $hFont = _GDIPlus_FontCreate($hFamily, 20, 2)

    ; Rectangle coordinates
    $tRect_Coords[0] = 50
    $tRect_Coords[1] = 50
    $tRect_Coords[2] = 200
    $tRect_Coords[3] = 100

    ; Register for painting
    GUIRegisterMsg($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)

    ; Clear the canvas
    _GDIPlus_GraphicsClear($hGraphic, 0xFFF0F0F0)

    ; Create a path for the rounded rectangle
    Local $hPath = _GDIPlus_PathCreate()
    _GDIPlus_PathAddRoundRect($hPath, $tRect_Coords[0], $tRect_Coords[1], $tRect_Coords[2], $tRect_Coords[3], 20)

    ; Fill the rounded rectangle
    _GDIPlus_GraphicsFillPath($hGraphic, $hPath, $hBrush)

    ; Optionally draw the outline
    Local $hPen = _GDIPlus_PenCreate(0xFF3685B2, 2) ; Darker Blue border
    _GDIPlus_GraphicsDrawPath($hGraphic, $hPath, $hPen)

    ; Draw the string centered in the rectangle
    _GDIPlus_GraphicsDrawStringEx($hGraphic, "Click Me", $hFont, _GDIPlus_RectFCreate($tRect_Coords[0], $tRect_Coords[1], $tRect_Coords[2], $tRect_Coords[3]), $hFormat, $hBrushFont)

    ; Clean up
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_PathDispose($hPath)

    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return 0
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[0] + $aiCoords_ClientRel[2], $aiCoords_ClientRel[1] + $aiCoords_ClientRel[3])
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) ; Blue
        $tRect_Coords[0] = 50
        $tRect_Coords[1] = 50

    ElseIf $Theme = 1 Then ; MouseDown
        _GDIPlus_BrushSetSolidColor($hBrush, 0xFF3685B2) ; Darker Blue
        $tRect_Coords[0] = 52
        $tRect_Coords[1] = 52 ; Move slightly down

    ElseIf $Theme = 2 Then ; MouseOver
        _GDIPlus_BrushSetSolidColor($hBrush, 0xBB7BC1E9) ; Lighter Blue
        $tRect_Coords[0] = 50
        $tRect_Coords[1] = 50

    Else
        Return SetError(1, 0, 0)
    EndIf

    $iTheme = $Theme
    If $f_Redraw Then _WinAPI_RedrawWindow($hGUI, 0, 0, BitOR($RDW_INTERNALPAINT, $RDW_ERASE))
EndFunc   ;==>SetTheme
;---------------------------------------------------------------------------------------
Func _GDIPlus_PathAddRoundRect($hPath, $iX, $iY, $iWidth, $iHeight, $iRadius)
    Local $iDiameter = $iRadius * 2
    _GDIPlus_PathAddArc($hPath, $iX, $iY, $iDiameter, $iDiameter, 180, 90)
    _GDIPlus_PathAddLine($hPath, $iX + $iRadius, $iY, $iX + $iWidth - $iRadius, $iY)
    _GDIPlus_PathAddArc($hPath, $iX + $iWidth - $iDiameter, $iY, $iDiameter, $iDiameter, 270, 90)
    _GDIPlus_PathAddLine($hPath, $iX + $iWidth, $iY + $iRadius, $iX + $iWidth, $iY + $iHeight - $iRadius)
    _GDIPlus_PathAddArc($hPath, $iX + $iWidth - $iDiameter, $iY + $iHeight - $iDiameter, $iDiameter, $iDiameter, 0, 90)
    _GDIPlus_PathAddLine($hPath, $iX + $iWidth - $iRadius, $iY + $iHeight, $iX + $iRadius, $iY + $iHeight)
    _GDIPlus_PathAddArc($hPath, $iX, $iY + $iHeight - $iDiameter, $iDiameter, $iDiameter, 90, 90)
    _GDIPlus_PathAddLine($hPath, $iX, $iY + $iHeight - $iRadius, $iX, $iY + $iRadius)
EndFunc   ;==>_GDIPlus_PathAddRoundRect
;---------------------------------------------------------------------------------------

 

Edited by ioa747

I know that I know nothing

Posted

I like the AddArc.  It is simpler to code than AddCurve.

Here my take on your function :

Func _GDIPlus_PathAddRoundRect(ByRef $hPath, $iX, $iY, $iWidth, $iHeight, $iRadius)
    _GDIPlus_PathAddArc($hPath, $iX, $iY, $iRadius, $iHeight, 90, 180)
    _GDIPlus_PathAddArc($hPath, $iX + $iWidth, $iY, $iRadius, $iHeight, -90, 180)
    _GDIPlus_PathCloseFigure($hPath)
EndFunc

Called this way :

_GDIPlus_PathAddRoundRect($hPath, $tRect_Coords[0], $tRect_Coords[1], $tRect_Coords[2], $tRect_Coords[3], 60)

Adding :

_GDIPlus_GraphicsSetSmoothingMode($hGraphic, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ; antialiasing

Make really nice rounded buttons...

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
  • Recently Browsing   0 members

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