Jump to content

Recommended Posts

Posted (edited)

Here is a small UDF to create simple toggle buttons. I made this for one of my projects but I decided to make it available for those who might find it useful for their projects. It has no other dependencies except GDI+. Basically call _GDIPlus_Startup() before using any other function from this UDF and _GDIPlus_Shutdown() after you properly deleted all controls created with this UDF. Colors and sizes are customizable and there is a picture control that is associated with each toggle button so you can use other AutoIt functions with these buttons, like GUICtrlSetCursor(), GUICtrlSetTip(), etc.

image.png.fde94e9ddef4e6715b098e6bfffe4541.png

Here is a simple example:

#include <ToggleButton.au3>

_GDIPlus_Startup()

$mAdminOn = GUICtrlToggle_CreateTheme(0xFF2C6E49, 0x30B5C99A, 0xFFB5C99A, 5, 2)
$mAdminOff = GUICtrlToggle_CreateTheme(0xFF800F2F, 0x30F45B69, 0xFFF45B69, 5, 2)

$hMain = GUICreate('Toggle Button UDF', 300, 200)
GUICtrlCreateLabel('Admin mode', 10, 10, 80, 25, 0x200) ; SS_CENTERIMAGE
GUICtrlSetFont(-1, 10, 500, 0, 'Segoe UI')
$mHToggle = GUICtrlCreateToggle($hMain, 90, 10, 50, 25)
GUICtrlSetTip(GUICtrlToggle_CtrlID($mHToggle), 'Click me')
GUICtrlToggle_SetTheme($mHToggle, $mAdminOff)
$mVToggle = GUICtrlCreateToggle($hMain, 200, 20, 40, 170, $TOGGLE_VERTICAL)
GUICtrlSetTip(GUICtrlToggle_CtrlID($mVToggle), 'Click me multiple times')
GUISetState()

While True
    Switch GUIGetMsg()
        Case $mHToggle['ID']
            ToggleState($mHToggle)
            If GUICtrlToggle_State($mHToggle) Then
                GUICtrlToggle_SetTheme($mHToggle, $mAdminOn)
            Else
                GUICtrlToggle_SetTheme($mHToggle, $mAdminOff)
            EndIf
        Case $mVToggle['ID']
            ToggleState($mVToggle)
            $iSwitchSize = GUICtrlToggle_SwitchSize($mVToggle)
            $iSwitchSize += 1
            If $iSwitchSize > 10 Then $iSwitchSize = 3
            GUICtrlToggle_SwitchSize($mVToggle, $iSwitchSize)
            GUICtrlToggle_SwitchColor($mVToggle, 0xFF000000 + Random(0, 0xFFFFFF, 1))
        Case -3 ; GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

GUICtrlDeleteToggle($mVToggle)
GUICtrlDeleteToggle($mHToggle)
_GDIPlus_Shutdown()

Enjoy!

Edit: added a little bit of code to detect the background color of the parent window, if it's not provided.

 

ToggleButton.au3

Edited by Andreik
Posted

Added to the wiki 🙂

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

  • Andreik changed the title to Toggle Control UDF

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
  • Recently Browsing   0 members

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