Sign in to follow this  
Followers 0
ActualAkshay

Help in creating Metro style buttons using GDIPlus

11 posts in this topic

#1 ·  Posted (edited)

Hey Guys,
I need help in creating a function that uses GDI+ to  create Metro style buttons, like in Windows 8 Sidebar, i have attached some example below:

post-72083-0-36994200-1387175956_thumb.jpost-72083-0-87725000-1387175962_thumb.j

Earlier I used to design the buttons on photoshop but now I am thinking of doing it through code which would decrease the size of executable, also it would take time once for creating code rather than spending hours on photoshop.

Here is the code I made till now, was able to draw only the button without text, as I suck on GDI+  :ermm:  :

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

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Test", 615, 437)
$Button1 = CtrlCreateButton("Button1", 48, 72, 100, 20)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func CtrlCreateButton($Text = "", $Left = 0, $Top = 0, $Width = 0, $Height = 0, $BGColor = 0xff00ff, $HoverColor = 0xbbbbbb, $FontSize = 10, $FontColor = 0xff456f, $FontFamily ="Arial", $WorkDir = @TempDir)
    _GDIPlus_Startup()
    ;Global $iWidth = 100, $iHeight = 100 ;dimension of the bitmap
    Local $iStride = 0, $pScan0 = 0, $iPixelFormat = $GDIP_PXF32ARGB ;some bitmap parameters
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $Width, "int", $Height, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    Local $hBitmap = $aResult[6] ;this is the handle of the new empty bitmap
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap) ;create a context to the bitmap handle to do some GDI+ operations
    ;Global Const $iBgColor = 0xFFDE2697 ;define background color -> ARGB -> 0xFFDE2697
    _GDIPlus_GraphicsClear($hContext, $BGColor) ;clear empty bitmap with new color

    ;$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hContext)
    ;_GDIPlus_GraphicsDrawStringEx($hContext, $Text, _GDIPlus_FontCreate(_GDIPlus_FontFamilyCreate($FontFamily), $FontSize), _GDIPlus_RectFCreate(), _GDIPlus_StringFormatCreate(), _GDIPlus_BrushCreateSolid($FontColor))

    $sFilename = $WorkDir & "\" & Chr(Random(65, 90, 1)) & Chr(Random(65, 90, 1)) & Random(1000000, 999999, 1)
    _GDIPlus_ImageSaveToFile($hBitmap, $sFilename)
    GUICtrlCreatePic($sFilename, $Left, $Top, $Width, $Height)

    _GDIPlus_GraphicsDispose($hContext)
    _GDIPlus_Shutdown()
EndFunc   ;==>MetroUICtrlCreateButton

Please help me out guys...
Thanks in advance  :bye:

Edited by ActualAkshay

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

 
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>

#include <GDIPlus.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Test", 615, 437, 100, 100, $WS_POPUPWINDOW)
GUISetBkColor(0x000000)
;$Button1 = CtrlCreateButton("Button1", 48, 72, 100, 20)
$Button1 = GUICtrlCreateLabel("Button1", 48, 72, 100, 20, $SS_CENTER)
GUICtrlSetBkColor(-1, 0x20FFFF)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            MsgBox(0,0,"Clicked")
            Exit
    EndSwitch
WEnd

:unsure:

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here something you can play with:

;needs GDIPlus.au3 at least from AutoIt version 3.3.9.23
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>

_GDIPlus_Startup()
Global Const $STM_SETIMAGE = 0x0172
Global Const $hGUI = GUICreate("GDI+ Test", 200, 100)
GUISetBkColor(0x505050)
Global Const $iPicBtn = GUICtrlCreatePic("", 60, 38, 80, 24)
Global $aButtons = _GDIPlus_CreateTextButton("install", 80, 24)
_WinAPI_DeleteObject(GUICtrlSendMsg($iPicBtn, $STM_SETIMAGE, $IMAGE_BITMAP, $aButtons[0]))
GUISetState()

Global $aMouseInfo, $bShow = False, $bHide = False

Do
    If WinActive($hGUI) Then
        $aMouseInfo = GUIGetCursorInfo($hGUI) ;hover simulation
        Switch $aMouseInfo[4]
            Case $iPicBtn
                _WinAPI_DeleteObject(GUICtrlSendMsg($iPicBtn, $STM_SETIMAGE, $IMAGE_BITMAP, $aButtons[1]))
                $bShow = True
                $bHide = False
            Case Else
                _WinAPI_DeleteObject(GUICtrlSendMsg($iPicBtn, $STM_SETIMAGE, $IMAGE_BITMAP, $aButtons[0]))
                $bHide = True
                $bShow = False
        EndSwitch
    EndIf
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            _WinAPI_DeleteObject($aButtons[0])
            _WinAPI_DeleteObject($aButtons[1])
            _GDIPlus_Shutdown()

            Exit
        Case $iPicBtn
            MsgBox(0, "Information", "Button pressed")
    EndSwitch
