Sign in to follow this  
Followers 0
Cybernetic

Changing background colours of buttons

8 posts in this topic

Hello guys.

quick question and probably a basic one, how do i make it so that when i click a button in a GUI interface, it changes the colour of that button to say red for example, but then also make it, that if the button is already red, it then changes to green?

Also is it possible to do something like, right click a button to change the background colour to red, and left clicking would change it to green?

thank you in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

For chaning collors you could use GuiCtrlSetBkColor. If you want to make ON/Off button which would switch to Green/Red color, then just make something like this:

If $ON = 1 Then
    GUICtrlSetBkColor($Buttom1, 0x00ff00) ;Buttom1 green
Else
    GuiCtrlSetBkcolor($Buttom1, 0xff0000) ;Buttom1 ReD
EndIf

You would just need to think how do you want that this $ON change to lets say 0/1.

With second part you could use functions like this:

_IsPressed(1) ; 01 - left mouse
_IsPressed(2) ; 02 - right mouse
_IsPressed(4) ; 03 - middle mouse
Edited by Makaule

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

For chaning collors you could use GuiCtrlSetBkColor. If you want to make ON/Off button which would switch to Green/Red color, then just make something like this:

If $ON = 1 Then
    GUICtrlSetBkColor($Buttom1, 0x00ff00) ;Buttom1 green
Else
    GuiCtrlSetBkcolor($Buttom1, 0xff0000) ;Buttom1 ReD
EndIf

You would just need to think how do you want that this $ON change to lets say 0/1.

With second part you could use functions like this:

_IsPressed(1) ; 01 - left mouse
_IsPressed(2) ; 02 - right mouse
_IsPressed(4) ; 03 - middle mouse

i see what you are saying, however i cant seem to get it working properly?

and with the $ON, i need to declare it or no?

edit: NVM just got it working, thank you :(

Another thing, say i have multiple buttons i want to set this same condition for, is there a way to stack all these buttons in the same "IF" function as oppose to creating a seperate function for every single button?

Edited by Cybernetic

Share this post


Link to post
Share on other sites

like for example, say i have 4 buttons

$A1 = GUICtrlCreateButton("A1", 50, 150, 50, 50)

$A2 = GUICtrlCreateButton("A2", 100, 150, 50, 50)

$A3 = GUICtrlCreateButton("A3", 150, 150, 50, 50)

$A4 = GUICtrlCreateButton("A4", 200, 150, 50, 50)

GUICtrlSetOnEvent($A1, "A1")

GUICtrlSetOnEvent($A2, "A2")

GUICtrlSetOnEvent($A3, "A3")

GUICtrlSetOnEvent($A4, "A4")

there is the script for it, as you can see the $A1 to $A4 would be buttons and i have events there and so on, is there a way to sort of combine them all into 1 sort of function or way, cause im planning on having a WHOLE lot more buttons and itll be hard to sort of stack them all up 1 after the other.

Share this post


Link to post
Share on other sites

Guys,

Take care when colouring your buttons. Once a button is coloured it can act as though it has the $BS_DEFPUSHBUTTON style and so fire when "Enter" is pressed even though it does not have focus.

Look at this example - use the "Tab" key to put the focus on the checkbox and press "Enter":

#include <GUIConstantsEx.au3>

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

$hButton_1 = GUICtrlCreateButton("Normal", 10, 10, 80, 30)

$hButton_2 = GUICtrlCreateButton("Coloured", 10, 100, 80, 30)
GUICtrlSetBkColor(-1, 0xFFC4C4)

$hCheckBox = GUICtrlCreateCheckBox(" Test", 10, 200, 100, 20)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            MsgBox(0, "Normal", "Normal button pressed!")
        Case $hButton_2
            MsgBox(0, "Coloured", "Coloured button pressed!" & @CRLF & "Or was Enter pressed?")
    EndSwitch

WEnd

See what I mean? :) This is a known bug from a long time ago (reported at #376) and as you can see from the conversation there it will not be fixed any time soon, if ever.

By all means go ahead and colour your buttons, but just be aware of the problem. :(

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

thank you for that information :(

however that still doesnt solve my problem for when i create over 200 buttons and i have to have a HUGE ass script for it cause i dont know how to sort of stack or compile them

or stick them into some sort of an array :)

can anyone help me with this please?

Share this post


Link to post
Share on other sites

@Cybernetic: Please don't bump your post within 24h. Use the edit button. Sounds like you want something like this:

#include <Array.au3>

Global $AmountOfButtons = 4 ;the amount of buttons is defined here.
Global $buttons[$AmountOfButtons], $states[$AmountOfButtons]
Opt("GUIOnEventMode",1)
GUICreate("",120,20+($AmountOfButtons*30))
GUISetOnEvent(-3, "_Exit")

For $i = 0 To $AmountOfButtons -1 ;buttons are created here
    $buttons[$i] = GUICtrlCreateButton("A" & $i+1,10,10+($i*30),100)
    GUICtrlSetOnEvent(-1,"_ButtonPressed")
    GUICtrlSetBkColor(-1, 0x00ff00)
Next

GUISetState()

While 1
    Sleep(1000)
WEnd

Func _Exit()
    Exit
EndFunc

Func _ButtonPressed()
    Local $button = @GUI_CtrlId
    Local $buttonIndex = _ArraySearch($buttons,$button)
    
    If $states[$ButtonIndex] Then   ;change the color
        GUICtrlSetBkColor($button, 0x00ff00)
    Else
        GuiCtrlSetBkcolor($button, 0xff0000)
    EndIf
    $states[$ButtonIndex] = Not $states[$ButtonIndex]
    
    Switch $buttonIndex ;run code depending on button
        Case 0
            ConsoleWrite("button1 pressed" & @CRLF)
        Case 1
            ConsoleWrite("button2 pressed" & @CRLF)
        Case 2
            ConsoleWrite("button3 pressed" & @CRLF)
        Case 3
            ConsoleWrite("button4 pressed" & @CRLF)
    EndSwitch
EndFunc

The right click approach is possible, but probably more trouble than it's worth.

Share this post


Link to post
Share on other sites

Tvern,

sorry didnt mean to bump my post so frequently, i tried to edit it earlier but for some reason didnt allow me too.

thanks for the script you just wrote for me even though i barely understand it, however i will just need to merge it into mine and hopefully ill be ok.

thank you :(

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