Jump to content

Add a bevel to image using GDIPlus


Recommended Posts

I have a function that creates an image of a specified colour. The image is saved and all works OK. What I would like to know is if it is possible to add effects to the image using GDI+ or if it is not built for that. I want the final image to be bevelled akin to what a progress bar looks like.?

#include <GDIPlus.au3>

_GDIPlus_Startup()

Local $sFpath = __GDIp_CreateProgressColourImage('#AC9090')
If Not @error Then ShellExecute($sFpath)

_GDIPlus_Shutdown()

Func __GDIp_CreateProgressColourImage($vColor)
    $vColor = __GDIp_ColorToARGB($vColor) ; convert to argb for gdi funcs

    Local $sImgPath = @TempDir & '\GDIp_Pbar' & $vColor & '.png' ; create a file path
    If FileExists($sImgPath) Then Return $sImgPath ; if it exists, don't re-create

    Local $iExtended = 0
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0(200, 25)

    If Not @error Then
        ; Get a graphics context from the bitmap
        Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)

        If Not @error Then ; clear the graphic to the specified colour
            _GDIPlus_GraphicsClear($hContext, $vColor)

            If Not @error Then ; Save the bitmap to the specified file path
                _GDIPlus_ImageSaveToFile($hBitmap, $sImgPath)
                If @error Then $iExtended = @error
            Else
                $iExtended = @error
            EndIf
            ; Clean up resources
            _GDIPlus_GraphicsDispose($hContext)
            _GDIPlus_BitmapDispose($hBitmap)
        Else
            $iExtended = @error
        EndIf
    Else
        $iExtended = @error
    EndIf

    If Not $iExtended Then Return $sImgPath

    Return SetError(1, @extended, '')
EndFunc   ;==>__GDIp_CreateProgressColourImage

Func __GDIp_ColorToARGB($vColor)
    Local $iDefault = '0xFFFFFFFF'

    If IsString($vColor) Then
        If StringRegExp($vColor, "^(0x|#)?[0-9A-Fa-f]{6,8}$") Then ; valid format
            $vColor = StringRegExpReplace($vColor, "^(?:0x|#)?(.*)$", "$1") ; Trim "0x" prefix using regex if present

            If StringLen($vColor) == 6 Then $vColor = "FF" & $vColor ; Convert to ARGB format (add "FF" for alpha)
            $vColor = "0x" & $vColor
        Else
            $vColor = $iDefault
        EndIf
    ElseIf IsInt($vColor) Then ; Convert integer to hexadecimal string
        $vColor = Hex($vColor, 8)

        If StringLeft($vColor, 2) = '00' Then ; it's actually a 6-digit RGB value padded to 8 digits
            $vColor = StringMid($vColor, 3, 6)
            $vColor = 'FF' & $vColor
        EndIf

        $vColor = "0x" & $vColor ; Return in ARGB format
    Else ; Invalid input type
        $vColor = $iDefault
    EndIf

    Return $vColor
EndFunc   ;==>__GDIp_ColorToARGB

 

Link to comment
Share on other sites

10 minutes ago, benners said:

I want the final image to be bevelled akin to what a progress bar looks like.?

I cannot image what you mean. Do you have an example how it should look like?

 

Here some progress bars:

 

 

Edited by 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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Well I obviously overestimated my abilities and underestimated GDI+. They're amazing, make mine look rubbish :x Initially I only wanted a progress with text for a gui project. I tried a simple label one but it flickered

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

$Form1 = GUICreate("Form1", 350, 50, 811, 345)

$Progress = GUICtrlCreateLabel("", 97, 10, 0, 20)
GUICtrlSetBkColor(-1, 0xBD1EA)

GUICtrlCreateLabel("SciTE.exe", 5, 5, 300, 23, $SS_CENTERIMAGE, $WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)

GUISetState(@SW_SHOW)

For $i = 1 To 300
    GUICtrlSetPos($Progress, 5, 5, $i, 22)
    Sleep(10)
Next

Exit

Then I searched the hdd and found one from progandy that met my needs. I set about trying to under stand it and modifying it. It works but I keep adding things. The one I used comes with some jpgs. I thought I could add any colour and make then look similar to those. The UDF I was making is not finished but I'll add in case it may help with the topic. Something I have done may affect your working code. It' s the first time I've dabbled with gdi so it's a case of fake  it till you make it currently.

 

