Jump to content

Changing button colors on click


Go to solution Solved by JLogan3o13,

Recommended Posts

Hello! I am making a GUI on autoit, I would like the button to change color when selected from default to red.  I have searched the forums and haven't really found what I am looking for.  Any suggestions would be appreciated. Here is my code thus far.

#include "utils.au3"

;Setting Constants
Global $screenHeight=@DesktopHeight
Global $screenWidth=@DesktopWidth
Global $expectedScore=0
Global $errors=0
Global $type
Global $user
Global $pwd
Global $program
Global $exam
Global $platform
Global $passScore
Global $activity

;Can/Will Change these values later
Global $wordRecognitionIntensity=99
Global $tutorialquestions=4
Global $actualquestions=50
Global $SecondsUntilQuestionTimesOut=20
Global $gridSearchIntensity=30
Global $topScreenCutOff=1
Local  $failedQuestions=""

;Starts GUI that asks for login information, exam to take, etc..
$allowInput= True
StartGUI()
$allowInput= False


Func StartGUI()
;========================GUI Items=========================;
$hGUI = GUICreate("Auto Tester", 601, 436, 433, 170)
GUISetBkColor(0xFFFFFF)
$certiportLogo = GUICtrlCreatePic("C:\Users\UKELLKI\Dropbox\AutoIt\Images\certiport.jpg", 0, 0, 600, 75, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
$portalButton = GUICtrlCreateButton("Portal", 24, 120, 115, 25, $WS_GROUP)
GUICtrlSetFont(-1, 10, 400, 0, "Georgia")
GUICtrlSetCursor (-1, 0)
GUICtrlSetColor($portalButton, 0xfffafa)
GUICtrlSetBkColor(-1, 0x808080)
$lmsButton = GUICtrlCreateButton("LMS", 168, 120, 115, 25, $WS_GROUP)
GUICtrlSetFont(-1, 10, 400, 0, "Georgia")
GUICtrlSetCursor (-1, 0)
GUICtrlSetColor($lmsButton, 0xfffafa)
GUICtrlSetBkColor(-1, 0x808080)
$consoleButton = GUICtrlCreateButton("Console", 312, 120, 115, 25, $WS_GROUP)
GUICtrlSetFont(-1, 10, 400, 0, "Georgia")
GUICtrlSetCursor (-1, 0)
GUICtrlSetColor($consoleButton, 0xfffafa)
GUICtrlSetBkColor(-1, 0x808080)
$launcherButton = GUICtrlCreateButton("Exam Launcher", 456, 120, 115, 25, $WS_GROUP)
GUICtrlSetFont(-1, 10, 400, 0, "Georgia")
GUICtrlSetCursor (-1, 0)
GUICtrlSetColor($launcherButton, 0xfffafa)
GUICtrlSetBkColor(-1, 0x808080)
$serviceLabel = GUICtrlCreateLabel("Select Service", 376, 64, 152, 29)
GUICtrlSetFont(-1, 16, 800, 0, "Georgia")
$exitButton = GUICtrlCreateButton("Exit", 456, 376, 115, 30, $WS_GROUP)
GUICtrlSetFont(-1, 14, 400, 0, "Georgia")
GUICtrlSetCursor (-1, 0)
GUICtrlSetColor($exitButton, 0xfffafa)
GUICtrlSetBkColor(-1, 0x000000)
;=======================Hidden Items=======================;
$userLabel = GUICtrlCreateLabel("User Name", 376, 192, 94, 22)
GUICtrlSetFont(-1, 12, 800, 0, "Georgia")
$passwordLabel = GUICtrlCreateLabel("Password", 376, 264, 83, 22)
GUICtrlSetFont(-1, 12, 800, 0, "Georgia")
$iuser = GUICtrlCreateInput("", 376, 232, 193, 21)
$ipassword = GUICtrlCreateInput("123456", 376, 304, 193, 21, 0x0020)
$runLabel = GUICtrlCreateLabel("Run On", 24, 168, 66, 22)
GUICtrlSetFont(-1, 12, 800, 0, "Georgia")
$programLabel = GUICtrlCreateLabel("Program", 24, 232, 76, 22)
GUICtrlSetFont(-1, 12, 800, 0, "Georgia")
$courseLabel = GUICtrlCreateLabel("Course ID", 24, 232, 100, 22)
GUICtrlSetFont(-1, 12, 800, 0, "Georgia")
$courseInput = GUICtrlCreateInput("", 24, 264, 257, 25)
$scoringLabel = GUICtrlCreateLabel("Scoring Pass", 24, 296, 108, 22)
GUICtrlSetFont(-1, 12, 800, 0, "Georgia")
$typeSelect = GUICtrlCreateCombo("", 24, 200, 257, 25)
GUICtrlSetData($typeSelect, "Staging|Test|Prod", "Staging")
$programSelect = GUICtrlCreateCombo("", 24, 264, 257, 25)
GUICtrlSetData($programSelect, "Internet and Computing Core Certification|Adobe Certified Associate|Autodesk Certified User|Certiport Training|CompTIA Strata|HP Accredited Technical Associate|Intuit QuickBooks|Microsoft Certified Educator|Microsoft Office Specialist|Microsoft Technology Associate", "Internet and Computing Core Certification")
$scoreSelect = GUICtrlCreateCombo("", 24, 328, 257, 25)
GUICtrlSetData($scoreSelect, "Smoke Test|1000 Pass|Barely Pass|Barely Fail", "Smoke Test")
$iContinue = GUICtrlCreateButton("Continue", 24, 376, 115, 30, $WS_GROUP)
GUICtrlSetFont(-1, 14, 400, 0, "Georgia")
GUICtrlSetCursor (-1, 0)
GUICtrlSetColor($iContinue, 0xfffafa)
GUICtrlSetBkColor(-1, 0x000000)
$browserLabel = GUICtrlCreateLabel("Select Browser", 24, 168, 125, 22)
GUICtrlSetFont(-1, 12, 800, 0, "Georgia")
$browserCombo = GUICtrlCreateCombo("", 24, 200, 257, 25)
GUICtrlSetData($browserCombo, "Internet Explorer|Mozilla Firefox|Google Chrome|Safari", "Internet Explorer")
$activityLabel = GUICtrlCreateLabel("Select Activity", 24, 168, 126, 22)
GUICtrlSetFont(-1, 12, 800, 0, "Georgia")
$activityCombo = GUICtrlCreateCombo("", 24, 200, 257, 25)
GUICtrlSetData($activityCombo, "Study|Practice|Certify", "Study")
;=======================Hiding Items=======================;
                    GUICtrlSetState($userLabel, $GUI_HIDE)
                    GUICtrlSetState($passwordLabel, $GUI_HIDE)
                    GUICtrlSetState($iuser, $GUI_HIDE)
                    GUICtrlSetState($ipassword, $GUI_HIDE)
                    GUICtrlSetState($runLabel, $GUI_HIDE)
                    GUICtrlSetState($programLabel, $GUI_HIDE)
                    GUICtrlSetState($scoringLabel, $GUI_HIDE)
                    GUICtrlSetState($typeSelect, $GUI_HIDE)
                    GUICtrlSetState($programSelect, $GUI_HIDE)
                    GUICtrlSetState($scoreSelect, $GUI_HIDE)
                    GUICtrlSetState($iContinue, $GUI_HIDE)
                    GUICtrlSetState($browserLabel, $GUI_HIDE)
                    GUICtrlSetState($browserCombo, $GUI_HIDE)
                    GUICtrlSetState($activityLabel, $GUI_HIDE)
                    GUICtrlSetState($activityCombo, $GUI_HIDE)
                    GUICtrlSetState($courseLabel, $GUI_HIDE)
                    GUICtrlSetState($courseInput, $GUI_HIDE)
;================End of GUI Items/Hidden Items================;

GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
                Switch $nMsg
                Case $GUI_EVENT_CLOSE, $exitButton
                    Exit
                Case $portalButton
                    ;This ensures that the fields change when clicking different browser service
                    GUICtrlSetState($userLabel, $GUI_HIDE)
                    GUICtrlSetState($passwordLabel, $GUI_HIDE)
                    GUICtrlSetState($iuser, $GUI_HIDE)
                    GUICtrlSetState($ipassword, $GUI_HIDE)
                    GUICtrlSetState($runLabel, $GUI_HIDE)
                    GUICtrlSetState($programLabel, $GUI_HIDE)
                    GUICtrlSetState($scoringLabel, $GUI_HIDE)
                    GUICtrlSetState($typeSelect, $GUI_HIDE)
                    GUICtrlSetState($programSelect, $GUI_HIDE)
                    GUICtrlSetState($scoreSelect, $GUI_HIDE)
                    GUICtrlSetState($iContinue, $GUI_HIDE)
                    GUICtrlSetState($browserLabel, $GUI_HIDE)
                    GUICtrlSetState($browserCombo, $GUI_HIDE)
                    GUICtrlSetState($activityLabel, $GUI_HIDE)
                    GUICtrlSetState($activityCombo, $GUI_HIDE)
                    GUICtrlSetState($courseLabel, $GUI_HIDE)
                    GUICtrlSetState($courseInput, $GUI_HIDE)
                    ;Clearing the data in the user and password fields
                    GUICtrlSetState($iuser, "")
                    ;Clears password data, automatically set to 123456 *will need to be changed!*
                    ;GUICtrlSetData($ipassword, ""); will need to decomment when launched
                    ;End Reset Function
                    GUICtrlSetState($userLabel, $GUI_SHOW)
                    GUICtrlSetState($passwordLabel, $GUI_SHOW)
                    GUICtrlSetState($iuser, $GUI_SHOW)
                    GUICtrlSetState($ipassword, $GUI_SHOW)
                    GUICtrlSetState($scoringLabel, $GUI_SHOW)
                    GUICtrlSetState($scoreSelect, $GUI_SHOW)
                    GUICtrlSetState($programLabel, $GUI_SHOW)
                    GUICtrlSetState($programSelect, $GUI_SHOW)
                    GUICtrlSetState($runLabel, $GUI_SHOW)
                    GUICtrlSetState($typeSelect, $GUI_SHOW)
                    GUICtrlSetState($iContinue, $GUI_SHOW)
                Case $lmsButton
                    ;This ensures that the fields change when clicking different browser service
                    GUICtrlSetState($userLabel, $GUI_HIDE)
                    GUICtrlSetState($passwordLabel, $GUI_HIDE)
                    GUICtrlSetState($iuser, $GUI_HIDE)
                    GUICtrlSetState($ipassword, $GUI_HIDE)
                    GUICtrlSetState($runLabel, $GUI_HIDE)
                    GUICtrlSetState($programLabel, $GUI_HIDE)
                    GUICtrlSetState($scoringLabel, $GUI_HIDE)
                    GUICtrlSetState($typeSelect, $GUI_HIDE)
                    GUICtrlSetState($programSelect, $GUI_HIDE)
                    GUICtrlSetState($scoreSelect, $GUI_HIDE)
                    GUICtrlSetState($iContinue, $GUI_HIDE)
                    GUICtrlSetState($browserLabel, $GUI_HIDE)
                    GUICtrlSetState($browserCombo, $GUI_HIDE)
                    GUICtrlSetState($activityLabel, $GUI_HIDE)
                    GUICtrlSetState($activityCombo, $GUI_HIDE)
                    GUICtrlSetState($courseLabel, $GUI_HIDE)
                    GUICtrlSetState($courseInput, $GUI_HIDE)
                    ;Clearing the data in the user and password fields
                    GUICtrlSetState($iuser, "")
                    ;Clears password data, automatically set to 123456 *will need to be changed!*
                    ;GUICtrlSetState($ipassword, ""); will need to decomment when launched
                    ;End Reset Function
                    GUICtrlSetState($userLabel, $GUI_SHOW)
                    GUICtrlSetState($passwordLabel, $GUI_SHOW)
                    GUICtrlSetState($iuser, $GUI_SHOW)
                    GUICtrlSetState($ipassword, $GUI_SHOW)
                    GUICtrlSetState($scoringLabel, $GUI_SHOW)
                    GUICtrlSetState($scoreSelect, $GUI_SHOW)
                    GUICtrlSetState($browserLabel, $GUI_SHOW)
                    GUICtrlSetState($browserCombo, $GUI_SHOW)
                    GUICtrlSetState($iContinue, $GUI_SHOW)
                Case $consoleButton
                    ;This ensures that the fields change when clicking different browser service
                    GUICtrlSetState($userLabel, $GUI_HIDE)
                    GUICtrlSetState($passwordLabel, $GUI_HIDE)
                    GUICtrlSetState($iuser, $GUI_HIDE)
                    GUICtrlSetState($ipassword, $GUI_HIDE)
                    GUICtrlSetState($runLabel, $GUI_HIDE)
                    GUICtrlSetState($programLabel, $GUI_HIDE)
                    GUICtrlSetState($scoringLabel, $GUI_HIDE)
                    GUICtrlSetState($typeSelect, $GUI_HIDE)
                    GUICtrlSetState($programSelect, $GUI_HIDE)
                    GUICtrlSetState($scoreSelect, $GUI_HIDE)
                    GUICtrlSetState($iContinue, $GUI_HIDE)
                    GUICtrlSetState($browserLabel, $GUI_HIDE)
                    GUICtrlSetState($browserCombo, $GUI_HIDE)
                    GUICtrlSetState($activityLabel, $GUI_HIDE)
                    GUICtrlSetState($activityCombo, $GUI_HIDE)
                    GUICtrlSetState($courseLabel, $GUI_HIDE)
                    GUICtrlSetState($courseInput, $GUI_HIDE)
                    ;Clearing the data in the user and password fields
                    GUICtrlSetState($iuser, "")
                    ;Clears password data, automatically set to 123456 *will need to be changed!*
                    ;GUICtrlSetState($ipassword, ""); will need to decomment when launched
                    ;End Reset Function
                    GUICtrlSetState($userLabel, $GUI_SHOW)
                    GUICtrlSetState($passwordLabel, $GUI_SHOW)
                    GUICtrlSetState($iuser, $GUI_SHOW)
                    GUICtrlSetState($ipassword, $GUI_SHOW)
                    GUICtrlSetState($scoringLabel, $GUI_SHOW)
                    GUICtrlSetState($scoreSelect, $GUI_SHOW)
                    GUICtrlSetState($activityLabel, $GUI_SHOW)
                    GUICtrlSetState($activityCombo, $GUI_SHOW)
                    GUICtrlSetState($iContinue, $GUI_SHOW)
                Case $launcherButton
                    ;This ensures that the fields change when clicking different browser service
                    GUICtrlSetState($userLabel, $GUI_HIDE)
                    GUICtrlSetState($passwordLabel, $GUI_HIDE)
                    GUICtrlSetState($iuser, $GUI_HIDE)
                    GUICtrlSetState($ipassword, $GUI_HIDE)
                    GUICtrlSetState($runLabel, $GUI_HIDE)
                    GUICtrlSetState($programLabel, $GUI_HIDE)
                    GUICtrlSetState($scoringLabel, $GUI_HIDE)
                    GUICtrlSetState($typeSelect, $GUI_HIDE)
                    GUICtrlSetState($programSelect, $GUI_HIDE)
                    GUICtrlSetState($scoreSelect, $GUI_HIDE)
                    GUICtrlSetState($iContinue, $GUI_HIDE)
                    GUICtrlSetState($browserLabel, $GUI_HIDE)
                    GUICtrlSetState($browserCombo, $GUI_HIDE)
                    GUICtrlSetState($activityLabel, $GUI_HIDE)
                    GUICtrlSetState($activityCombo, $GUI_HIDE)
                    GUICtrlSetState($courseLabel, $GUI_HIDE)
                    GUICtrlSetState($courseInput, $GUI_HIDE)
                    ;Clearing the data in the user and password fields
                    GUICtrlSetState($iuser, "")
                    ;Clears password data, automatically set to 123456 *will need to be changed!*
                    ;GUICtrlSetState($ipassword, ""); will need to decomment when launched
                    ;End Reset Function
                    GUICtrlSetState($userLabel, $GUI_SHOW)
                    GUICtrlSetState($passwordLabel, $GUI_SHOW)
                    GUICtrlSetState($iuser, $GUI_SHOW)
                    GUICtrlSetState($ipassword, $GUI_SHOW)
                    GUICtrlSetState($scoringLabel, $GUI_SHOW)
                    GUICtrlSetState($scoreSelect, $GUI_SHOW)
                    GUICtrlSetState($iContinue, $GUI_SHOW)
                    GUICtrlSetState($browserLabel, $GUI_SHOW)
                    GUICtrlSetState($browserCombo, $GUI_SHOW)
                    GUICtrlSetState($courseLabel, $GUI_SHOW)
                    GUICtrlSetState($courseInput, $GUI_SHOW)
                Case $iContinue
                    ;Makes all of the information from GUI available
                    $type=GUICtrlRead($typeSelect)
                    $user=GUICtrlRead($iuser)
                    $pwd=GUICtrlRead($ipassword)
                    $program=GUICtrlRead($programSelect)
                    $passScore=GUICtrlRead($scoreSelect)
                    $activity=GUICtrlRead($activityCombo)
                    $exam="selectLater"
                    ExitLoop
                EndSwitch
    WEnd
    GUIDelete($hGUI)

        ;delete the file so it can be rewritten and set error log to 0
FileDelete ( "result.log.txt" )
DirRemove ("C:\TEMP",1)
DirCreate ("C:\TEMP")

_BlockInputEx(1, "{Pause}")

if $program="Adobe Certified Associate" then
    $topScreenCutOff=5
endif

;loads the test up from the correct platform and browser
if $platform="Portal Internet Explorer" then
    _callIE()
ElseIf $platform="Portal Chrome" then
    _PortalBrowser("chrome.exe")
ElseIf $platform="Portal Firefox" then
    _PortalBrowser("firefox.exe")
ElseIf $platform="Portal Safari" Then
    _PortalBrowser("safari.exe")
ElseIf $platform="Console" then
    _CallConsole()
EndIf

    LockdownNav()
EndFunc
Link to comment
Share on other sites

  • Moderators
  • Solution

Here is a simple example. A tip for future posts, posting runnable code will help us help you ;)

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

