Jump to content

Tray Icon Help - When I Already Have A Loop


Recommended Posts

I have the below code I've tried but the right-click menu items do nothing, I'm guessing it's on account of TrayGetMsg()?

Any help would be much appreciated!

Opt("TrayMenuMode", 3)

TrayIcon()

Func TrayIcon()
    Global $trayAbout = TrayCreateItem("About")
    TrayItemSetOnEvent(-1, "IconAbout")
    Global $traySettings = TrayCreateItem("Settings")
    TrayItemSetOnEvent(-1, "IconSettings")
    TraySetOnEvent(-13, "About"); $TRAY_EVENT_PRIMARYDOUBLE
    Global $trayExit = TrayCreateItem("Exit")
    TrayItemSetOnEvent(-1, "IconExit")
    TraySetClick(8); $TRAY_CLICK_SECONDARYDOWN
    TraySetState(1); Show the icon
EndFunc

Func IconAbout()
    MsgBox(0, "About", "This is info about the app")
EndFunc

Func IconSettings()
    StartGui()
EndFunc

Func IconExit()
    Exit
EndFunc

; Primary app loop to do stuff
While 1
    ;I've tried adding in the TrayGetMsg() here but doesn't work
    Switch TrayGetMsg()
        Case $trayAbout
            IconAbout()
    EndSwitch
    ; do the rest of my app stuff in a loop every 20 seconds, this loop interval is required
    Sleep(20000)
WEnd

 

Link to post
Share on other sites

take ideas maybe help  :idea:

#include <StringConstants.au3>
#include <TrayConstants.au3>

Opt("TrayMenuMode", 3) ; These are options 1 and 2 for TrayMenuMode.