green.jpg

yellow.jpg

GDIpProgress.au3

Link to comment
Share on other sites

@UEZ I'll probably incorporate the code from _GDIPlus_StripProgressbar.au3 instead of creating images. It will be easier to use colours. Is it possible to alter the code so that it can act as a marquee style as well.  I would think it would need the widths modifying but I would have no idea where to start.

I am going to try and break up your code so it uses the array I have in mine. This would stop the need to delete the gdi resources, unless they need to be deleted every time?

Link to comment
Share on other sites

Well I managed to get the progress to act like a marquee style by altering this line

_GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $fPerc / 100 * $iW, $iH, $hTextureBrush)

to

_GDIPlus_GraphicsFillRect($hCtxt, $fPerc / 100 * $iW, 0, 75, $iH, $hTextureBrush)

and altering the % value in PlayAnim. It does judder sometimes so that needs improving. The code that draws the stripes will need updating if I decide to keep them in marquee mode.

Link to comment
Share on other sites

Here something you can play with:

;Coded by UEZ build 2024-06-25
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

_GDIPlus_Startup() ;initialize GDI+
Global Const $iWidth = 600, $iHeight = 300, $iBgColor = 0x303030 ;$iBgColor format RRGGBB

Global $hGUI = GUICreate("GDI+ Test", $iWidth, $iHeight) ;create a test GUI
GUISetBkColor($iBgColor, $hGUI) ;set GUI background color
GUISetState(@SW_SHOW)

Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle
_GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;sets the graphics object rendering quality (antialiasing)

Global $iW = $iWidth * 0.8, $iH = 30
Global $hImage_PB1 = _GDIPlus_BitmapCreateProgressbar($iW, $iH, 0xFFFFFF00, 0, 0, 1)
Global $hImage_PB2 = _GDIPlus_BitmapCreateProgressbar($iW, $iH, 0xFF400000, 0xFFFF4040, 0, 2)
Global $hImage_PB3 = _GDIPlus_BitmapCreateProgressbar($iW, $iH)

_GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage_PB1, ($iWidth - $iW) / 2, $iHeight * 0.10, $iW, $iH)
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage_PB2, ($iWidth - $iW) / 2, $iHeight * 0.40, $iW, $iH)
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage_PB3, ($iWidth - $iW) / 2, $iHeight * 0.70, $iW, $iH)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

;cleanup GDI+ resources
_GDIPlus_ImageDispose($hImage_PB1)
_GDIPlus_ImageDispose($hImage_PB2)
_GDIPlus_ImageDispose($hImage_PB3)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hGUI)