Until False


; #FUNCTION# ====================================================================================================================
; Name ..........: _GDIPlus_CreateTextButton
; Description ...: Draws via GDI+ a Windows 8 style button with text
; Syntax ........: _GDIPlus_CreateTextButton($sString, $iWidth, $iHeight[, $iBgColor = 0xFF1BA0E1[, $iFontSize = 16[, $sFont = "Times New Roman"[,
;                  $iHoverColor = 0xFFC9388C[, $iFrameSize = 2[, $iFontFrameColor = 0x408AD5EA[, $iFontColor = 0xFFFFFFFF]]]]]])
; Parameters ....: $sString             - A string value.
;                  $iWidth              - An integer value.
;                  $iHeight             - An integer value.
;                  $iBgColor            - [optional] An integer value. Default is 0xFF1BA0E1.
;                  $iFontSize           - [optional] An integer value. Default is 16.
;                  $sFont               - [optional] A string value. Default is "Times New Roman".
;                  $iHoverColor         - [optional] An integer value. Default is 0xFFC9388C.
;                  $iFrameSize          - [optional] An integer value. Default is 2.
;                  $iFontFrameColor     - [optional] An integer value. Default is 0x408AD5EA.
;                  $iFontColor          - [optional] An integer value. Default is 0xFFFFFFFF.
; Return values .: an array with 2 GDI bitmap handles -> [0]: default button, [1]: hover button
; Author ........: UEZ
; Version .......: 0.85 build 2014-05-08
; Modified ......:
; Remarks .......: Dispose returned GDI bitmap handles when done
; Example .......: Yes
; ===============================================================================================================================
Func _GDIPlus_CreateTextButton($sString, $iWidth, $iHeight, $iBgColor = 0xFF1BA0E1, $iFontSize = 16, $sFont = "Times New Roman", $iHoverColor = 0xFFFFFFFF, $iFrameSize = 2, $iFontFrameColor = 0x408AD5EA, $iFontColor = 0xFFFFFFFF)
    ;some checks
    If $sString = "" Then Return SetError(1, 0, 0)
    If Int($iWidth) < $iFrameSize * 2 Then Return SetError(2, 0, 0)
    If Int($iHeight) < $iFrameSize * 2 Then Return SetError(3, 0, 0)

    ;create font objects
    Local Const $hFormat = _GDIPlus_StringFormatCreate()
    Local Const $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iWidth, $iHeight)
    _GDIPlus_StringFormatSetAlign($hFormat, 1) ;center string on X axis
    _GDIPlus_StringFormatSetLineAlign($hFormat, 1) ;center string on Y axis

    ;create bitmap and graphics context handles
    Local Const $aBitmaps[2] = [_GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight), _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)]
    Local Const $aGfxCtxt[2] = [_GDIPlus_ImageGetGraphicsContext($aBitmaps[0]), _GDIPlus_ImageGetGraphicsContext($aBitmaps[1])]

    ;set drawing quality
    _GDIPlus_GraphicsSetSmoothingMode($aGfxCtxt[0], $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    _GDIPlus_GraphicsSetTextRenderingHint($aGfxCtxt[0], $GDIP_TEXTRENDERINGHINT_ANTIALIASGRIDFIT)

    ;define brush and pen objects
    Local Const $hBrushFontColor = _GDIPlus_BrushCreateSolid($iFontColor)
    Local Const $hPenFontFrameColor = _GDIPlus_PenCreate($iFontFrameColor, 1), $hPenHoverColor = _GDIPlus_PenCreate($iHoverColor, $iFrameSize)

    ;create path object
    Local Const $hPath = _GDIPlus_PathCreate()

    ;create cloned path object for string measurement
    Local Const $hPath_Dummy = _GDIPlus_PathClone($hPath)
    _GDIPlus_PathAddString($hPath_Dummy, $sString, $tLayout, $hFamily, 0, $iFontSize, $hFormat)
;~  Local Const $aInfo = _GDIPlus_PathGetWorldBounds($hPath_Dummy)
;~  $tLayout.Y = ($iHeight - $aInfo[3]) / 2 - Ceiling($aInfo[1]) ;center string on Y axis

    ;add string to path
    _GDIPlus_PathAddString($hPath, $sString, $tLayout, $hFamily, 1, $iFontSize, $hFormat)

    ;clear bitmap and draw string
    _GDIPlus_GraphicsClear($aGfxCtxt[0], $iBgColor)
    _GDIPlus_GraphicsFillPath($aGfxCtxt[0], $hPath, $hBrushFontColor)
    _GDIPlus_GraphicsDrawPath($aGfxCtxt[0], $hPath, $hPenFontFrameColor)

    ;draw rectangle on cloned bitmap for hover effect
    _GDIPlus_GraphicsDrawImageRect($aGfxCtxt[1], $aBitmaps[0], 0, 0, $iWidth, $iHeight)
    _GDIPlus_GraphicsDrawRect($aGfxCtxt[1], $iFrameSize / 2, $iFrameSize / 2, $iWidth - $iFrameSize, $iHeight - $iFrameSize, $hPenHoverColor)

    ;dispose object resources
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_PathDispose($hPath)
    _GDIPlus_PathDispose($hPath_Dummy)
    _GDIPlus_GraphicsDispose($aGfxCtxt[0])
    _GDIPlus_GraphicsDispose($aGfxCtxt[1])
    _GDIPlus_BrushDispose($hBrushFontColor)
    _GDIPlus_PenDispose($hPenFontFrameColor)
    _GDIPlus_PenDispose($hPenHoverColor)

    ;create GDI bitmap for later usage
    Local $aHBitmaps[2] = [_GDIPlus_BitmapCreateHBITMAPFromBitmap($aBitmaps[0]), _GDIPlus_BitmapCreateHBITMAPFromBitmap($aBitmaps[1])]

    ;dispose GDI+ bitmaps
    _GDIPlus_BitmapDispose($aBitmaps[0])
    _GDIPlus_BitmapDispose($aBitmaps[1])
    Return $aHBitmaps
EndFunc
Br,

UEZ

Edited by UEZ
2 people like this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi UEZ, sorry for bumping this up but I found 1 bug and I don't know how to update the button image.

So I managed to scavenge some code from other scripts and build a button without the need of doing something in the main loop:

; kyan script, v1 18092014
;I probably don't need all this stuff
#include <GUIConstantsEx.au3>
#include <GUIScrollbars_Ex.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <Constants.au3>
#include <Array.au3>
#NoTrayIcon
;#RequireAdmin
OnAutoItExitRegister("Cleanup")
_GDIPlus_Startup()
Global $bg[1], $text[1], $iPicBtn[1], $aSB_WindowInfo[1][8], $aSB_WindowInfoEx[1][5], $xic[1], $CtrlhWnd_Array[1][4], $HoverProcPtr = 0, $HoverRegister = "", $hHook_HOVER, $aStates[10]
Global Const $STM_SETIMAGE = 0x0172, $aButtons = _GDIPlus_CreateTextButton("Enable", "Disable", 80, 24) ;[0][0 or 1] = 1st color (1=hover), [1][0 or 1] 2nd color (1=hover)
$hGUI = GUICreate("Test GUI", 365 + 2, 290);48*10+8)
GUISetBkColor(0x505050)
GUISetState()
For $j = 0 To 9 ;generating elements background $fundos[], text $textos[] and buttons $iPicBtn[]
    ReDim $text[$j + 1]
    ReDim $bg[$j + 1]
    ReDim $iPicBtn[$j + 1]
    Local $iExitCode, $out, $fi, $hWnd, $iElem
    If Mod($j, 2) = 0 Then
        $aStates[$j] = 1
        $iPicBtn[$j] = CreateButtonHover(275, 17 * ($j + 1) + (31 * $j), 80, 24, $aButtons[1][0], $aButtons[1][0], $aButtons[1][1])
    Else
        $aStates[$j] = 0
        $iPicBtn[$j] = CreateButtonHover(275, 17 * ($j + 1) + (31 * $j), 80, 24, $aButtons[0][0], $aButtons[0][0], $aButtons[0][1])
    EndIf
    $bg[$j] = GUICtrlCreateGraphic(4, 8 * ($j + 1) + (40 * $j), 342, 40)
    GUICtrlSetBkColor(-1, 0x646464)
    $text[$j] = GUICtrlCreateLabel("Label " & $j, 12, 22 * ($j + 1) + (26 * $j), 300, 22)
    GUICtrlSetBkColor(-1, -2) ;Alpha ON
    GUICtrlSetFont(-1, -1, 600, -1, "Verdana", 5)
    GUICtrlSetColor(-1, 0xFFFFFF)
    _WinAPI_DeleteObject(GUICtrlSendMsg($iPicBtn[$j], $STM_SETIMAGE, $IMAGE_BITMAP, $aButtons[0][0]))
    _WinAPI_DeleteObject(GUICtrlSendMsg($iPicBtn[$j], $STM_SETIMAGE, $IMAGE_BITMAP, $aButtons[0][1]))
    _WinAPI_DeleteObject(GUICtrlSendMsg($iPicBtn[$j], $STM_SETIMAGE, $IMAGE_BITMAP, $aButtons[1][0]))
    _WinAPI_DeleteObject(GUICtrlSendMsg($iPicBtn[$j], $STM_SETIMAGE, $IMAGE_BITMAP, $aButtons[1][1]))
