Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mrsnakers
Posts: 29
Joined: 23 Jan 2017, 16:08

Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

24 Mar 2017, 11:22

I was thinking either simply changing the color from when pressed the text is green = active and when toggled off = red. Perhaps would be cool to have an actual on off switch image but at this point i Just need a visual indication of whether it's enabled or disabled. I'm also not sure how to have a GUI button toggle to have a function when pressed then toggle to do nothing when pressed again? Thanks!
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

24 Mar 2017, 15:20

You can use a picture control as button ( use a g_label ) , if you just want text to indicate on/off there is a way using two buttons in the same location see the example.

Code: Select all

gui ,add,button, x10 y10 vbutton_on w50 h50 gchange,on
gui ,add,button, x10 y10 vbutton_off w50 h50 gchange,off
GuiControl, hide,button_on
Gui, show
return 

alert:
soundbeep 600
return


change:
t_on:=!t_on

if t_on
  {
  GuiControl, hide,button_off
  GuiControl, show,button_on
  SetTimer, alert,200
  }
else
  {
  GuiControl, hide,button_on
  GuiControl, show,button_off
  SetTimer, alert,off
  }
return

esc::exitapp
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

24 Mar 2017, 15:41

Progress control have color option, which one can bet set using GuiControl command. Also GuiControl command can disable control.
(see: https://autohotkey.com/docs/commands/GuiControl.htm)

Here's a draft to help you start:

Code: Select all

Gui, Add, Progress, x3 y58 w294 h5 vMyProgress cGreen, 100
Gui, Add, Button, x3 y3 w294 h54 vMyButton gTEST, blabla ; TEST subroutine will be triggered as soon as button will be clicked. see: https://autohotkey.com/docs/commands/Gui.htm#label
Gui, Show, w400 h400
return

TEST: ; TEST subroutine
MsgBox % "You clicked " . A_GuiControl
GuiControl, +cred, MyProgress ; set MyProgress progress GUI control ''c'' (color) option to be red using GuiControl command
GuiControl, Disable, MyButton ; disabled MyButton button
return
my scripts
mrsnakers
Posts: 29
Joined: 23 Jan 2017, 16:08

Re: Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

24 Mar 2017, 18:57

Thanks folks! I'll try to tinker with both examples. Appreciate it.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

25 Mar 2017, 18:11

Code: Select all

BS_PUSHLIKE := 0x1000
Gui Add, CheckBox, %BS_PUSHLIKE% r2 vB1 gBtn, This button toggles when you click it.
Gui Add, CheckBox, %BS_PUSHLIKE% r2 vB2 gBtn -Theme, So does this button.
Gui Add, Button, r1.2 vB3 gBtn, This button doesn't.
Gui Show
GuiClose() {
    ExitApp
}
Btn() {
    GuiControlGet v,, %A_GuiControl%
    MsgBox % A_GuiControl "=" v
}
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

27 Mar 2017, 11:52

mrsnakers wrote:Perhaps would be cool to have an actual on off switch image...
Below is a link to a nice example and tutorial complete with images to download for the buttons.
https://jszapp.com/make-beautiful-graph ... ines-less/

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

27 Mar 2017, 14:59

example color Green/Red ... ON/OFF

EDIT :
see better short version from user TheDewd below
---------------------------------------------------

Code: Select all

;- Color button changes to RED/GREEN   ON/OFF
#warn
Modified=20130110
Filename1=Color_Button_Test
setworkingdir,%a_scriptdir%
Gui,2:Font,Cdefault,Fixedsys
Gui,2:Color,Black
z:=""
Gui,2:Add,Progress,  x10  y20  w100  h23  Disabled BackgroundRed   vC2
Gui,2:Add,Text,      xp   yp   wp    hp   cYellow  BackgroundTrans Center 0x200 vB2 gStart,OFF
Gui,2:Add,Progress,  xp   yp   wp    hp   Disabled BackgroundGreen vC1
Gui,2:Add,Text,      xp   yp   wp    hp   cYellow  BackgroundTrans Center 0x200 vB1 gStart,ON

Gui,2: Show, x10 y1 w200 h60 ,%filename1%
return
2guiclose:
exitapp

start:
gui,2:submit,nohide
z:=!z
if z
  {
  GuiControl,2: hide,B1
  GuiControl,2: hide,C1
  GuiControl,2: show,B2
  GuiControl,2: show,C2
  msgbox, 262208,ColorButtonTest ,You clicked ON_Green-Button,1
  }
else
  {
  GuiControl,2: hide,B2
  GuiControl,2: hide,C2
  GuiControl,2: show,B1
  GuiControl,2: show,C1
  msgbox, 262208,ColorButtonTest ,You clicked OFF_Red-Button,1
  }
return
;=================================
Last edited by garry on 27 Mar 2017, 16:14, edited 1 time in total.
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

27 Mar 2017, 15:07

Building on Lexikos' example, you can also use the buttons like the radio control.

Code: Select all

#SingleInstance, Force

BS_PUSHLIKE := 0x1000
Gui, Margin, 10, 10
Gui, Add, Radio, %BS_PUSHLIKE% w120 h23 Group, Radio Button 1
Gui, Add, Radio, %BS_PUSHLIKE% w120 h23, Radio Button 2
Gui, Add, Radio, %BS_PUSHLIKE% w120 h23, Radio Button 3
Gui, Add, Radio, %BS_PUSHLIKE% w120 h23, Radio Button 4
Gui, Add, Radio, %BS_PUSHLIKE% w120 h23 -Theme Group, Radio Button 5
Gui, Add, Radio, %BS_PUSHLIKE% w120 h23 -Theme, Radio Button 6
Gui, Add, Radio, %BS_PUSHLIKE% w120 h23 -Theme, Radio Button 7
Gui, Add, Radio, %BS_PUSHLIKE% w120 h23 -Theme, Radio Button 8
Gui, Show, AutoSize, Example
return

GuiClose() {
    ExitApp
}
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

27 Mar 2017, 15:30

garry wrote:example color Green/Red ... ON/OFF

Code: Select all

;- Color button changes to RED/GREEN   ON/OFF
#warn
Modified=20130110
Filename1=Color_Button_Test
setworkingdir,%a_scriptdir%
Gui,2:Font,Cdefault,Fixedsys
Gui,2:Color,Black
z:=""
Gui,2:Add,Progress,  x10  y20  w100  h23  Disabled BackgroundRed   vC2
Gui,2:Add,Text,      xp   yp   wp    hp   cYellow  BackgroundTrans Center 0x200 vB2 gStart,OFF
Gui,2:Add,Progress,  xp   yp   wp    hp   Disabled BackgroundGreen vC1
Gui,2:Add,Text,      xp   yp   wp    hp   cYellow  BackgroundTrans Center 0x200 vB1 gStart,ON

Gui,2: Show, x10 y1 w200 h60 ,%filename1%
return
2guiclose:
exitapp

start:
gui,2:submit,nohide
z:=!z
if z
  {
  GuiControl,2: hide,B1
  GuiControl,2: hide,C1
  GuiControl,2: show,B2
  GuiControl,2: show,C2
  msgbox, 262208,ColorButtonTest ,You clicked ON_Green-Button,1
  }
else
  {
  GuiControl,2: hide,B2
  GuiControl,2: hide,C2
  GuiControl,2: show,B1
  GuiControl,2: show,C1
  msgbox, 262208,ColorButtonTest ,You clicked OFF_Red-Button,1
  }
return
;=================================
Garry -- Your example can be simplified:
Slight modifications added (personal preference)

Code: Select all

#SingleInstance, Force

z := ""

Gui, Margin, 10, 10
Gui, Add, Progress, w160 h28 Disabled BackgroundFF0000 vProgress
Gui, Add, Text, xp yp wp hp cFFFFFF BackgroundTrans 0x201 vText gToggle, OFF
Gui, Show, AutoSize, Example
return

Toggle:
    z := !z
    Gui, Submit, NoHide
    GuiControl, % "+Background" (z ? "008000" : "FF0000"), Progress
    GuiControl,, Text, % (z ? "ON" : "OFF")
return

GuiClose:
    ExitApp
return
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

27 Mar 2017, 16:12

@TheDewd , thank you , this is really nice and very short
william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

Re: Basic GUI button that changes color / toggles when pressed? Like an "on / off" ?

24 Sep 2023, 11:50

Very nice. You can actually create an ON/OFF switch with this technique :D

Code: Select all

#SingleInstance Force

BS_PUSHLIKE := 0x1000
Gui, Margin, 20, 20
Gui, Font, s11
Gui, Add, Text, xm, Toggle: 
Gui, Add, Radio, %BS_PUSHLIKE% x+10 w40 h23 -Theme Checked, ON
Gui, Add, Radio, %BS_PUSHLIKE% x+0 w40 h23 -Theme, OFF

Gui, Add, Text, xm, POWER:
Gui, Font, Bold
Gui, Add, Radio, %BS_PUSHLIKE% x+10 w25 h23 -Theme Checked, |
Gui, Add, Radio, %BS_PUSHLIKE% y+0 w25 h23 -Theme, ◯
Gui, Font, Normal

Gui, Show
return

GuiClose:
ExitApp
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], prototype_zero and 230 guests