Func _GDIPlus_BitmapCreateProgressbar($iW, $iH, $iStartColor = 0xFF004000, $iEndColor = 0xFF40FF40, $iCenterColor = 0xF0E0FFE0, $iMode = 3, $iBlur = 5, $iWrapMode = 3)
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
    Local Const $hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hCtxt, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    Local $hBrush, $hPath = _GDIPlus_PathCreate()
    Switch $iMode
        Case 1
            $hBrush = _GDIPlus_BrushCreateSolid($iStartColor)
        Case 2
            Local $tRECTF = _GDIPlus_RectFCreate(0, 0, $iW, $iH / 2)
            $hBrush = _GDIPlus_LineBrushCreateFromRectWithAngle($tRECTF, $iStartColor, $iEndColor, 90, False, $iWrapMode)
            _GDIPlus_LineBrushSetGammaCorrection($hBrush, True)
        Case 3
            $hBrush = _GDIPlus_BrushCreateSolid($iStartColor)
            _GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $iW, $iH, $hBrush)
            _GDIPlus_BrushDispose($hBrush)
            _GDIPlus_PathAddRectangle($hPath, 0, 0, $iW, $iH)
            $hBrush = _GDIPlus_PathBrushCreateFromPath($hPath)
            Local $aColors[3] = [2, $iStartColor, $iEndColor]
            _GDIPlus_PathBrushSetSurroundColorsWithCount($hBrush, $aColors)
            _GDIPlus_PathBrushSetCenterColor($hBrush, $iCenterColor)
            _GDIPlus_PathBrushSetCenterPoint($hBrush, $iW / 2, $iH / 2)
            _GDIPlus_PathBrushSetWrapMode($hBrush, $iWrapMode)
            _GDIPlus_PathBrushSetGammaCorrection($hBrush, True)
    EndSwitch
    _GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $iW, $iH, $hBrush)
    _GDIPlus_BrushDispose($hBrush)

    Local $iColor
    Switch $iMode
        Case 1
            $iColor = ColorChange($iStartColor, 0.4)
        Case 2
            $iColor = 0x20FFFFFF
        Case 3
            $iColor = 0x60FFFFFF
    EndSwitch
    $hBrush = _GDIPlus_BrushCreateSolid($iColor)
    _GDIPlus_GraphicsFillRect($hCtxt, 0, $iH * 0.85, $iW, $iH, $hBrush)

    Switch $iMode
        Case 1
            $iColor = ColorChange($iStartColor, 0.5)
        Case 2
            $iColor = ColorChange($iStartColor)
        Case 3
            $iColor = ColorChange($iStartColor)
    EndSwitch

    _GDIPlus_BrushSetSolidColor($hBrush, $iColor)
    _GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $iW * 0.03, $iH, $hBrush)
    _GDIPlus_GraphicsFillRect($hCtxt, $iW * 0.97, 0, $iW, $iH, $hBrush)
    Switch $iMode
        Case 1
            $iColor = ColorChange($iStartColor, 0.1)
        Case 2
            $iColor = 0x40FFFFFF
        Case 3
            $iColor = 0x50FFFFFF
    EndSwitch
    _GDIPlus_BrushSetSolidColor($hBrush, $iColor)
    _GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $iW, $iH * 0.225, $hBrush)

    Local $hEffect = _GDIPlus_EffectCreateBlur($iBlur)
    _GDIPlus_BitmapApplyEffect($hBitmap, $hEffect)
    _GDIPlus_EffectDispose($hEffect)

    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hCtxt)
    _GDIPlus_PathDispose($hPath)
    Return $hBitmap
EndFunc

Func ColorChange($iColor, $fFactor = 0.99)
    Local $iR = BitShift(BitAND($iColor, 0xFF0000), 16), $iG = BitShift(BitAND($iColor, 0xFF00), 8), $iB = BitAND($iColor, 0xFF)
    Return BitOR(BitAND($iColor, 0x20000000), BitShift($iR * $fFactor, -16), BitShift($iG * $fFactor, -8), $iB * $fFactor)
EndFunc

 

Edited by 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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks UEZ. I have learned a bit from butchering yours and ProgAndy's code :D, whether I remember it all after is another matter. I did use your code to create the bitmaps and then thought, why am I doing this just to get images. Your bars were better so just use those. The code I used to save was 

#include-once
#include <GDIPlus.au3>

_GDIPlus_Startup()

Local $vRet = __GDIp_CreateProgressColourImage(400, 25, 0xFFEE5F5B, 0xFFFF0000)
If @error Then MsgBox(0, '', $vRet)
ShellExecute($vRet)
_GDIPlus_Shutdown()

Func __GDIp_CreateProgressColourImage($iWidth, $iHeight, $iFGColor = 0xFFEE5F5B, $iBGColor = Default)
    $iFGColor = __GDIp_ConvertToARGB($iFGColor) ; convert to argb for gdi funcs
    $iBGColor = ($iBGColor = Default) ? ($iFGColor) : (__GDIp_ConvertToARGB($iBGColor))

;~  Local $sImgPath = @TempDir & '\GDIp_Pbar' & $iFGColor & '.png' ; create a file path
    Local $sImgPath = @ScriptDir & '\Pic.png' ; create a file path