Next

_GUIScrollbars_Generate($hGUI, 0, 47 * 10, 1) ;Creating a scrollbar fir to width (356px)and 470px heigh
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN ;you clicked in something
            $iPidBtn = _ControlGetHovered() ;CtrlID of what you had clicked
            $idx = _ArraySearch($iPicBtn, $iPidBtn) ;Search for that CtrlID in your $iPicBtn array
            If $idx <> -1 Then
                MsgBox(0, "Information", "ID:" & $idx & @CRLF & "Btn state:" & $aStates[$idx])
                If Not $aStates[$idx] Then
                    SwitchBtn($iPicBtn[$idx])
                Else
                    SwitchBtn($iPicBtn[$idx], 0)
                EndIf
            EndIf
    EndSwitch
WEnd


; #FUNCTION# ====================================================================================================================
; Name ..........: _GDIPlus_CreateTextButton
; Description ...: Draws via GDI+ a Windows 8 style button with text
; Syntax ........: _GDIPlus_CreateTextButton($sString, $iWidth, $iHeight[, $iBgColor = 0xFF1BA0E1[, $iFontSize = 16[, $sFont = "Times New Roman"[,
;                  $iHoverColor = 0xFFC9388C[, $iFrameSize = 2[, $iFontFrameColor = 0x408AD5EA[, $iFontColor = 0xFFFFFFFF]]]]]])
; Parameters ....: $sString             - A string value.
;                  $iWidth              - An integer value.
;                  $iHeight             - An integer value.
;                  $iBgColor            - [optional] An integer value. Default is 0xFF1BA0E1.
;                  $iFontSize           - [optional] An integer value. Default is 16.
;                  $sFont               - [optional] A string value. Default is "Times New Roman".
;                  $iHoverColor         - [optional] An integer value. Default is 0xFFC9388C.
;                  $iFrameSize          - [optional] An integer value. Default is 2.
;                  $iFontFrameColor     - [optional] An integer value. Default is 0x408AD5EA.
;                  $iFontColor          - [optional] An integer value. Default is 0xFFFFFFFF.
; Return values .: an array with 2 GDI bitmap handles -> [0]: default button, [1]: hover button
; Author ........: UEZ
; Version .......: 0.85 build 2014-05-08
; Modified ......:
; Remarks .......: Dispose returned GDI bitmap handles when done
; Example .......: Yes
; ===============================================================================================================================
Func _GDIPlus_CreateTextButton($sString, $sStringDisable, $iWidth, $iHeight, $iBgColor = 0xFF22B14C, $iBgColorDisable = 0xFFDD0000, $iFontSize = 12, $sFont = "Verdana", $iHoverColor = 0xFFFFFFFF, $iFrameSize = 2, $iFontFrameColor = 0x408AD5EA, $iFontColor = 0xFFFFFFFF)
    ;windows 8 blue color:0xFF1BA0E1
    ;some checks
    If $sString = "" Then Return SetError(1, 0, 0)
    If $sStringDisable = "" Then Return SetError(1, 1, 0)
    If Int($iWidth) < $iFrameSize * 2 Then Return SetError(2, 0, 0)
    If Int($iHeight) < $iFrameSize * 2 Then Return SetError(3, 0, 0)

    ;create font objects
    Local Const $hFormat = _GDIPlus_StringFormatCreate()
    Local Const $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iWidth, $iHeight)
    _GDIPlus_StringFormatSetAlign($hFormat, 1) ;center string on X axis
    _GDIPlus_StringFormatSetLineAlign($hFormat, 1) ;center string on Y axis

    ;create bitmap and graphics context handles
    Local Const $aBitmaps[4] = [_GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight), _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight), _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight), _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)]
    Local Const $aGfxCtxt[4] = [_GDIPlus_ImageGetGraphicsContext($aBitmaps[0]), _GDIPlus_ImageGetGraphicsContext($aBitmaps[1]), _GDIPlus_ImageGetGraphicsContext($aBitmaps[2]), _GDIPlus_ImageGetGraphicsContext($aBitmaps[3])]

    ;set drawing quality
    _GDIPlus_GraphicsSetSmoothingMode($aGfxCtxt[0], $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    _GDIPlus_GraphicsSetSmoothingMode($aGfxCtxt[2], $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    _GDIPlus_GraphicsSetTextRenderingHint($aGfxCtxt[0], $GDIP_TEXTRENDERINGHINT_ANTIALIASGRIDFIT)
    _GDIPlus_GraphicsSetTextRenderingHint($aGfxCtxt[2], $GDIP_TEXTRENDERINGHINT_ANTIALIASGRIDFIT)

    ;define brush and pen objects
    Local Const $hBrushFontColor = _GDIPlus_BrushCreateSolid($iFontColor)
    Local Const $hPenFontFrameColor = _GDIPlus_PenCreate($iFontFrameColor, 1), $hPenHoverColor = _GDIPlus_PenCreate($iHoverColor, $iFrameSize)

    ;create path object
    Local Const $hPath = _GDIPlus_PathCreate()
    Local Const $hPath2 = _GDIPlus_PathCreate()

    ;create cloned path object for string measurement
    Local Const $hPath_Dummy = _GDIPlus_PathClone($hPath)
    Local Const $hPath_Dummy2 = _GDIPlus_PathClone($hPath2)
    _GDIPlus_PathAddString($hPath_Dummy, $sString, $tLayout, $hFamily, 0, $iFontSize, $hFormat)
    _GDIPlus_PathAddString($hPath_Dummy2, $sStringDisable, $tLayout, $hFamily, 0, $iFontSize, $hFormat)
;~  Local Const $aInfo = _GDIPlus_PathGetWorldBounds($hPath_Dummy)
;~  $tLayout.Y = ($iHeight - $aInfo[3]) / 2 - Ceiling($aInfo[1]) ;center string on Y axis

    ;add string to path
    _GDIPlus_PathAddString($hPath, $sString, $tLayout, $hFamily, 1, $iFontSize, $hFormat)
    _GDIPlus_PathAddString($hPath2, $sStringDisable, $tLayout, $hFamily, 1, $iFontSize, $hFormat)

    ;clear bitmap and draw string
    _GDIPlus_GraphicsClear($aGfxCtxt[0], $iBgColor)
    _GDIPlus_GraphicsClear($aGfxCtxt[2], $iBgColorDisable)
    _GDIPlus_GraphicsFillPath($aGfxCtxt[0], $hPath, $hBrushFontColor)
    _GDIPlus_GraphicsFillPath($aGfxCtxt[2], $hPath2, $hBrushFontColor)
    _GDIPlus_GraphicsDrawPath($aGfxCtxt[0], $hPath, $hPenFontFrameColor)
    _GDIPlus_GraphicsDrawPath($aGfxCtxt[2], $hPath2, $hPenFontFrameColor)

    ;draw rectangle on cloned bitmap for hover effect
    _GDIPlus_GraphicsDrawImageRect($aGfxCtxt[1], $aBitmaps[0], 0, 0, $iWidth, $iHeight)
    _GDIPlus_GraphicsDrawImageRect($aGfxCtxt[3], $aBitmaps[2], 0, 0, $iWidth, $iHeight)
    _GDIPlus_GraphicsDrawRect($aGfxCtxt[1], $iFrameSize / 2, $iFrameSize / 2, $iWidth - $iFrameSize, $iHeight - $iFrameSize, $hPenHoverColor)
    _GDIPlus_GraphicsDrawRect($aGfxCtxt[3], $iFrameSize / 2, $iFrameSize / 2, $iWidth - $iFrameSize, $iHeight - $iFrameSize, $hPenHoverColor)

    ;dispose object resources
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_PathDispose($hPath)
    _GDIPlus_PathDispose($hPath2)
    _GDIPlus_PathDispose($hPath_Dummy)
    _GDIPlus_PathDispose($hPath_Dummy2)
    _GDIPlus_GraphicsDispose($aGfxCtxt[0])
    _GDIPlus_GraphicsDispose($aGfxCtxt[1])
    _GDIPlus_GraphicsDispose($aGfxCtxt[2])
    _GDIPlus_GraphicsDispose($aGfxCtxt[3])
    _GDIPlus_BrushDispose($hBrushFontColor)
    _GDIPlus_PenDispose($hPenFontFrameColor)
    _GDIPlus_PenDispose($hPenHoverColor)

    ;create GDI bitmap for later usage
    ;[x][y] x=btn y=state
    Local $aHBitmaps[2][2] = [[_GDIPlus_BitmapCreateHBITMAPFromBitmap($aBitmaps[0]), _GDIPlus_BitmapCreateHBITMAPFromBitmap($aBitmaps[1])],[_GDIPlus_BitmapCreateHBITMAPFromBitmap($aBitmaps[2]), _GDIPlus_BitmapCreateHBITMAPFromBitmap($aBitmaps[3])]]

    ;dispose GDI+ bitmaps
    _GDIPlus_BitmapDispose($aBitmaps[0])
    _GDIPlus_BitmapDispose($aBitmaps[1])
    _GDIPlus_BitmapDispose($aBitmaps[2])
    _GDIPlus_BitmapDispose($aBitmaps[3])
    Return $aHBitmaps
EndFunc   ;==>_GDIPlus_CreateTextButton

Func SwitchBtn($ctrlID, $ENABLE = 1)
    Local $hWnd, $I, $BM_SETIMAGE_HOVER = 0x00F7, $BS_BITMAP_HOVER = 0x00000080, $BS_ICON_HOVER = 0x00000040, _
            $iPID_HOVER = 0, $hmod = _WinAPI_GetModuleHandle(0)
    $hWnd = GUICtrlGetHandle($ctrlID)
    $i = ArraySearch($hWnd)
    If Not ($HoverProcPtr) Then
        $HoverRegister = DllCallbackRegister("ButtonHoverProc", "long", "int;wparam;lparam")
        $HoverProcPtr = DllCallbackGetPtr($HoverRegister)
        $hHook_HOVER = _WinAPI_SetWindowsHookEx($WH_GETMESSAGE, $HoverProcPtr, $hmod, _
                _WinAPI_GetWindowThreadProcessId(_WinAPI_GetParent($hWnd), $iPID_HOVER))
    EndIf
    If Not BitAND(_WinAPI_GetWindowLong($hWnd, $GWL_STYLE), $BS_BITMAP_HOVER) = $BS_BITMAP_HOVER Then
        _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, _WinAPI_GetWindowLong($hWnd, $GWL_STYLE) + $BS_BITMAP_HOVER)
    ElseIf BitAND(_WinAPI_GetWindowLong($hWnd, $GWL_STYLE), $BS_ICON_HOVER) = $BS_ICON_HOVER Then
        _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, _WinAPI_GetWindowLong($hWnd, $GWL_STYLE) - $BS_ICON_HOVER)
    EndIf
    If $ENABLE Then
        _SendMessage($hWnd, 0x00F7, 0, $aButtons[0][0])
        $CtrlhWnd_Array[$i][1] = $aButtons[0][0]
        $CtrlhWnd_Array[$i][2] = $aButtons[0][1]
        $CtrlhWnd_Array[$i][3] = $aButtons[0][1]
    Else
        _SendMessage($hWnd, 0x00F7, 0, $aButtons[1][0])
        $CtrlhWnd_Array[$i][1] = $aButtons[1][0]
        $CtrlhWnd_Array[$i][2] = $aButtons[1][1]
        $CtrlhWnd_Array[$i][3] = $aButtons[1][1]
    EndIf