GUICreate("Test", 300, 300)
$button = GUICtrlCreateButton("Click Me", 10, 10, 80, 40)
    GUICtrlSetBkColor($button, $COLOR_RED)

GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $button
                GUICtrlSetBkColor($button, $COLOR_BLUE)
        EndSwitch
    WEnd

GUIDelete()

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

Glad you got it sorted out :) Sometimes, a short reproducer of the problem you're facing is better than trying to include your entire code.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I have another question, if I had 4 buttons and when I clicked on one the color would change, but if I clicked on another; the first button that I clicked on would revert back to default.  How would I do something like that.

Link to comment
Share on other sites

  • Moderators

Something like this:

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

GUICreate("Test", 300, 300)
$button = GUICtrlCreateButton("Change me", 10, 10, 80, 40)
$red = GUICtrlCreateButton("Change to Red", 100, 10, 80, 40)
$blue = GUICtrlCreateButton("Change to Blue", 190, 10, 80, 40)

GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $red
                GUICtrlSetBkColor($button, $COLOR_RED)
            Case $blue
                GUICtrlSetBkColor($button, $COLOR_BLUE)
        EndSwitch
    WEnd

GUIDelete()

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

kkelley,

Colouring buttons is not a good idea. There is a bug deep in the AutoIt core code which leads to some serious consequences as you can see here:

#include <GUIConstantsEx.au3>
#include <Misc.au3>

$hDLL = DllOpen("user32.dll")

GUICreate("Test", 500, 500)
$cInput = GUICtrlCreateInput("Input", 10, 10, 200, 20)
$cButton = GUICtrlCreateButton("Button", 10, 40, 80, 30)
$cLabel = GUICtrlCreateLabel("", 10, 90, 200, 20)
GUISetState(@SW_SHOW)

; Move the focus to the button and then back to the input
Sleep(1000)
GUICtrlSetState($cButton, $GUI_FOCUS)
Sleep(1000)
GUICtrlSetState($cInput, $GUI_FOCUS)
GUICtrlSetData($cLabel, "Press Enter and nothing happens")

; Wait for ENTER to be pressed and released
While 1
    If _IsPressed("0D", $hDLL) Then
        While 1
            If Not _IsPressed("0D", $hDLL) Then
                ExitLoop 2
            EndIf
            Sleep(10)
        WEnd
    EndIf
    Sleep(10)
WEnd

; Colour button
GUICtrlSetData($cLabel, "Colouring the button")
GUICtrlSetBkColor($cButton, 0x00FF00)

; Move the focus to the button and then back to the input again
Sleep(1000)
GUICtrlSetState($cButton, $GUI_FOCUS)
Sleep(1000)
GUICtrlSetState($cInput, $GUI_FOCUS)
GUICtrlSetData($cLabel, "Now press Enteragain and see the difference")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            DllClose($hDLL)
            Exit
        Case $cButton
            MsgBox(0, "", "button triggered")
    EndSwitch