;~  If FileExists($sImgPath) Then Return ; if it exists, don't re-create

    ; Creates a bitmap object
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_BitmapCreateFromScan01'))

    ; and retrieves the graphics context of $hBitmap
    Local Const $hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_ImageGetGraphicsContext1'))

    ; Sets high-quality pixel offset mode for better rendering.
    _GDIPlus_GraphicsSetPixelOffsetMode($hCtxt, $GDIP_PIXELOFFSETMODE_HIGHQUALITY)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_GraphicsSetPixelOffsetMode1'))

    _GDIPlus_GraphicsClear($hCtxt, 0xFFF0F0F0)

    Local $hBmp = _GDIPlus_BitmapCreateFromScan0($iHeight * 2, $iHeight)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_BitmapCreateFromScan02'))

    Local Const $hCtxt_Bmp = _GDIPlus_ImageGetGraphicsContext($hBmp)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_ImageGetGraphicsContext2'))

    _GDIPlus_GraphicsSetPixelOffsetMode($hCtxt_Bmp, $GDIP_PIXELOFFSETMODE_HIGHQUALITY)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_GraphicsSetPixelOffsetMode2'))

    _GDIPlus_GraphicsClear($hCtxt_Bmp, $iBGColor)

    Local $hPen = _GDIPlus_PenCreate($iFGColor)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_PenCreate'))

    ; add barbers pole effect
    For $iY = 0 To $iHeight - 1
        _GDIPlus_GraphicsDrawLine($hCtxt_Bmp, $iY, $iY, $iY + $iHeight, $iY, $hPen)
        _GDIPlus_GraphicsDrawLine($hCtxt_Bmp, $iY + 2 * $iHeight, $iY, $iY + 3 * $iHeight, $iY, $hPen)
    Next

    Local $tPoint1 = DllStructCreate("float;float")
    Local $tPoint2 = DllStructCreate("float;float")

    ; Set the fraction of the image height to be used for the highlights
    Local $highlightFraction = 2 ; This will use half the image height for highlights

    ; Draws top highlight
    DllStructSetData($tPoint1, 2, 0) ; y1
    DllStructSetData($tPoint2, 2, $iHeight / $highlightFraction) ; y2

    Local $hLineBrush = DllCall($__g_hGDIPDll, "uint", "GdipCreateLineBrush", "struct*", $tPoint1, "struct*", $tPoint2, "uint", 0xB0FFFFFF, "uint", 0x00FFFFFF, "int", 0, "int*", 0)
    If @error Then Return SetError(0, 0, 'DllCall: GdipCreateLineBrush Failed')

    _GDIPlus_GraphicsFillRect($hCtxt_Bmp, 0, 0, $iWidth, $iHeight / $highlightFraction, $hLineBrush[6])
    If @error Then Return SetError(0, 0, 'DllCall: _GDIPlus_GraphicsFillRect Failed')

    ; Draws bottom highlight
    $highlightFraction = 2

    DllStructSetData($tPoint1, 2, $iHeight) ; y1
    DllStructSetData($tPoint2, 2, ($highlightFraction - 1) * $iHeight / $highlightFraction) ; y2

    $hLineBrush = DllCall($__g_hGDIPDll, "uint", "GdipCreateLineBrush", "struct*", $tPoint1, "struct*", $tPoint2, "uint", 0xB0FFFFFF, "uint", 0x00FFFFFF, "int", 0, "int*", 0)
    If @error Then Return SetError(0, 0, 'DllCall: GdipCreateLineBrush Failed')

;~  _GDIPlus_GraphicsFillRect($hCtxt_Bmp, 0, ($highlightFraction - 1) * $iHeight / $highlightFraction, $iWidth, $iHeight / $highlightFraction, $hLineBrush[6])
;~  If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_GraphicsFillRect2'))

    _GDIPlus_GraphicsFillRect($hCtxt_Bmp, 0, 2 * $iHeight / 3, $iWidth, $iHeight / 3, $hLineBrush[6])
;~  If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_GraphicsFillRect2'))

    Local $hTextureBrush = _GDIPlus_TextureCreate($hBmp)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_TextureCreate'))

    _GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $iWidth, $iHeight, $hTextureBrush)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_GraphicsFillRect3'))

    _GDIPlus_GraphicsSetTextRenderingHint($hCtxt, 4)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_GraphicsSetTextRenderingHint'))

    Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_BitmapCreateHBITMAPFromBitmap'))

    _GDIPlus_ImageSaveToFile($hBitmap, $sImgPath)
    If @error Then Return SetError(1, 0, __GDIp_GetLastError(@extended, '_GDIPlus_ImageSaveToFile'))

    ; clean up
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hCtxt)
    _GDIPlus_GraphicsDispose($hCtxt_Bmp)
    _GDIPlus_BitmapDispose($hBmp)
    _GDIPlus_BrushDispose($hTextureBrush)
    _GDIPlus_BrushDispose($hLineBrush)
    _GDIPlus_BitmapDispose($hBitmap)

    Return $sImgPath