EndFunc   ;==>SwitchBtn

Func CreateButtonHover($L, $T, $W, $H, $NORMAL_hBMP, $PRESS_hBMP, $OVER_hBMP, $style = 0, $exStyle = 0) ;Left Top Width Height Handles....
    Local $BM_SETIMAGE_HOVER = 0x00F7, $BS_BITMAP_HOVER = 0x00000080, $BS_ICON_HOVER = 0x00000040, _
            $iPID_HOVER = 0, $hmod = _WinAPI_GetModuleHandle(0)
    $controlID = GUICtrlCreateButton("", $L, $T, $W, $H, $style, $exStyle)
    $hWnd = GUICtrlGetHandle($controlID)
    $CtrlhWnd_Array[UBound($CtrlhWnd_Array) - 1][0] = $hWnd
    If Not ($HoverProcPtr) Then
        $HoverRegister = DllCallbackRegister("ButtonHoverProc", "long", "int;wparam;lparam")
        $HoverProcPtr = DllCallbackGetPtr($HoverRegister)
        $hHook_HOVER = _WinAPI_SetWindowsHookEx($WH_GETMESSAGE, $HoverProcPtr, $hmod, _
                _WinAPI_GetWindowThreadProcessId(_WinAPI_GetParent($hWnd), $iPID_HOVER))
    EndIf
    If Not BitAND(_WinAPI_GetWindowLong($hWnd, $GWL_STYLE), $BS_BITMAP_HOVER) = $BS_BITMAP_HOVER Then
        _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, _WinAPI_GetWindowLong($hWnd, $GWL_STYLE) + $BS_BITMAP_HOVER)
    ElseIf BitAND(_WinAPI_GetWindowLong($hWnd, $GWL_STYLE), $BS_ICON_HOVER) = $BS_ICON_HOVER Then
        _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, _WinAPI_GetWindowLong($hWnd, $GWL_STYLE) - $BS_ICON_HOVER)
    EndIf
    _SendMessage($hWnd, $BM_SETIMAGE_HOVER, 0, $NORMAL_hBMP)
    $CtrlhWnd_Array[UBound($CtrlhWnd_Array) - 1][1] = $NORMAL_hBMP
    $CtrlhWnd_Array[UBound($CtrlhWnd_Array) - 1][2] = $PRESS_hBMP
    $CtrlhWnd_Array[UBound($CtrlhWnd_Array) - 1][3] = $OVER_hBMP
    ReDim $CtrlhWnd_Array[UBound($CtrlhWnd_Array) + 1][4]
    Return $controlID