WEnd
So unless you really need the coloured buttons - and are prepared to accept the consequences - I would strongly suggest leaving your buttons au naturel. I often use a coloured border by setting an over-sized label underneath the button which can be coloured at will without consequence - but do remember to disable it so that AutoIt does not get confused. ;)

M23

Edited by Melba23
Fixed tags

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Might be a workaround:

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

_GDIPlus_Startup()
$hDLL = DllOpen("user32.dll")

$hGUI = GUICreate("Test", 500, 500)
$cInput = GUICtrlCreateInput("Input", 10, 10, 200, 20)
$cButton = GUICtrlCreateButton("Button", 10, 40, 80, 30)
$cLabel = GUICtrlCreateLabel("", 10, 90, 220, 20)
GUISetState(@SW_SHOW)

; Move the focus to the button and then back to the input
Sleep(1000)
GUICtrlSetState($cButton, $GUI_FOCUS)
Sleep(1000)
GUICtrlSetState($cInput, $GUI_FOCUS)
GUICtrlSetData($cLabel, "Press Enter and nothing happens")

; Wait for ENTER to be pressed and released
While 1
    If _IsPressed("0D", $hDLL) Then
        While 1
            If Not _IsPressed("0D", $hDLL) Then
                ExitLoop 2
            EndIf
            Sleep(10)
        WEnd
    EndIf
    Sleep(10)