EndFunc   ;==>__GDIp_CreateProgressColourImage

Func __GDIp_ConvertToARGB($vColor)
    Local $iDefault = '0xFFFFFFFF'

    If IsString($vColor) Then
        ; Trim "0x" prefix using regex if present
        $vColor = StringRegExpReplace($vColor, "^(?:0x|#)?(.*)$", "$1")

        ; Validate the string is a valid hexadecimal color
        If Not StringRegExp($vColor, "^(?:[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$") Then  ; Invalid format
            $vColor = $iDefault
        Else ; Check if it's in RGB format (6 digits) or ARGB format (8 digits)
            If StringLen($vColor) == 6 Then $vColor = "FF" & $vColor ; Convert to ARGB format (add "FF" for alpha)
            $vColor = "0x" & $vColor
        EndIf
    ElseIf IsInt($vColor) Then ; Convert integer to hexadecimal string
        $vColor = Hex($vColor, 8)

        If StringLeft($vColor, 2) = '00' Then ; it's actually a 6-digit RGB value padded to 8 digits
            $vColor = StringMid($vColor, 3, 6)
            $vColor = 'FF' & $vColor
        EndIf

        $vColor = "0x" & $vColor ; Return in ARGB format
    Else ; Invalid input type
        $vColor = $iDefault
    EndIf

    Return $vColor
EndFunc   ;==>__GDIp_ConvertToARGB

Func __GDIp_GetLastError($iExtended, $sFunc = '')
    Local $sErrMsg = $sFunc & 'Unknown error (' & $iExtended & ')'
    $sFunc = ($sFunc = '') ? ('') : ($sFunc & ': ')

    Switch $iExtended
        Case $GDIP_ERROK
            $sErrMsg = $sFunc & 'Method call was successful'
        Case $GDIP_ERRGENERICERROR
            $sErrMsg = $sFunc & 'Generic method call error'
        Case $GDIP_ERRINVALIDPARAMETER
            $sErrMsg = $sFunc & 'One of the arguments passed to the method was not valid'
        Case $GDIP_ERROUTOFMEMORY
            $sErrMsg = $sFunc & 'The operating system is out of memory'
        Case $GDIP_ERROBJECTBUSY
            $sErrMsg = $sFunc & 'One of the arguments in the call is already in use'
        Case $GDIP_ERRINSUFFICIENTBUFFER
            $sErrMsg = $sFunc & 'A buffer is not large enough'
        Case $GDIP_ERRNOTIMPLEMENTED
            $sErrMsg = $sFunc & 'The method is not implemented'
        Case $GDIP_ERRWIN32ERROR
            $sErrMsg = $sFunc & 'The method generated a Microsoft Win32 error'
        Case $GDIP_ERRWRONGSTATE
            $sErrMsg = $sFunc & 'The object is in an invalid state to satisfy the API call'
        Case $GDIP_ERRABORTED
            $sErrMsg = $sFunc & 'The method was aborted'
        Case $GDIP_ERRFILENOTFOUND
            $sErrMsg = $sFunc & 'The specified image file or metafile cannot be found'
        Case $GDIP_ERRVALUEOVERFLOW
            $sErrMsg = $sFunc & 'The method produced a numeric overflow'
        Case $GDIP_ERRACCESSDENIED
            $sErrMsg = $sFunc & 'A write operation is not allowed on the specified file'
        Case $GDIP_ERRUNKNOWNIMAGEFORMAT
            $sErrMsg = $sFunc & 'The specified image file format is not known'
        Case $GDIP_ERRFONTFAMILYNOTFOUND
            $sErrMsg = $sFunc & 'The specified font family cannot be found'
        Case $GDIP_ERRFONTSTYLENOTFOUND
            $sErrMsg = $sFunc & 'The specified style is not available for the specified font'
        Case $GDIP_ERRNOTTRUETYPEFONT
            $sErrMsg = $sFunc & 'The font retrieved is not a TrueType font'
        Case $GDIP_ERRUNSUPPORTEDGDIVERSION
            $sErrMsg = $sFunc & 'The installed GDI+ version is incompatible'
        Case $GDIP_ERRGDIPLUSNOTINITIALIZED
            $sErrMsg = $sFunc & 'The GDI+ API is not in an initialized state'
        Case $GDIP_ERRPROPERTYNOTFOUND
            $sErrMsg = $sFunc & 'The specified property does not exist in the image'
        Case $GDIP_ERRPROPERTYNOTSUPPORTED
            $sErrMsg = $sFunc & 'The specified property is not supported'
        Case 30
            $sErrMsg = $sFunc & '(missing)'
        Case 31
            $sErrMsg = $sFunc & 'Failed to create progress bar'
        Case 32
            $sErrMsg = $sFunc & 'Failed to delete progress bar'
        Case Else
    EndSwitch

    Return $sErrMsg