EndFunc   ;==>CreateButtonHover

Func ArraySearch($hWnd)
    For $Element = 0 To UBound($CtrlhWnd_Array) - 1
        If $CtrlhWnd_Array[$Element][0] == $hWnd Then Return $Element
    Next
    Return -1
EndFunc   ;==>ArraySearch

Func ButtonHoverDelete($controlID)
    Local $Temp_Array[1][4], $Test = False
    For $Element = 0 To UBound($CtrlhWnd_Array) - 1
        If $CtrlhWnd_Array[$Element][0] == GUICtrlGetHandle($controlID) Then
            GUICtrlDelete($controlID)
            $Test = True
        Else
            $Temp_Array[UBound($Temp_Array) - 1][0] = $CtrlhWnd_Array[$Element][0]
            $Temp_Array[UBound($Temp_Array) - 1][1] = $CtrlhWnd_Array[$Element][1]
            $Temp_Array[UBound($Temp_Array) - 1][2] = $CtrlhWnd_Array[$Element][2]
            $Temp_Array[UBound($Temp_Array) - 1][3] = $CtrlhWnd_Array[$Element][3]
            ReDim $Temp_Array[UBound($Temp_Array) + 1][4]
        EndIf
    Next
    ReDim $Temp_Array[UBound($Temp_Array) - 1][4]
    $CtrlhWnd_Array = $Temp_Array
    Return $Test