WEnd

; Colour button
;~ GUICtrlSetData($cLabel, "Colouring the button")
;~ GUICtrlSetBkColor($cButton, 0x00FF00)
GUICtrlSetData($cButton, "")
GUICtrlSetStyle($cButton, BitOR($BS_BITMAP, $GUI_SS_DEFAULT_BUTTON))
Global $hHBitmap = _GDIPlus_BtnCtrlSetBgColorSolid($hGUI, $cButton, 0x00FF00, "Button")
; Move the focus to the button and then back to the input again
Sleep(1000)
GUICtrlSetState($cButton, $GUI_FOCUS)
Sleep(1000)
GUICtrlSetState($cInput, $GUI_FOCUS)
GUICtrlSetData($cLabel, "Now press Enter again and see the difference")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            DllClose($hDLL)
            _WinAPI_DeleteObject($hHBitmap)
            _GDIPlus_Shutdown()
            Exit
        Case $cButton
            MsgBox(0, "", "button triggered")
    EndSwitch
    If _IsPressed("0D", $hDLL) Then
        While 1
            If Not _IsPressed("0D", $hDLL) Then
                _WinAPI_DeleteObject(_SendMessage(GUICtrlGetHandle($cButton), $BM_SETIMAGE, 0, 0))
                GUICtrlSetStyle($cButton, 0)
                GUICtrlSetData($cButton, "Button")
                ExitLoop 1
            EndIf
            Sleep(10)
        WEnd
    EndIf