EndFunc   ;==>__GDIp_GetLastError

Funny how the mind focuses on one thing without considering other alternatives. I eventually combined bit's of yours and Andy's to get somewhere near. The only issue I currently have is when the progress bar is a marquee style. Instead of a smooth transition from left to right, the image appears at 0 but at its width of 60 so it looks sudden. The opposite end seems OK. If I tweak the stating position, the end position is out by the same amount.

If you have time I would appreciate any help you can give on this last bit. I should be finished after that and a few more functions and tests. I have attached the udf. The bit that needs looking at is in the __MPB_RefreshProgress function.

MyProgressBar.au3

Link to comment
Share on other sites

Well after playing around with image sizes ,adding, subtracting etc. All it took to get a smoother marquee bar was to change

$nX to $nX - 60

and

If $iPcent > 100 Then $iPcent = 0 to If $iPcent > 145 Then $iPcent = 0 
$nX = ($iPcent = 0) ? (-60) : ($iPcent / 100 * $__g_avPBars[$iID][$eMPB_cWidth] - 60)

seems better

Edited by benners
Link to comment
Share on other sites

Looks nice.

I modified my example above a little bit:

;Coded by UEZ build 2024-06-27
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global Const $STM_SETIMAGE = 0x0172
_GDIPlus_Startup() ;initialize GDI+
Global Const $iWidth = 600, $iHeight = 300, $iBgColor = 0x303030 ;$iBgColor format RRGGBB

Global $hGUI = GUICreate("GDI+ Test", $iWidth, $iHeight) ;create a test GUI
GUISetBkColor($iBgColor, $hGUI) ;set GUI background color
GUISetState(@SW_SHOW)

Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle
_GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;sets the graphics object rendering quality (antialiasing)

Global $iW = $iWidth * 0.8, $iH = 30
Global $aImage_PB1 = _GDIPlus_BitmapCreateProgressbar($iW, $iH, 0xFFFFFF00, 0, 0, 1)
Global $aImage_PB2 = _GDIPlus_BitmapCreateProgressbar($iW, $iH, 0xFF400000, 0xFFFF4040, 0, 2)
Global $aImage_PB3 = _GDIPlus_BitmapCreateProgressbar($iW, $iH, 0xFF004000, 0xFF40FF40, 0xF0E0FFE0, 3, $iW / 2, $iH / 2)

_GDIPlus_GraphicsDrawImageRect($hGraphics, $aImage_PB1[0], ($iWidth - $iW) / 2, $iHeight * 0.10, $iW, $iH)
_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $aImage_PB1[1], 0, 0, $iW / 2, $iH, ($iWidth - $iW) / 2, $iHeight * 0.10, $iW / 2, $iH)

_GDIPlus_GraphicsDrawImageRect($hGraphics, $aImage_PB2[0], ($iWidth - $iW) / 2, $iHeight * 0.40, $iW, $iH)
_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $aImage_PB2[1], 0, 0, $iW / 2, $iH, ($iWidth - $iW) / 2, $iHeight * 0.40, $iW / 2, $iH)

_GDIPlus_GraphicsDrawImageRect($hGraphics, $aImage_PB3[0], ($iWidth - $iW) / 2, $iHeight * 0.70, $iW, $iH)
_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $aImage_PB3[1], 0, 0, $iW / 2, $iH, ($iWidth - $iW) / 2, $iHeight * 0.70, $iW / 2, $iH)