EndFunc   ;==>ButtonHoverDelete

Func ButtonHoverProc($nCode, $wParam, $lParam)
    If $nCode < 0 Then _
            Return _WinAPI_CallNextHookEx($hHook_HOVER, $nCode, $wParam, $lParam)
    Local _
            $WM_MOUSEFIRST_HOVER = 0x0200, _
            $WM_MOUSELEAVE_HOVER = 0x02A3, _
            $WM_LBUTTONDOWN_HOVER = 0x0201, _
            $WM_LBUTTONUP_HOVER = 0x0202, _
            $BM_SETIMAGE_HOVER = 0x00F7
    $Tag_struct = "HWND hwnd;UINT message;WPARAM wParam;LPARAM lParam;" & _
            "DWORD time;LONG y;LONG x"
    $MSG = DllStructCreate($Tag_struct, $lParam)
    $hWnd = DllStructGetData($MSG, 1)
    $uiMsg = DllStructGetData($MSG, 2)
    $Element = ArraySearch($hWnd)
    If $Element <> -1 Then
        Select
            Case $uiMsg = $WM_MOUSEFIRST_HOVER
                _SendMessage($hWnd, $BM_SETIMAGE_HOVER, 0, $CtrlhWnd_Array[$Element][3])
            Case $uiMsg = $WM_MOUSELEAVE_HOVER
                _SendMessage($hWnd, $BM_SETIMAGE_HOVER, 0, $CtrlhWnd_Array[$Element][1])
            Case $uiMsg = $WM_LBUTTONDOWN_HOVER
                _SendMessage($hWnd, $BM_SETIMAGE_HOVER, 0, $CtrlhWnd_Array[$Element][2])
            Case $uiMsg = $WM_LBUTTONUP_HOVER
                _SendMessage($hWnd, $BM_SETIMAGE_HOVER, 0, $CtrlhWnd_Array[$Element][3])
        EndSelect
    EndIf
    Return _WinAPI_CallNextHookEx($hHook_HOVER, $nCode, $wParam, $lParam)