WEnd

Func _GDIPlus_BtnCtrlSetBgColorSolid($hGUI, $iCtrlID, $iColor, $sText = "", $sFont = "Arial", $fSize = 8.5, $iFontColor = 0xFF000000) ;coded by UEZ 2014
    Local $aSize = ControlGetPos($hGUI, "", $iCtrlID)
    If @error Then Return SetError(1, 0, 0)
    If $sText = "" Then $sText = ControlGetText($hGUI, "", $iCtrlID)
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($aSize[2], $aSize[3])
    Local Const $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    Local Const $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000 + $iColor)
    _GDIPlus_GraphicsFillRect($hGfx, 2, 2, $aSize[2] - 4, $aSize[3] - 4, $hBrush)
    _GDIPlus_GraphicsSetTextRenderingHint($hGfx, 5)
    _GDIPlus_GraphicsSetSmoothingMode($hGfx, 2)
    Local Const $hFormat = _GDIPlus_StringFormatCreate()
    Local Const $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    Local Const $hFont = _GDIPlus_FontCreate($hFamily, $fSize)
    Local Const $tLayout = _GDIPlus_RectFCreate(0, 0, $aSize[2], $aSize[3])
    Local Const $hBrush_Font = _GDIPlus_BrushCreateSolid($iFontColor)
    _GDIPlus_StringFormatSetAlign($hFormat, 1)
    _GDIPlus_StringFormatSetLineAlign($hFormat, 1)
    _GDIPlus_GraphicsDrawStringEx($hGfx, $sText, $hFont, $tLayout, $hFormat, $hBrush_Font)
    Local Const $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _GDIPlus_BrushDispose($hBrush_Font)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_GraphicsDispose($hGfx)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_BrushDispose($hBrush)
    _WinAPI_DeleteObject(_SendMessage(GUICtrlGetHandle($iCtrlID), $BM_SETIMAGE, 0, $hHBitmap))

    Return $hHBitmap
EndFunc