Global $iSleep = 10, $fProgress = 0, $aPos = WinGetPos($hGUI)
Global $hGUI_PB = GUICreate("Progressbar Example", $iW + 10, $iH + 10, -1, $aPos[1] + $aPos[3] - $iH)
Global $iPB = GUICtrlCreatePic("", 5, 5, $iW, $iH), $hPB = GUICtrlGetHandle($iPB)
GUISetState(@SW_SHOW, $hGUI_PB)

GUIRegisterMsg($WM_TIMER, "SetProgressbar")
DllCall("user32.dll", "int", "SetTimer", "hwnd", $hGUI, "int", 0, "int", $iSleep, "int", 0)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

GUIRegisterMsg($WM_TIMER, "")
;cleanup GDI+ resources
_GDIPlus_ImageDispose($aImage_PB1[0])
_GDIPlus_ImageDispose($aImage_PB1[1])
_GDIPlus_ImageDispose($aImage_PB2[0])
_GDIPlus_ImageDispose($aImage_PB2[1])
_GDIPlus_ImageDispose($aImage_PB3[0])
_GDIPlus_ImageDispose($aImage_PB3[1])
_WinAPI_DeleteObject($aImage_PB1[2])
_WinAPI_DeleteObject($aImage_PB1[3])
_WinAPI_DeleteObject($aImage_PB2[2])
_WinAPI_DeleteObject($aImage_PB2[3])
_WinAPI_DeleteObject($aImage_PB3[2])
_WinAPI_DeleteObject($aImage_PB3[3])
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hGUI)

Func SetProgressbar()
    $fProgress = Mod($fProgress + 0.1, 100)
    Local $fP = Max(Min(100, $fProgress), 0)
    Local $fWidth = $fP / 100 * $iW
    Static $cx = 0
    Local $aBitmap = _GDIPlus_BitmapCreateProgressbar($iW, $iH, 0xFF004000, 0xFF40FF40, 0xF0E0FFE0, 3, Abs(Sin($cx / 100) * 2 * $iW) - $iW, $iH / 2)
    $cx += 1
    Local Const $hBmp = _GDIPlus_BitmapCloneArea($aImage_PB3[0], 0, 0, $iW, $iH, $GDIP_PXF32ARGB), _
                $hGfx = _GDIPlus_ImageGetGraphicsContext($hBmp)
    _GDIPlus_GraphicsDrawImageRectRect($hGfx, $aBitmap[1], 0, 0, $fWidth, $iH, 0, 0, $fWidth, $iH)

    Local Const $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
    Local Const $hFormat = _GDIPlus_StringFormatCreate()
    Local Const $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    Local Const $hFont = _GDIPlus_FontCreate($hFamily, $iH / 3, 0)
    Local Const $tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH)
    _GDIPlus_StringFormatSetAlign($hFormat, 1)
    _GDIPlus_StringFormatSetLineAlign($hFormat, 1)
    _GDIPlus_GraphicsDrawStringEx($hGfx, Int($fP) & "%", $hFont, $tLayout, $hFormat, $hBrush)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_FontDispose($hFont)

    Local Const $hBmp_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBmp)   
    Local Const $hB = GUICtrlSendMsg($iPB, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp_GDI)
    If $hB Then _WinAPI_DeleteObject($hB)
    
    _GDIPlus_ImageDispose($hBmp)
    _GDIPlus_GraphicsDispose($hGfx)
    _WinAPI_DeleteObject($hBmp_GDI)
    _GDIPlus_ImageDispose($aBitmap[0])
    _GDIPlus_ImageDispose($aBitmap[1])
    _WinAPI_DeleteObject($aBitmap[2])
    _WinAPI_DeleteObject($aBitmap[3])
EndFunc

Func Max($a, $b)
    Return $a > $b ? $a : $b
EndFunc

Func Min($a, $b)
    Return $a < $b ? $a : $b
EndFunc

