Sign in to follow this  
Followers 0
tjestr

TrayMenu Item

3 posts in this topic

Hello, I'm new to AutoIT and this is one of my first scripts.

I don't know how to solve this problem:

I want, if i click on the TrayItem "Autostart" that "Autostart" will be checked if unchecked, or unchecked if checked.

Also the function "autostart" should be called.

;----------Function Check Autostart----------------------------------------------------

func autostart ()
    If BitAND(TrayItemGetState($checkeditem), $TRAY_CHECKED) = $TRAY_CHECKED Then
        TrayItemSetState($checkeditem, $TRAY_UNCHECKED)
        MsgBox(0,"test", "The item should be unchecked")
    Else
        TrayItemSetState($checkeditem, $TRAY_CHECKED)
        MsgBox(0,"test", "The item should be checked")
    EndIf
EndFunc

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;++++++++++++++++++ Main ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

call ("startupstatus")

;----------Tray Menue------------------------------------------------------------------

Opt("TrayMenuMode", 1)

$create_vpnsite = TrayCreateItem("Create VPN Site")
$create_blank = TrayCreateItem("")
$vpn_connect = TrayCreateItem("VPN connect")
$vpn_disconnect = TrayCreateItem("VPN disconnect")
$create_blank = TrayCreateItem("")
$checkeditem = TrayCreateItem("Autostart")
$create_blank = TrayCreateItem("")
$exit = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $create_vpnsite
            call ("createvpnsite")
        Case $msg = $vpn_connect
            call ("connect")
        Case $msg = $vpn_disconnect
            call ("disconnect")
        Case $msg = $checkeditem
            call ("autostart")
        Case $msg = $exit
            ExitLoop
    EndSelect
WEnd

Share this post


Link to post
Share on other sites

#2 ·  Posted (edited)

This is not the correct way to do it, but it works all the same..lol

click on autostart and it gets ticked, click again and it's unticked.

#Include <Constants.au3>

Opt("TrayMenuMode", 1)

Global $State[1]

$create_vpnsite = TrayCreateItem("Create VPN Site")
$create_blank = TrayCreateItem("")
$vpn_connect = TrayCreateItem("VPN connect")
$vpn_disconnect = TrayCreateItem("VPN disconnect")
$create_blank = TrayCreateItem("")
$checkeditem = TrayCreateItem("Autostart")
$create_blank = TrayCreateItem("")
$exit = TrayCreateItem("Exit")
TraySetState()

LoadState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $create_vpnsite
;~             call ("createvpnsite")
        Case $msg = $vpn_connect
;~             call ("connect")
        Case $msg = $vpn_disconnect
;~             call ("disconnect")
        Case $msg = $checkeditem
            autostart()
        Case $msg = $exit
            SaveState()
            ExitLoop
    EndSelect
WEnd

func autostart ()
    If $State[0] = 1 Then
        TrayItemSetState($checkeditem, $TRAY_UNCHECKED)
        $State[0] = 0
    ElseIf  $State[0] = 0 Then
        TrayItemSetState($checkeditem, $TRAY_CHECKED)
        $State[0] = 1  
    EndIf
EndFunc

Func SaveState()
    IniWrite(@ScriptDir & '\state.ini', 'STATE', 'AutoStart', $State[0])
EndFunc

Func LoadState()
    If FileExists(@ScriptDir & '\state.ini') Then
        $State[0] = IniRead(@ScriptDir & '\state.ini', 'STATE', 'AutoStart', '')
        If $State[0] = 1 Then
            TrayItemSetState($checkeditem, $TRAY_CHECKED)
        ElseIf $State[0] = 0 Then
            TrayItemSetState($checkeditem, $TRAY_UNCHECKED)
        EndIf   
    EndIf
EndFunc

Edit: I forgot to add the check when the script is run what state the tray should be set as..

Added Save state to ini on Exit and Load state from ini on startup (if it exists).

Cheers

Edited by smashly

Share this post


Link to post
Share on other sites

Works great. Thank you man!

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