Edit: forgot to cleanup the bitmap resource.

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

  • Moderators

UEZ,

Clever. :thumbsup:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

kkelley,

As I explained earlier, in the past I have used underlying labels like this:

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$cLabel_1 = GUICtrlCreateLabel("", 8, 8, 84, 34)
GUICtrlSetBkColor($cLabel_1, 0x00FF00)
GUICtrlSetState($cLabel_1, $GUI_Disable) ; Do not ignore this line - comment it out and see what happens!! <<<<<<<<<<<<<<<<<
$cButton_1 = GUICtrlCreateButton("Button 1", 10, 10, 80, 30)

$cLabel_2 = GUICtrlCreateLabel("", 8, 58, 84, 34)
GUICtrlSetBkColor($cLabel_2, 0x00FF00)
GUICtrlSetState($cLabel_2, $GUI_Disable)
$cButton_2 = GUICtrlCreateButton("Button 1", 10, 60, 80, 30)

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton_1
            GUICtrlSetBkColor($cLabel_1, 0xFF0000)
            GUICtrlSetBkColor($cLabel_2, 0x00FF00)
        Case $cButton_2
            GUICtrlSetBkColor($cLabel_2, 0xFF0000)
            GUICtrlSetBkColor($cLabel_1, 0x00FF00)
    EndSwitch
WEnd
How about that? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 5 years later...
On 7/8/2014 at 5:55 PM, Melba23 said:

Colouring buttons is not a good idea. There is a bug deep in the AutoIt core code which leads to some serious consequences as you can see here:

Hi everybody :)
Even if this thread isn't recent, maybe what follows could be interesting to a new approach of the issue ?
Please have a look at this picture and the corresponding script :

321060999_BS_Ownerdrawissue.png.19b8760ea2fdc6f5a5155de31e9b201d.png

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WinAPISysWin.au3>

GUICreate("$BS_OWNERDRAW issue ?", 300, 250)

$cInput1 = GUICtrlCreateInput("Input1", 10, 10, 200, 20)
$cInput2 = GUICtrlCreateInput("Input2", 10, 40, 200, 20)

$cButton1 = GUICtrlCreateButton("Button1 $GUI_SS_DEFAULT_BUTTON", _
    10, 70, 200, 30, $GUI_SS_DEFAULT_BUTTON)
$hButton1 = GUICtrlGetHandle($cButton1)
$iStyle_Button1 = _WinAPI_GetWindowLong($hButton1, $GWL_STYLE)

$cButton2 = GUICtrlCreateButton("Button2 $DEFPUSHBUTTON (0x0001)", _
    10, 110, 200, 30, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON))
$hButton2 = GUICtrlGetHandle($cButton2)
$iStyle_Button2 = _WinAPI_GetWindowLong($hButton2, $GWL_STYLE)

$cButton3 = GUICtrlCreateButton("Button3 $BS_OWNERDRAW (0x000B)", _
    10, 150, 200, 30, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_OWNERDRAW))
$hButton3 = GUICtrlGetHandle($cButton3)
$iStyle_Button3 = _WinAPI_GetWindowLong($hButton3, $GWL_STYLE)

GUISetState(@SW_SHOW)

MsgBox($MB_TOPMOST,"Styles of 3 Buttons ", _
    $iStyle_Button1 & @CRLF & @CRLF & @CRLF & _
    $iStyle_Button2 & @CRLF & @CRLF & @CRLF & _
    $iStyle_Button3)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton1
            MsgBox(0, "", "button1 triggered")
        Case $cButton2
            MsgBox(0, "", "button2 triggered")
        Case $cButton3
            MsgBox(0, "", "button3 triggered")
    EndSwitch
WEnd

; https://www.autoitscript.com/forum/topic/162593-changing-button-colors-on-click/

$BS_OWNERDRAW is a style which is added when you change the background color of a button control and it's value is 0x000B (11 decimal)
If you BitOr() its value to the default button style, you should get a total of 1342242816 + 11 = 1342242827
Instead of that, we see that AutoIt indicates 1342242817, exactly as if we had added... a $DEFPUSHBUTTON Style (0x0001)

I thought of that when I tried Melba23's example, after creating several Input controls and changed the background color of the button, because after that, each Enter key in any Input field triggered the button, exactly as when the button gets the $DEFPUSHBUTTON style.

My apologies if this was already discussed or solved in another thread.
Thanks for reading.

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

×
×
  • Create New...