EndFunc   ;==>ButtonHoverProc

Func _ControlGetHovered()
    Local $Old_Opt_MCM = Opt("MouseCoordMode", 1)
    Local $aRet = DllCall("User32.dll", "int", "WindowFromPoint", _
            "long", MouseGetPos(0), _
            "long", MouseGetPos(1))
    $aRet = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $aRet[0])
    Opt("MouseCoordMode", $Old_Opt_MCM)
    Return $aRet[0]
EndFunc   ;==>_ControlGetHovered

Func _GetServicesNames($aArray)
    Local $rret = ''
    For $i = 0 To UBound($aArray) - 1
        Local $iPID = Run(@ComSpec & ' /C sc GetDisplayName ' & $aArray[$i], '', @SW_HIDE, 0x2)
        ProcessWaitClose($iPID)
        $out = StdoutRead($iPID)
        $fi = StringRegExp($out, " = (\V+)", 3)
        If Not @error Then
            ;FileWriteLine(@DesktopDir&"\lsm.txt",@TAB&@TAB&@TAB&'["'&$aArray[$i]&'","'&$fi[0]&'"],')
            $rret &= @TAB & @TAB & @TAB & '["' & $aArray[$i] & '","' & $fi[0] & '"],'
            ConsoleWrite($i & " -> " & $fi[0] & @LF)
        Else
            ConsoleWrite("ERRO:" & $i & @TAB & $aArray[$i] & @LF)
        EndIf
    Next
    Return $rret