Func _GDIPlus_BitmapCreateProgressbar($iW, $iH, $iStartColor = 0xFF004000, $iEndColor = 0xFF40FF40, $iCenterColor = 0xF0E0FFE0, $iMode = 3, $fXC = 0, $fCY = 0, $iBlur = 5, $iWrapMode = 3)
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
    Local Const $hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hCtxt, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    Local $hBrush, $hPath = _GDIPlus_PathCreate()
    Switch $iMode
        Case 1
            $hBrush = _GDIPlus_BrushCreateSolid($iStartColor)
        Case 2
            Local $tRECTF = _GDIPlus_RectFCreate(0, 0, $iW, $iH / 2)
            $hBrush = _GDIPlus_LineBrushCreateFromRectWithAngle($tRECTF, $iStartColor, $iEndColor, 90, False, $iWrapMode)
            _GDIPlus_LineBrushSetGammaCorrection($hBrush, True)
        Case 3
            $hBrush = _GDIPlus_BrushCreateSolid($iStartColor)
            _GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $iW, $iH, $hBrush)
            _GDIPlus_BrushDispose($hBrush)
            _GDIPlus_PathAddRectangle($hPath, 0, 0, $iW, $iH)
            $hBrush = _GDIPlus_PathBrushCreateFromPath($hPath)
            Local $aColors[3] = [2, $iStartColor, $iEndColor]
            _GDIPlus_PathBrushSetSurroundColorsWithCount($hBrush, $aColors)
            _GDIPlus_PathBrushSetCenterColor($hBrush, $iCenterColor)
            _GDIPlus_PathBrushSetCenterPoint($hBrush, $fXC, $fCY)
            _GDIPlus_PathBrushSetWrapMode($hBrush, $iWrapMode)
            _GDIPlus_PathBrushSetGammaCorrection($hBrush, True)
    EndSwitch
    _GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $iW, $iH, $hBrush)
    _GDIPlus_BrushDispose($hBrush)

    Local $iColor
    Switch $iMode
        Case 1
            $iColor = ColorChange($iStartColor, 0.4)
        Case 2
            $iColor = 0x20FFFFFF
        Case 3
            $iColor = 0x60FFFFFF
    EndSwitch
    $hBrush = _GDIPlus_BrushCreateSolid($iColor)
    _GDIPlus_GraphicsFillRect($hCtxt, 0, $iH * 0.85, $iW, $iH, $hBrush)

    Switch $iMode
        Case 1
            $iColor = ColorChange($iStartColor, 0.5)
        Case 2
            $iColor = ColorChange($iStartColor)
        Case 3
            $iColor = ColorChange($iStartColor)
    EndSwitch

    _GDIPlus_BrushSetSolidColor($hBrush, $iColor)
    _GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $iW * 0.03, $iH, $hBrush)
    _GDIPlus_GraphicsFillRect($hCtxt, $iW * 0.97, 0, $iW, $iH, $hBrush)
    Switch $iMode
        Case 1
            $iColor = ColorChange($iStartColor, 0.1)
        Case 2
            $iColor = 0x40FFFFFF
        Case 3
            $iColor = 0x50FFFFFF
    EndSwitch
    _GDIPlus_BrushSetSolidColor($hBrush, $iColor)
    _GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $iW, $iH * 0.225, $hBrush)

    Local $hEffect = _GDIPlus_EffectCreateBlur($iBlur)
    _GDIPlus_BitmapApplyEffect($hBitmap, $hEffect)
    _GDIPlus_EffectDispose($hEffect)

    Local $hBitmap_Bg = _GDIPlus_BitmapCloneArea($hBitmap, 0, 0, $iW, $iH, $GDIP_PXF32ARGB)
    Local $tColorMatrix = _GDIPlus_ColorMatrixCreateGrayScale()
    $hEffect = _GDIPlus_EffectCreateColorMatrix($tColorMatrix)
    _GDIPlus_BitmapApplyEffect($hBitmap_Bg, $hEffect)
    _GDIPlus_EffectDispose($hEffect)

    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hCtxt)
    _GDIPlus_PathDispose($hPath)
    Local $aBitmaps[] = [$hBitmap_Bg, $hBitmap, _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap_Bg), _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)]
    Return $aBitmaps
EndFunc

Func ColorChange($iColor, $fFactor = 0.99)
    Local $iR = BitShift(BitAND($iColor, 0xFF0000), 16), $iG = BitShift(BitAND($iColor, 0xFF00), 8), $iB = BitAND($iColor, 0xFF)
    Return BitOR(BitAND($iColor, 0x20000000), BitShift($iR * $fFactor, -16), BitShift($iG * $fFactor, -8), $iB * $fFactor)
EndFunc

 

Edited by 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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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...