Jump to content

GUI rocker switch <ON>|<OFF> UDF?


MKANET
 Share

Recommended Posts

I just starting to get my feet wet browsing through various AutoIt UDFs. One of the first things I tried looking for after learning about basic GUI buttons and checkboxes was rocker switches. I couldn't find anything on this. The closest I found was a way to turn a GUI checkbox into a button. Using checkboxes and buttons can only go so far. Is it because the OS itself doesnt have native rocker switches? Anyway, hopefully someone has made some kind of basic functionality like this alread.

Below are examples of rocker ON/OFF switches when skinned. I would be happy with just a very basic rocker switch toggle which would match any basic non-skinned GUI application.

Posted Image

Posted Image

Link to comment
Share on other sites

I don't know how to make the slide, but if you click on them, they move to the opposite side. Perhaps this will get you started in the right direction.

#include <GUIConstants.au3>
#include <GuiButton.au3>
#include <GUIConstantsEx.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 172, 177)
$Group1 = GUICtrlCreateGroup("", 8, 7, 130, 38)
$Button1 = GUICtrlCreateButton("On", 16, 16, 51, 25, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("", 8, 55, 130, 38)
$Button2 = GUICtrlCreateButton("Off", 81, 64, 51, 25, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("", 8, 103, 130, 38)
$Button3 = GUICtrlCreateButton("On", 16, 112, 51, 25, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Group1()
        Case $Button2
            _Group2()
        Case $Button3
            _Group3()
    EndSwitch
WEnd

Func _Group1()
    $readButton = _GUICtrlButton_GetText($Button1)
    If $readButton = "On" Then
        GUICtrlSetPos($Button1, 81, 16, 51, 25)
        _GUICtrlButton_SetText($Button1, "Off")
    Else
        GUICtrlSetPos($Button1, 16, 16, 51, 25)
        _GUICtrlButton_SetText($Button1, "On")
    EndIf
EndFunc

Func _Group2()
    $readButton = _GUICtrlButton_GetText($Button2)
    If $readButton = "On" Then
        GUICtrlSetPos($Button2, 81, 64, 51, 25)
        _GUICtrlButton_SetText($Button2, "Off")
    Else
        GUICtrlSetPos($Button2, 16, 64, 51, 25)
        _GUICtrlButton_SetText($Button2, "On")
    EndIf
EndFunc

Func _Group3()
    $readButton = _GUICtrlButton_GetText($Button3)
    If $readButton = "On" Then
        GUICtrlSetPos($Button3, 81, 112, 51, 25)
        _GUICtrlButton_SetText($Button3, "Off")
    Else
        GUICtrlSetPos($Button3, 16, 112, 51, 25)
        _GUICtrlButton_SetText($Button3, "On")
    EndIf
EndFunc
Link to comment
Share on other sites

Thanks so much for doing this! Actually, I don't really care about fancy graphics or sliding animation. It's somewhat close to what I was looking for.

Is there any chance you could have the side that's completely blank, to have a pressed down button labeled ON in bold? The other side should have a button in the up position with the label OFF (without bold)? Unfortunately, I don't have the experience to do this on my own quite yet.

Thanks again for giving it a shot.

Edited by MKANET
Link to comment
Share on other sites

Ahh, upon closer look, the rocker switches do not have "on/off" on the buttons. Updated the code:

#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 172, 177)
$Group1 = GUICtrlCreateGroup("", 8, 7, 130, 38)
$Button1 = GUICtrlCreateButton("", 16, 16, 51, 25, 0)
$Label1 = GUICtrlCreateLabel("On", 100, 22, 18, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("", 8, 55, 130, 38)
$Button2 = GUICtrlCreateButton("", 81, 64, 51, 25, 0)
$Label2 = GUICtrlCreateLabel("Off", 34, 70, 18, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("", 8, 103, 130, 38)
$Button3 = GUICtrlCreateButton("", 16, 112, 51, 25, 0)
$Label3 = GUICtrlCreateLabel("On", 100, 118, 18, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Group1()
        Case $Button2
            _Group2()
        Case $Button3
            _Group3()
    EndSwitch
WEnd

Func _Group1()
    $readLabel = GUICtrlRead ($Label1)
    If $readLabel = "On" Then
        GUICtrlSetPos($Button1, 81, 16, 51, 25)
        GUICtrlSetPos($Label1, 34, 22, 18, 17)
        GUICtrlSetData($Label1, "Off")
    Else
        GUICtrlSetPos($Button1, 16, 16, 51, 25)
        GUICtrlSetPos($Label1, 100, 22, 18, 17)
        GUICtrlSetData($Label1, "On")
    EndIf
EndFunc

Func _Group2()
    $readLabel = GUICtrlRead ($Label2)
    If $readLabel = "On" Then
        GUICtrlSetPos($Button2, 81, 64, 51, 25)
        GUICtrlSetPos($Label2, 34, 70, 18, 17)
        GUICtrlSetData($Label2, "Off")
    Else
        GUICtrlSetPos($Button2, 16, 64, 51, 25)
        GUICtrlSetPos($Label2, 100, 70, 18, 17)
        GUICtrlSetData($Label2, "On")
    EndIf
EndFunc

Func _Group3()
    $readLabel = GUICtrlRead ($Label3)
    If $readLabel = "On" Then
        GUICtrlSetPos($Button3, 81, 112, 51, 25)
        GUICtrlSetPos($Label3, 34, 118, 18, 17)
        GUICtrlSetData($Label3, "Off")
    Else
        GUICtrlSetPos($Button3, 16, 112, 51, 25)
        GUICtrlSetPos($Label3, 100, 118, 18, 17)
        GUICtrlSetData($Label3, "On")
    EndIf
EndFunc
Link to comment
Share on other sites

I really appreciate all your help. I ended up going through a crash coarse in Koda last night ;). I probably wasn't too clear on what I am looking for. There are actually two different things I was interested in.

1. Slider switch (unfortunately, not too effective without proper graphics and/or possible animation)

2. Rocker switch

I was able to make a rocker switch from the limited button options available in Koda. Unfortunately, I wouldn't really consider these attempts at slider/rocker switches quite good enough for me to use. Luckily, I don't have a serious need to use these kinds of switches. Maybe some day someone much more skilled than me can make a very nice rocker/slider switch UDF to suppliment what Koda can't do.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$_1 = GUICreate("Rocker Switches", 170, 167, 275, 162, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_TABSTOP))
$button_1 = GUICtrlCreateRadio("ON", 20, 16, 65, 30, BitOR($GUI_SS_DEFAULT_RADIO,$BS_PUSHLIKE))
GUICtrlSetState(-1, $GUI_CHECKED)
$button_2 = GUICtrlCreateRadio("OFF", 85, 16, 65, 30, BitOR($GUI_SS_DEFAULT_RADIO,$BS_PUSHLIKE))
$button_3 = GUICtrlCreateRadio("ON", 20, 51, 65, 30, BitOR($GUI_SS_DEFAULT_RADIO,$BS_PUSHLIKE))
$button_4 = GUICtrlCreateRadio("OFF", 85, 51, 65, 30, BitOR($GUI_SS_DEFAULT_RADIO,$BS_PUSHLIKE))
$button_5 = GUICtrlCreateRadio("ON", 20, 86, 65, 30, BitOR($GUI_SS_DEFAULT_RADIO,$BS_PUSHLIKE))
$button_6 = GUICtrlCreateRadio("OFF", 85, 86, 65, 30, BitOR($GUI_SS_DEFAULT_RADIO,$BS_PUSHLIKE))
$button_7 = GUICtrlCreateRadio("ON", 20, 121, 65, 30, BitOR($GUI_SS_DEFAULT_RADIO,$BS_PUSHLIKE))
$button_8 = GUICtrlCreateRadio("OFF", 85, 121, 65, 30, BitOR($GUI_SS_DEFAULT_RADIO,$BS_PUSHLIKE))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
EndSwitch
WEnd

Posted Image

Edited by MKANET
Link to comment
Share on other sites

If it doesn't have to be a slider, how about this idea:

#include <GUIConstants.au3>
#include <GuiButton.au3>
#include <GUIConstantsEx.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 150, 85)
$Button1 = GUICtrlCreateButton("On", 32, 24, 75, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetBkColor ($Button1, 0x87f717)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _ToggleButton()
    EndSwitch
WEnd

Func _ToggleButton()
    $readButton = _GUICtrlButton_GetText($Button1)
    If $readButton = "On" Then
        _GUICtrlButton_SetText($Button1, "Off")
        GUICtrlSetBkColor ($Button1, 0xFF0000)
    Else
        _GUICtrlButton_SetText($Button1, "On")
        GUICtrlSetBkColor ($Button1, 0x87f717)
    EndIf
EndFunc
Link to comment
Share on other sites

Oh cool. I didn't think of that! Could you make it so the button on the ON position is pressed down like in my code above; and, when it's OFF, for it to be in the up position? Everything else can be the same.

Link to comment
Share on other sites

Oh cool. I didn't think of that! Could you make it so the button on the ON position is pressed down like in my code above; and, when it's OFF, for it to be in the up position? Everything else can be the same.

I tried playing with it, but it appears that coloring the button makes the button look flat no matter what state you specify. If you take away the color, you can make it look pushed or non-pushed. So, unless someone more experienced than myself can offer a workaround to that, I don't see any way to do exactly what you want.

Link to comment
Share on other sites

  • Moderators

Hi,

There is no work-around. There is a bug deep in the AutoIt core code which means that colouring buttons causes all sorts of problems - and it will not be fixed any time soon. ;)

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

I appreciate for the response Melba23. Now it makes sense why there aren't that many sample scripts with colored buttons. I wonder if it's still possible to use the script below; and just have the ON button look like it's pressed down. Maybe the color prevents it from happening? I tried to do it in Koda, but it didn't work correctly.

BTW: I just LOVE Extended Message Box. After I saw that, I thought pretty much anything could be possible ;)

Link to comment
Share on other sites

  • Moderators

MKANET,

Maybe the color prevents it from happening?

Exactly. ;)

Delighted you like ExtMsgBox - do check out the other UDFs in my sig as well. :)

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

MKANET,

here is an example using labels:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
$bOn = True
$hGUI = GUICreate("On/Off", 120, 45)
GUICtrlCreateLabel("ON", 10, 10, 50, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetFont(-1, 8, 800, 0, "Arial",5)
GUICtrlSetColor(-1, 0xC0C0C0)
GUICtrlSetBkColor(-1, 0x101010)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateLabel("OFF", 60, 10, 50, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetFont(-1, 8, 800, 0, "Arial",5)
GUICtrlSetColor(-1, 0xC0C0C0)
GUICtrlSetBkColor(-1, 0x101010)
GUICtrlSetState(-1, $GUI_DISABLE)
$hSwitch = GUICtrlCreateLabel("", 60, 10, 50, 25)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")
GUICtrlSetBkColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)

While 1
Switch GUIGetMsg()
  Case -3
   Exit
  Case $hSwitch
   $aPos = ControlGetPos($hGUI, "", $hSwitch)
   If $bOn Then
    For $i = 0 To 50
     ControlMove($hGUI, "", $hSwitch, $aPos[0] - $i, $aPos[1])
    Next
    $bOn = False
   Else
    For $i = 0 To 50
     ControlMove($hGUI, "", $hSwitch, $aPos[0] + $i, $aPos[1])
    Next
    $bOn = True
   EndIf
EndSwitch
WEnd
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...