Jump to content

Change button text color on button with image


Go to solution Solved by UEZ,

Recommended Posts

Hi all,

I have a problem with button text color, basically I only want to set button image to some image and then set the text color to red, but what i got is this, please see the picture:

7Ewt7oD.png

And this is the code i use:

Local $Button1 = GUICtrlCreateButton("This fine when color is black", 8, 144, 235, 28);, 8, 144, 235, 25)
Local $Button2 = GUICtrlCreateButton("But not when color is red", 8, 175, 235, 28);, 8, 144, 235, 25)
GUICtrlSetColor($Button2, 0xff0000)
Local $hImagebtn2 = _GUIImageList_Create(235, 28, 5, 3)
_GUIImageList_AddBitmap($hImagebtn2, @ScriptDir & "\include\button.bmp");1 - Normal
_GUICtrlButton_SetImageList($Button1, $hImagebtn2, 4)
_GUICtrlButton_SetImageList($Button2, $hImagebtn2, 4)

Any help would be greatly appreciated

Link to comment
Share on other sites

  • Solution

Try this:

 

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

$hGUI = GUICreate("Test")
Local $Button1 = GUICtrlCreateButton("This fine when color is black", 8, 144, 235, 28);, 8, 144, 235, 25)
Local $Button2 = GUICtrlCreateButton("", 8, 175, 235, 28, $BS_BITMAP);, 8, 144, 235, 25)

_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\include\button.bmp")
Local $hHBitmap = _GDIPlus_CreateTextOnBmp($Button2, "But not when color is red", $hImage)
_GDIPlus_ImageDispose($hImage)

Local $hImagebtn2 = _GUIImageList_Create(235, 28, 5)
_GUIImageList_AddBitmap($hImagebtn2, @ScriptDir & "\include\button.bmp");1 - Normal
_GUICtrlButton_SetImageList($Button1, $hImagebtn2, 4)
_SendMessage(GUICtrlGetHandle($Button2), $BM_SETIMAGE, 0, $hHBitmap)
_WinAPI_UpdateWindow(GUICtrlGetHandle($Button2))
GUISetState()

Do
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            _GUIImageList_Destroy($hImagebtn2)
            _WinAPI_DeleteObject($hHBitmap)
            GUIDelete()
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
Until False

Func _GDIPlus_CreateTextOnBmp($iBtnCtrl, $sTXT, $hImage, $iTxtColor = 0xFFFF0000, $iFontSize = 8.5, $sFont = "Arial", $iAlign = 1, $iFontStyle = 0)
    Local $hCtrl = GUICtrlGetHandle($iBtnCtrl)
    Local $hGUI = _WinAPI_GetAncestor($hCtrl)
    Local $aPos = ControlGetPos($hGUI, "", $iBtnCtrl)
    Local $iW = $aPos[2], $iH = $aPos[3]
    Local $hBitmap = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iW, "int", $iH, "int", 0, "int", $GDIP_PXF32ARGB, "ptr", 0, "int*", 0)
    $hBitmap = $hBitmap[6]
    Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImageRect($hBmpCtxt, $hImage, 0, 0, $iW, $iH)
    Local $hBrush = _GDIPlus_BrushCreateSolid($iTxtColor)
    Local $hFormat = _GDIPlus_StringFormatCreate()
    Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    Local $hFont = _GDIPlus_FontCreate($hFamily, $iFontSize, $iFontStyle)
    Local $tLayout = _GDIPlus_RectFCreate(0, $iH / 2 - $iFontSize, $iW, $iH)
    _GDIPlus_StringFormatSetAlign($hFormat, $iAlign)
    _GDIPlus_GraphicsDrawStringEx($hBmpCtxt, $sTXT, $hFont, $tLayout, $hFormat, $hBrush)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hBmpCtxt)
    Local $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _GDIPlus_BitmapDispose($hBitmap)
    Return $hHBitmap
EndFunc
Br,

UEZ

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