Local $bPaused = False
TraySetIcon(StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", $STR_NOCASESENSEBASIC, -1) - 1) & "\Icons\MyAutoIt3_Blue.ico")

#Region === Tray_Menu ===
Local $idPaused = TrayCreateItem("Pause", -1, -1, $TRAY_ITEM_RADIO)
TrayItemSetState(-1, $TRAY_UNCHECKED)
TrayCreateItem("") ; Create a separator line.
Local $idExit = TrayCreateItem("Exit")
#EndRegion === Tray_Menu ===

ToolTip(@ScriptName & @CRLF & " ", @DesktopWidth / 2, @DesktopHeight / 5, "Started", 1)
Sleep(3000)
ToolTip("")

While 1
    _GetTrayMsg()
    Sleep(50)
WEnd

Exit

;----------------------------------------------------------------------------------------
Func GoToExit()    ; exit
    Exit
EndFunc   ;==>GoToExit
;----------------------------------------------------------------------------------------
Func _GetTrayMsg()
    While 1
        Switch TrayGetMsg()

            Case $TRAY_EVENT_SECONDARYDOUBLE ;
                ConsoleWrite("$TRAY_EVENT_SECONDARYDOUBLE" & @CRLF)

            Case $TRAY_EVENT_PRIMARYDOUBLE ; Paused the loop.
                TogglePause()

            Case $idPaused ; Paused the loop.
                TogglePause()

            Case $idExit ; Exit the loop.
                GoToExit()

        EndSwitch

        If $bPaused = False Then ExitLoop

    WEnd
EndFunc   ;==>_GetTrayMsg
;----------------------------------------------------------------------------------------
Func TogglePause()

    If $bPaused = False Then
        $bPaused = True
        TrayItemSetState($idPaused, $TRAY_CHECKED)    ; $TRAY_UNCHECKED, $TRAY_CHECKED
        TraySetIcon(StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", $STR_NOCASESENSEBASIC, -1) - 1) & "\Icons\MyAutoIt3_Red.ico")
    Else
        $bPaused = False
        TrayItemSetState($idPaused, $TRAY_UNCHECKED)    ; $TRAY_UNCHECKED, $TRAY_CHECKED
        TraySetIcon(StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", $STR_NOCASESENSEBASIC, -1) - 1) & "\Icons\MyAutoIt3_Blue.ico")
    EndIf

;~  ConsoleWrite("$bPaused=" & $bPaused & @CRLF)

EndFunc   ;==>TogglePause

;----------------------------------------------------------------------------------------

 

Edited by ioa747
Link to post
Share on other sites

OnEvent mode also work!

#NoTrayIcon
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <TrayConstants.au3>

Opt("TrayMenuMode", 3) ; The default tray menu items will not be shown and items are not checked when selected. These are options 1 and 2 for TrayMenuMode.
Opt("TrayOnEventMode", 1) ; Enable TrayOnEventMode.

Example()

Func Example()
    TrayCreateItem("About")
    TrayItemSetOnEvent(-1, "About")

    TrayCreateItem("test")
    TrayItemSetOnEvent(-1, "_test")

    TrayCreateItem("") ; Create a separator line.

    TrayCreateItem("Exit")
    TrayItemSetOnEvent(-1, "ExitScript")

    TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "About") ; Display the About MsgBox when the tray icon is double clicked on with the primary mouse button.

    TraySetOnEvent($TRAY_EVENT_SECONDARYDOUBLE, "_test") ;

    TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu.

    While 1
        Sleep(100) ; An idle loop.
    WEnd
EndFunc   ;==>Example

Func About()
    ; Display a message box about the AutoIt version and installation path of the AutoIt executable.
    MsgBox($MB_SYSTEMMODAL, "", "AutoIt tray menu example." & @CRLF & @CRLF & _
            "Version: " & @AutoItVersion & @CRLF & _
            "Install Path: " & StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", $STR_NOCASESENSEBASIC, -1) - 1)) ; Find the folder of a full path.
EndFunc   ;==>About

Func ExitScript()
    Exit
EndFunc   ;==>ExitScript

Func _test()
    ConsoleWrite("$TRAY_EVENT_SECONDARYDOUBLE" & @CRLF)
EndFunc

 

Edited by ioa747
Link to post
Share on other sites

These look like they loop every 50 and 100 ms, I need my program loop to be several seconds, normally between 5 seconds and 60 seconds depending on conditions

; do the rest of my app stuff in a loop every 20 seconds, this loop interval is required
    Sleep(20000)
Link to post
Share on other sites
1 hour ago, ioa747 said:

OnEvent mode also work!

#NoTrayIcon
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <TrayConstants.au3>

Opt("TrayMenuMode", 3) ; The default tray menu items will not be shown and items are not checked when selected. These are options 1 and 2 for TrayMenuMode.
Opt("TrayOnEventMode", 1) ; Enable TrayOnEventMode.

Example()

Func Example()
    TrayCreateItem("About")
    TrayItemSetOnEvent(-1, "About")

    TrayCreateItem("test")
    TrayItemSetOnEvent(-1, "_test")

    TrayCreateItem("") ; Create a separator line.

    TrayCreateItem("Exit")
    TrayItemSetOnEvent(-1, "ExitScript")

    TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "About") ; Display the About MsgBox when the tray icon is double clicked on with the primary mouse button.

    TraySetOnEvent($TRAY_EVENT_SECONDARYDOUBLE, "_test") ;

    TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu.

    While 1
        Sleep(100) ; An idle loop.
    WEnd
EndFunc   ;==>Example

Func About()
    ; Display a message box about the AutoIt version and installation path of the AutoIt executable.
    MsgBox($MB_SYSTEMMODAL, "", "AutoIt tray menu example." & @CRLF & @CRLF & _
            "Version: " & @AutoItVersion & @CRLF & _
            "Install Path: " & StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", $STR_NOCASESENSEBASIC, -1) - 1)) ; Find the folder of a full path.
EndFunc   ;==>About

Func ExitScript()
    Exit
EndFunc   ;==>ExitScript

Func _test()
    ConsoleWrite("$TRAY_EVENT_SECONDARYDOUBLE" & @CRLF)
EndFunc

 

double right click = ConsoleWrite("$TRAY_EVENT_SECONDARYDOUBLE" & @CRLF)

double left click Msgbox About

q2vAqRg.png  uZzCmqa.png

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

    No registered users viewing this page.

×
×
  • Create New...