SEKOMD Posted January 14 Posted January 14 Hi all! Please help me improve the function code. I can't figure out _GDIPlus at all(( (Coded by UEZ) - when changing the window size - the text disappears; - how to remove flickering when text is frequently updated (flickering is not very frequent); expandcollapse popup;Coded by UEZ #include <GUIConstantsEx.au3> #include <GDIPlus.au3> #include <WindowsConstants.au3> #include <WinApi.au3> #include <ColorConstants.au3> #include <Misc.au3> #include <Array.au3> #include <WinAPISysWin.au3> HotKeySet('{ESC}', '_ButtonExit') Func _ButtonExit() Exit EndFunc ;==>_ButtonExit _GDIPlus_Startup() Global Const $STM_SETIMAGE = 0x0172 Global Const $hGUI = GUICreate("GDI+ Test", 800, 400,Default,Default,$WS_SIZEBOX) GUISetBkColor(0x505050) Global Const $iPicBtn = GUICtrlCreatePic("", 50, 28, 100, 44) GUISetState() Global $aMouseInfo, $bShow = False, $bHide = False ;======== Flickering and text disappears when resizing ============================= While 1 $aButtons = _GDIPlus_BitmapCreateRoundedButtonAndText("AutoIt MSEC:" & @MSEC, 600,200) _WinAPI_DeleteObject(GUICtrlSendMsg($iPicBtn, $STM_SETIMAGE, $IMAGE_BITMAP, $aButtons[0])) ;_WinAPI_DeleteObject(GUICtrlSendMsg($iPicBtn, $STM_SETIMAGE, $IMAGE_BITMAP, $aButtons[1])) ;_WinAPI_RedrawWindow($hGUI, 0, 0, BitOR($RDW_FRAME, $RDW_INVALIDATE, $RDW_ALLCHILDREN)) _WinAPI_InvalidateRgn($hGUI,Default,0) _WinAPI_RedrawWindow($hGUI) WEnd ;==================================================================== 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_BitmapCreateRoundedButtonAndText ; Description ...: Draw rounded button ; Syntax ........: _GDIPlus_BitmapCreateRoundedButtonAndText($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 2025-01-12 ; Modified ......: ; Remarks .......: Dispose returned GDI bitmap handles when done ; Example .......: Yes ; =============================================================================================================================== Func _GDIPlus_BitmapCreateRoundedButtonAndText($sString, $iWidth, $iHeight, $iBgColor = 0xFF1BA0E1, $iFontSize = 86, $sFont = "Tahoma", $iHoverColor = 0xF0FFFFFF, $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_GraphicsSetSmoothingMode($aGfxCtxt[1], $GDIP_SMOOTHINGMODE_HIGHQUALITY) _GDIPlus_GraphicsSetTextRenderingHint($aGfxCtxt[0], $GDIP_TEXTRENDERINGHINTANTIALIASGRIDFIT) ;define brush and pen objects Local Const $hBrushFontColor = _GDIPlus_BrushCreateSolid($iFontColor) ;, $hBrushBGColor = _GDIPlus_BrushCreateSolid($iBgColor) Local Const $hPenFontFrameColor = _GDIPlus_PenCreate($iFontFrameColor, $iFrameSize), $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) _GDIPlus_PathStartFigure($hPath) Local $fArcSize = $iWidth * 0.11 _GDIPlus_PathAddArc($hPath, $iFrameSize, $iHeight - $fArcSize - $iFrameSize, $fArcSize, $fArcSize, 180, -90) ;BR _GDIPlus_PathAddArc($hPath, $iWidth - $fArcSize - $iFrameSize, $iHeight - $fArcSize - $iFrameSize, $fArcSize, $fArcSize, -270, -90) ;BL _GDIPlus_PathAddArc($hPath, $iWidth - $fArcSize - $iFrameSize, $iFrameSize, $fArcSize, $fArcSize, 0, -90) ;TR _GDIPlus_PathAddArc($hPath, $iFrameSize, $iFrameSize, $fArcSize, $fArcSize, -90, -90) ;TL _GDIPlus_PathCloseFigure($hPath) Local Const $hPath_Clone = _GDIPlus_PathClone($hPath) Local Const $hBrushBGColor = _GDIPlus_PathBrushCreateFromPath($hPath) _GDIPlus_PathBrushSetSurroundColor($hBrushBGColor, $iBgColor) _GDIPlus_PathBrushSetCenterColor($hBrushBGColor, 0xFFFFFFFF) _GDIPlus_PathBrushSetCenterPoint($hBrushBGColor, $iWidth / 2, $iHeight / 2) _GDIPlus_PathBrushSetSigmaBlend($hBrushBGColor, 1, 0.02) _GDIPlus_GraphicsFillPath($aGfxCtxt[0], $hPath, $hBrushBGColor) _GDIPlus_GraphicsDrawPath($aGfxCtxt[0], $hPath, $hPenFontFrameColor) _GDIPlus_PathReset($hPath) ;add string to path _GDIPlus_PathAddString($hPath, $sString, $tLayout, $hFamily, 1, $iFontSize, $hFormat) ;clear bitmap and draw string _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_GraphicsDrawPath($aGfxCtxt[1], $hPath_Clone, $hPenHoverColor) ;dispose object resources _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_PathDispose($hPath) _GDIPlus_PathDispose($hPath_Dummy) _GDIPlus_PathDispose($hPath_Clone) _GDIPlus_GraphicsDispose($aGfxCtxt[0]) _GDIPlus_GraphicsDispose($aGfxCtxt[1]) _GDIPlus_BrushDispose($hBrushFontColor) _GDIPlus_BrushDispose($hBrushBGColor) _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 ;==>_GDIPlus_BitmapCreateRoundedButtonAndText sorry my English..!
Nine Posted January 14 Posted January 14 1- no need for invalidate / redraw (that will eliminate flickering) 2- set the initial size of the picture the same as the one you pass to the function (600 x 200) (that will disable resize of the picture when resizing the window) 3- there is a large memory leak (delete all objects in $aButtons) (that will solve the disappearance of the picture after awhile) Those solved for me your issues. SEKOMD 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy
Solution UEZ Posted January 14 Solution Posted January 14 (edited) You may try something like this here: expandcollapse popup;Coded by UEZ #include <Constants.au3> #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global Const $STM_SETIMAGE = 0x0172 _GDIPlus_Startup() Global $iW = 600, $iH = 400, $sText = @MSEC, $bHover = False, $iTimer = 30 Global $iBGColor = 0xFF808080, $iBtnBGColor = 0xFF1BA0E1 Global $hGUI = GUICreate("Test", $iW, $iH, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX, $WS_THICKFRAME)) Global $idPic = GUICtrlCreatePic("", $iW / 4, $iH / 4, $iW / 2, $iH / 2) GUICtrlSetResizing(-1, $GUI_DOCKVCENTER + $GUI_DOCKHCENTER) GUISetBkColor(BitAND($iBGColor, 0xFFFFFF), $hGUI) Global $aSize = ControlGetPos($hGUI, "", $idPic) Global $aHBitmap = _GDIPlus_BitmapCreateRoundedButtonAndText($sText, $iW / 2, $iH / 2, $iBtnBGColor, $aSize[2] / 4, $iBGColor) Global $hB = GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $aHBitmap[0]) If $hB Then _WinAPI_DeleteObject($hB) GUISetState(@SW_SHOW) GUIRegisterMsg($WM_GETMINMAXINFO, "WM_GETMINMAXINFO") GUIRegisterMsg($WM_SIZE, "WM_SIZE") GUIRegisterMsg($WM_TIMER, "Animate") Global $iID = DllCall("user32.dll", "uint_ptr", "SetTimer", "hwnd", $hGUI, "int", 0, "int", $iTimer, "int", 0)[0] While 1 If WinActive($hGUI) Then $aMouseInfo = GUIGetCursorInfo($hGUI) ;hover simulation Switch $aMouseInfo[4] Case $idPic $bHover = True Case Else $bHover = False EndSwitch EndIf Switch GUIGetMsg() Case $GUI_EVENT_CLOSE DllCall("user32.dll", "bool", "KillTimer", "hwnd", $hGUI, "uint_ptr", $iID) GUIRegisterMsg($WM_TIMER, "") GUIRegisterMsg($WM_SIZE, "") GUIRegisterMsg($WM_GETMINMAXINFO, "") _GDIPlus_Shutdown() GUIDelete() Exit Case $idPic GUIRegisterMsg($WM_TIMER, "") MsgBox($MB_TOPMOST, "Information", "Button pressed") GUIRegisterMsg($WM_TIMER, "Animate") EndSwitch WEnd Func Animate() $sText = @MSEC WM_SIZE($hGUI, 0, 0, 0) EndFunc Func WM_SIZE($hWnd, $Msg, $wParam, $lParam) #forceref $Msg, $wParam, $lParam Local $aSize = ControlGetPos($hWnd, "", $idPic) Local $aHBitmap = _GDIPlus_BitmapCreateRoundedButtonAndText($sText, $aSize[2], $aSize[3], $iBtnBGColor, $aSize[2] / 4, $iBGColor) Local $hB = GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $bHover ? $aHBitmap[1] : $aHBitmap[0]) If $hB Then _WinAPI_DeleteObject($hB) _WinAPI_DeleteObject($aHBitmap[0]) _WinAPI_DeleteObject($aHBitmap[1]) Return "GUI_RUNDEFMSG" EndFunc Func WM_GETMINMAXINFO($hWnd, $Msg, $wParam, $lParam) Local $minmaxinfo = DllStructCreate("long ptReserved;long ptReserved;long ptMaxSizeX;long ptMaxSizeY;long ptMaxPositionX;long ptMaxPositionY;long ptMinTrackSizeX;long ptMinTrackSizeY;long ptMaxTrackSizeX;long ptMaxTrackSizeY", $lParam) With $minmaxinfo .ptMinTrackSizeX = 200 .ptMinTrackSizeY = 100 .ptMaxTrackSizeX = @DesktopWidth * 0.8 .ptMaxTrackSizeY = @DesktopHeight * 0.8 EndWith Return "GUI_RUNDEFMSG" EndFunc ;==>WM_GETMINMAXINFO ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GDIPlus_BitmapCreateRoundedButtonAndText ; Description ...: Draw rounded button ; Syntax ........: _GDIPlus_BitmapCreateRoundedButtonAndText($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". ; $iBGGfx - [optional] An integer value. Default is 0xFFF0F0F0 ; $iHoverColor - [optional] An integer value. Default is 0xFFFFFFFF. ; $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 2025-01-14 ; Modified ......: ; Remarks .......: Dispose returned GDI bitmap handles when done ; Example .......: Yes ; =============================================================================================================================== Func _GDIPlus_BitmapCreateRoundedButtonAndText($sString, $iWidth, $iHeight, $iBgColor = 0xFF1BA0E1, $iFontSize = 16, $iBGGfx = 0xFFF0F0F0, $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])] _GDIPlus_GraphicsClear($aGfxCtxt[0], $iBGGfx) _GDIPlus_GraphicsClear($aGfxCtxt[1], $iBGGfx) ;set drawing quality _GDIPlus_GraphicsSetSmoothingMode($aGfxCtxt[0], $GDIP_SMOOTHINGMODE_HIGHQUALITY) _GDIPlus_GraphicsSetSmoothingMode($aGfxCtxt[1], $GDIP_SMOOTHINGMODE_HIGHQUALITY) _GDIPlus_GraphicsSetTextRenderingHint($aGfxCtxt[0], $GDIP_TEXTRENDERINGHINTANTIALIASGRIDFIT) ;define brush and pen objects Local Const $hBrushFontColor = _GDIPlus_BrushCreateSolid($iFontColor) Local Const $hPenFontFrameColor = _GDIPlus_PenCreate($iFontFrameColor, $iFrameSize), $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) ;create rounded borders Local $fArcSize = $iWidth * 0.2 _GDIPlus_PathStartFigure($hPath) _GDIPlus_PathAddArc($hPath, $iFrameSize, $iHeight - $fArcSize - $iFrameSize, $fArcSize, $fArcSize, 180, -90) ;BR _GDIPlus_PathAddArc($hPath, $iWidth - $fArcSize - $iFrameSize, $iHeight - $fArcSize - $iFrameSize, $fArcSize, $fArcSize, -270, -90) ;BL _GDIPlus_PathAddArc($hPath, $iWidth - $fArcSize - $iFrameSize, $iFrameSize, $fArcSize, $fArcSize, 0, -90) ;TR _GDIPlus_PathAddArc($hPath, $iFrameSize, $iFrameSize, $fArcSize, $fArcSize, -90, -90) ;TL _GDIPlus_PathCloseFigure($hPath) Local Const $hPath_Clone = _GDIPlus_PathClone($hPath) ;needed to fill button background and border paint Local Const $hBrushBGColor = _GDIPlus_PathBrushCreateFromPath($hPath) _GDIPlus_PathBrushSetSurroundColor($hBrushBGColor, $iBgColor) _GDIPlus_PathBrushSetCenterColor($hBrushBGColor, 0xFFFFFFFF) _GDIPlus_PathBrushSetCenterPoint($hBrushBGColor, $iWidth / 2, $iHeight / 2) _GDIPlus_PathBrushSetSigmaBlend($hBrushBGColor, 1, 0.33333) _GDIPlus_GraphicsFillPath($aGfxCtxt[0], $hPath, $hBrushBGColor) _GDIPlus_GraphicsDrawPath($aGfxCtxt[0], $hPath, $hPenFontFrameColor) _GDIPlus_PathReset($hPath) ;add string to path _GDIPlus_PathAddString($hPath, $sString, $tLayout, $hFamily, 1, $iFontSize, $hFormat) ;clear bitmap and draw string _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_GraphicsDrawPath($aGfxCtxt[1], $hPath_Clone, $hPenHoverColor) ;dispose object resources _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_PathDispose($hPath) _GDIPlus_PathDispose($hPath_Dummy) _GDIPlus_PathDispose($hPath_Clone) _GDIPlus_GraphicsDispose($aGfxCtxt[0]) _GDIPlus_GraphicsDispose($aGfxCtxt[1]) _GDIPlus_BrushDispose($hBrushFontColor) _GDIPlus_BrushDispose($hBrushBGColor) _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 ;==>_GDIPlus_BitmapCreateRoundedButtonAndText Edited January 15 by UEZ 2025-01-15: Small update to the code SEKOMD 1 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
SEKOMD Posted January 14 Author Posted January 14 1 hour ago, UEZ said: You may try something like this here: thank you, very useful example!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now