Jump to content
Sign in to follow this  
kkelley

Changing button colors on click

Recommended Posts

kkelley

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

Share this post


Link to post
Share on other sites
JLogan3o13

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()

√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
kkelley

That works great! thanks JLogan3o13, I would include the rest of the code but it is quite a large number of files.

Share this post


Link to post
Share on other sites
JLogan3o13

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.


√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
kkelley

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.

Share this post


Link to post
Share on other sites
JLogan3o13

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()

√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
Melba23

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

 

Share this post


Link to post
Share on other sites
UEZ

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

Share this post


Link to post
Share on other sites
Melba23

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

 

Share this post


Link to post
Share on other sites
kkelley

Melba, what would be your recommendation to do a selected button? 

Share this post


Link to post
Share on other sites
Melba23

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

 

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  

  • Similar Content

    • nooneclose
      By nooneclose
      Hello again, I am in phase two of my help bot. I cannot figure out how to properly code Autoit to click on a webpage button using the I.E UDF. I want to click on the button that I have attached as an image. This button does not have a name but it has an ID. Here is the HTML code for this button that I found using chromes developer inspector.
      HTML Code: 
      <img role="button" tabindex="-1" id="toolactions_INSERT-tbb_image" src="nav_icon_insertkey.gif" alt="New Work Order CTRL+ALT+I" draggable="false"> Here is the code that I have so far:
      ;******************************************************************************* ; Opens IE and fill out a New Workorder ;******************************************************************************* ; Start IE at the ** Tracking Application in ****** $oIE = _IECreate("https:I_am_Bound_by_my_company_to_not_reveal_this_webpage") ; Wait for a browser page load to complete _IELoadWait($oIE) Sleep(6000) ; Store the field names where the important data will be sent Local $nWorkOrderB = _IEGetObjById($oIE, "toolactions_INSERT-tbb_image") _IEAction($nWorkOrderB, "click")  I want my program to click on the button that looks like a piece of paper with a blue # 
      (For those wondering, I am allowed to send images just not the URL.) 
      As always any help would be appreciated. 

    • Pike
      By Pike
      Hello Everyone,
      New Guy here. This is a two-part question, hopefully easy enough though. I know it's ugly coding, this is because I have only a couple hours worth of coding experience and could really use some help.
      1) When I single click the button I want one action (like playing a sound clip) and when I double click the button I want a different action (such as opening dialogue box to ask for soundclip directory address). At the bottom of the script I made a comment which one I would like single click versus double click.
      2) I can not get the FileOpenDialogue to save the file address to the config.ini file at the 'One' position on the .ini.
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <AutoItConstants.au3> #include <Sound.au3> Opt('MustDeclareVars', 1) Opt("GUIOnEventMode", 1) Global $OneID _Main() Func _Main() GUICreate("Form1", 156, 86, 281, 276) $OneID = GUICtrlCreateButton("Button", 15, 17, 122, 37) GUICtrlSetOnEvent($OneID, "OnOne") GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit") GUISetState() While 1 While 1 Sleep(1000) WEnd WEnd EndFunc ;---------------Functions------------- While 1 Sleep(100) WEnd Func OnOne() ; ******************Single Click for this action**************** $OneID = IniRead("config.ini", "Config", "One", $OneID) If $OneID = True Then SoundPlay($OneID, @HotKeyPressed) Else ; *******Double Click for this action*****And Help Saving File Address to config.ini 'One'*********** $OneID = FileOpenDialog("Select file", @WorkingDir, "All (*.*)") IniWrite("config.ini", "Config", "One", $OneID) $OneID = InputBox("Enter Information", "Short Name for File?", "") IniWrite("config.ini", "Config", "NameOne", $OneID) EndIf EndFunc Any help would be greatly appreciated, and if you need anymore clarity as to what exactly I'm trying to do, please do not hesitate to ask. Thanks so much!
      Pike
    • dadalt
      By dadalt
      Hi!
      I have a button where I need to close it!
      I was doing through  clicking same position in screen but there are some id's that have different sizes.
      What are the possible ways to click this closable button?
      Is there a way to close it through id? Is there a way to get it's position through it's ID?  
      Thanks in advance!


    • Robdog1955
      By Robdog1955
      I'm trying to click a button on a web page. I have added a couple of MsgBox lines to allow me to watch what happens on the page. As you can see the first half of my script enters data into text boxes on the page. I have no problem there. I just cannot click on the region buttons. The "set focus" line causes an outline to appear around the EU button and the "click button" line causes the "Pick a Region" text to disappear. Here is the code I have so far.
      #include <IE.au3> Local $oIE = _IECreate("http://questchecker.com/") Local $iQuestID = "123456" Local $sCharacterName = "CharacterName" Local $colForms = _IEFormGetCollection($oIE) $iCount = 0 For $oForm In $colForms $oFormElements = _IEFormElementGetCollection($oForm) For $oFormElement In $oFormElements $iCount = $iCount + 1 Local $sTagName = StringLower($oFormElement.tagName) Local $sElementType = $oFormElement.type Local $sElementName = $oFormElement.name Switch $iCount Case 6 _IEFormElementSetValue($oFormElement, "MyRealm", 0) ; realm Case 7 _IEFormElementSetValue($oFormElement, $sCharacterName, 0) Case 8 _IEFormElementSetValue($oFormElement, $iQuestID, 0) EndSwitch Next Next Local $oButtons = _IEGetObjByName($oIE, "questForm") For $oButton In $oButtons If _IEFormElementGetValue($oButton) = "US" Then MsgBox(0, "", "Click Okay to set focus") _IEAction($oButton, "focus") MsgBox(0, "", "Click Okay to click button") _IEAction($oButton, "click") ExitLoop EndIf Next MsgBox(0, "", "Click Okay to quit") _IEQuit($oIE) Exit  
    • dadalt95
      By dadalt95
      Perform a simple google search!
      The script below works fine until fill the google form!
      What I can't find is how to submit the form, tried a couple of ways and none of them worked.

       
      #include <IE.au3> $oIE = _IECreate ("www.google.com") $o_form = _IEFormGetObjByName ($oIE, "f") $o_login = _IEFormElementGetObjByName ($o_form, "q") $username = "80251369" _IEFormElementSetValue ($o_login, $username) $o_numer = _IEGetObjByName($o_form, "btnK") _IEAction ($o_numer, "click")  
      The code runs without any problem.
      I don't know how to proceed!
      Thanks in advance!
×