EndFunc   ;==>_GetServicesNames

Func Cleanup()
    _WinAPI_UnhookWindowsHookEx($hHook_HOVER)
    DllCallbackFree($HoverRegister)
    _WinAPI_DeleteObject($aButtons[0][0])
    _WinAPI_DeleteObject($aButtons[0][1])
    _WinAPI_DeleteObject($aButtons[1][0])
    _WinAPI_DeleteObject($aButtons[1][1])
    _GDIPlus_Shutdown()
EndFunc   ;==>Cleanup
 

I can't create the button after creating another control that hovers it. (So I must create the button before creating other control (like $bg[] GUICtrlCreateGraphic() )

And I want to update the image after I activated it to "disable" ($aButtons[1][x]) but I don't know how, I already created this function "SwitchBtn()" but ins't updating those buttons

Can you show me how do I update those GDI buttons to other image?

Grettings

Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

:sweating: UEZ we (me) need you!!

[i just need to update a GDI's button, don't know how I should do it]


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

I just flew over your code and did some small test. Found out that something is wrong with that part in the function SwitchBnt:

...
    If $ENABLE Then
        _SendMessage($hWnd, 0x00F7, 0, $aButtons[0][0])
        $CtrlhWnd_Array[$i][1] = $aButtons[0][0]
        $CtrlhWnd_Array[$i][2] = $aButtons[0][1]
        $CtrlhWnd_Array[$i][3] = $aButtons[0][1]
    Else
        _SendMessage($hWnd, 0x00F7, 0, $aButtons[1][0])
        $CtrlhWnd_Array[$i][1] = $aButtons[1][0]
        $CtrlhWnd_Array[$i][2] = $aButtons[1][1]
        $CtrlhWnd_Array[$i][3] = $aButtons[1][1]
    EndIf
...

I need to understand what you did to fix it. Maybe you can do it faster...

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I created 2 buttons with your _GDIPlus_CreateTextButton() function, then I took _GDIPlus_BitmapCreateHBITMAPFromBitmap() output, and created a 2d array, with [0][0]=Activate button normal state; [0][1]=activate button hover state; [1][0]=disable button normal state; [1][1]=disable button hover state (I'm sayin state but is a picture)

then, the SwitchBtn will change the button for the case I need, for example, I want it disable (button disable $aButtons[0][0]) , call this function with $enable parameter = 0

Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

As mentioned above there is a bug in this section.

Try this:

If $ENABLE Then
        _SendMessage($hWnd, 0x00F7, 0, $aButtons[1][0])
        $CtrlhWnd_Array[$i][1] = $aButtons[1][0]
        $CtrlhWnd_Array[$i][2] = $aButtons[1][1]
        $CtrlhWnd_Array[$i][3] = $aButtons[1][1]
        $aStates[$i] = 1
    Else
        _SendMessage($hWnd, 0x00F7, 0, $aButtons[0][0])
        $CtrlhWnd_Array[$i][1] = $aButtons[0][0]
        $CtrlhWnd_Array[$i][2] = $aButtons[0][1]
        $CtrlhWnd_Array[$i][3] = $aButtons[0][1]
        $aStates[$i] = 0
    EndIf

Br,

UEZ

1 person likes this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

So, SwitchBtn function was properly refreshing the selected CtrlID but I mess up with that "If", LOL, I feel ashamed by not seeing that.

Thank you so much UEZ, this was getting a pain in the ass


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

...

Thank you so much UEZ, this was getting a pain in the ass

 

U r welcome. 

Try this:

0068113176946_500X500.jpg

Br,

UEZ  ;)

1 person likes this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Literately, Literately UEZ


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
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
Sign in to follow this  
Followers 0