Sign in to follow this  
Followers 0
kkelley

Changing button colors on click

12 posts in this topic

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



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!

Share this post


Link to post
Share on other sites

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

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!

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

I'd never encountered that before, thanks Melba. Learn something new every day :)


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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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

UEZ,

Clever. :thumbsup:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

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

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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  
Followers 0

  • Similar Content

    • islandspapand
      By islandspapand
      Hi all
      i am currently trying to click on an element in a HTML Table, but just can get it to work.
      i am able to click the top of the table so it changes to sort  but just can't click on the element in the table.
      an i need to click on element to continue in the site.
      i have attached the code so far and pictures of the table  element want to click plus the source of the table.
      i am able to get data in the table with $oTable = _IETableGetCollection($oIE, 2) but not able to click on them.
       
      Help is very much appreciated
       
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <IE.au3> #include "DOM.au3" #include <Array.au3> #include <MsgBoxConstants.au3> Global $oIE = _IECreate("*") _IELoadWait($oIE) Sleep(2000) _PageLogin($oIE) _PageLoadWait() _PageNewReq($oIE) _PageLoadWait() _InputModelInf($oIE) _PageLoadWait() Sleep(1000) $aTableLink = BGe_IEGetDOMObjByXPathWithAttributes($oIE, "//table/tbody/tr/td[.='Name Of user']", 2000) ;~ $aTableLink = BGe_IEGetDOMObjByXPathWithAttributes($oIE, "//table/tbody/tr", 2000) ;~ _ArrayDisplay($aTableLink,"$aTableLink") If IsArray($aTableLink) Then ConsoleWrite("Able to BGe_IEGetDOMObjByXPathWithAttributes($oIE, //table/tbody/tr/td[.='Name Of user'])" & @CRLF) For $i = 0 To UBound($aTableLink)-1 ConsoleWrite(" OuterHTML : " & $aTableLink[$i].outerHTML & @CRLF) ConsoleWrite(" Parentnode : " & $aTableLink[$i].parentnode & @CRLF) ConsoleWrite(" Parentnode.click : " & $aTableLink[$i].parentnode.fireEvent("onclick","click") & @CRLF) $objClick = $aTableLink[$i].parentnode ;~ _IEAction($aTableLink[$i] , "focus") _IEAction($objClick , "focus") ;~ If _IEAction($aTableLink[$i], "click") Then If _IEAction($objClick, "click") Then ConsoleWrite("Able to _IEAction($aForumLink[0], 'click')" & @CRLF) _IELoadWait($oIE) Else ConsoleWrite("UNable to _IEAction($aForumLink[0], 'click')" & @CRLF) Exit 3 EndIf Next Else ConsoleWrite("Unable to BGe_IEGetDOMObjByXPathWithAttributes($oIE, //table/tbody/tr/td[.='Name Of user'])" & @CRLF) Exit 2 EndIf _PageLoadWait() Func _InputModelInf($oTmpIE) ; Add Var for Model & Serial in Func $oModelInput = _IEGetObjById($oTmpIE,"model") _IEAction($oModelInput,"focus") _IEDocInsertText($oModelInput, "*") $oSerialInput = _IEGetObjById($oTmpIE,"serial") _IEAction($oModelInput,"focus") _IEDocInsertText($oSerialInput, "*") $links = $oTmpIE.document.getElementsByClassName("btn btn-primary ng-scope") For $link In $links If $link.innertext = "Søg" Or $link.innertext = "Search" Then $link.click() ExitLoop EndIf Next Return True EndFunc Func _PageNewReq($oTmpIE) $links = $oTmpIE.document.getElementsByClassName("ng-scope k-link") For $link In $links If $link.innertext = "Send ny fejlmelding" Or $link.innertext = "Submit a New Service Request" Then $link.click() ExitLoop EndIf Next Return True EndFunc Func _PageLogin($oTmpIE) $oUserInput = _IEGetObjById($oTmpIE,"loginid") _IEDocInsertText($oUserInput, "*") $oPasswordInput = _IEGetObjById($oTmpIE,"password") _IEDocInsertText($oPasswordInput, "*") $links = $oTmpIE.document.getElementsByClassName("btn btn-primary login ng-scope") For $link In $links If $link.innertext = "Sign in" Then $link.click() ExitLoop EndIf Next Return True EndFunc Func _PageLoadWait() Local $PageLoadWait = False ;~ nav navbar-nav navbar-right ng-hide ;~ nav navbar-nav navbar-right $tags = $oIE.document.GetElementsByTagName("ul") For $tag in $tags $class_value = $tag.GetAttribute("class") If $class_value = "nav navbar-nav navbar-right" Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : Webpage loading :) ' & @CRLF) ;### Debug Console $PageLoadWait = True ExitLoop EndIf Next Do sleep(250) For $tag in $tags $class_value = $tag.GetAttribute("class") If $class_value = "nav navbar-nav navbar-right ng-hide" Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : Webpage load finished :)'& @CRLF) ;### Debug Console $PageLoadWait = False ExitLoop EndIf Next Until $PageLoadWait = False EndFunc  
      Thanks in advance
       
       


    • OtenMoten
      By OtenMoten
      Hello dear community,
       
      definitely I need external help now. I've did all 'ControlClick() - Tutorials' and read a lot of topics about interaction betweend AutoIT and external programs.
      The most of them are about "Clicking a button ...". In my case it's different because I need to click a specific element within the program.
       
      The 'program' what I am talking about is called Mendeley. It's library software for documents administration.
       
      In the attachements in picture '1.jpg' you can see the red marked Help element. 
      In the picture '2.jpg' you see the context menu of the Help element. This is what I'm trying to do with the ControlClick() function.
       
      Following I represent my code which not worked for me:
      #include <MsgBoxConstants.au3> Opt("WinTitleMatchMode", 2) $hWnd = WinGetHandle("Mendeley Desktop", "") MsgBox($MB_SYSTEMMODAL, "handle check", $hWnd) ControlClick($hWnd, "Mendeley Desktop", "Help") The MsgBox tells me the number of the handle of the Mendeley window. It's not 0 so for me it means the window is recognized correctly.
       
      When I'm execute the script the Help element is not clicked.
      Any suggestions for me?
       
      Kind regards
      OtenMoten


    • boy233
      By boy233
      I need to click on the text "Batch submission" but I can not!
      <div class="batchmenu2" onclick="Go('/lot/')" style="background-color: rgb(255, 255, 255);"> <span class="iconep">l</span> <div class="menu"> <b>Batch submission</b> <br> Bulk messages via file </div> </div> How could I do it?
      How can I click the specific OnClick?
       
    • SkysLastChance
      By SkysLastChance
      <input name="VsMasterPage$MainContent$LoginUserControl$LoginForm$FormsAuthLoginContainer$LoginButton$InternalButton" class="Button" id="VsMasterPage_MainContent_LoginUserControl_LoginForm_FormsAuthLoginContainer_LoginButton_InternalButton" onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("VsMasterPage$MainContent$LoginUserControl$LoginForm$FormsAuthLoginContainer$LoginButton$InternalButton", "", true, "Login", "", false, false))' type="submit" value="Sign In"> I am trying to click the sign in button on this page. 
      I have tried by class, text, etc 
      Here was my last attempt
      $oButtons = _IETagNameAllGetCollection($o_IE, "button") For $oButton in $oButtons If String($oButton.value) = "Sign In" Then Sleep (1000) _IEAction($oButton, "click") ExitLoop EndIf Next This did not work either.
    • 5ervant
      By 5ervant
      Go create a quick account in Zapier and go to https://zapier.com/app/dashboard and make some Zaps!
      Just need to click the specific instance of an .open-menu button and then click its a.run (anchor) element.
      Doing this IE.au3 script just causing to click its container element:
      $target = 1; Target the first instance #include <IE.au3> $oIE = _IEAttach("Dashboard - Zapier") $count = 0; $tags = _IETagNameGetCollection($oIE, "div") For $tag in $tags $class_value = $tag.GetAttribute("class") If $class_value = "open-menu" Then $count += 1 if $count = $target Then MsgBox(0, "Instance: ", $count) ; $tag.fireEvent("onmousedown") ; _IEAction($tag, "click") ; $tag.fireEvent("onmouseup") ; Or this but not working ; $tag.Click EndIf EndIf Next I also tried to do it using FF.au3
      $target = 1; Target the first instance #Include <Array.au3> #Include <FF.au3> $count = 0; If _FFConnect(Default, Default, 3000) Then $aArray = _FFXPath( "//div[@class='open-menu']", "", 7 ) ; _ArrayDisplay($aArray) For $tag in $aArray $count += 1 if $count = $target Then MsgBox(0, "Instance: ", $count) _FFClick($tag) EndIf Next EndIf Error: _FFClick ==> No match: $sElement: [number]
      Could some help me how to click such buttons on such kind of a dynamic page?