jpujut

how to immediately update tray items when clicking on tray icon?

4 posts in this topic

#1 ·  Posted

I have a script that gets a list of visible windows and makes a tray item of each in the tray menu. I'd also want it to update every time I want to access that menu again, in case new windows have appeared or some would have been closed. I'm using TrayOnEventMode option and trying to get the update going with TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN...) as in the snippet below. However, the list doesn't update until after the tray menu has been closed again. I think I could work around this by having the script update the list constantly in the background, but that feels like it'd take up unnecessary amount of resources since the script might be used rarely by the user anyway. I tried using TraySetOnEvent($TRAY_EVENT_MOUSEOVER...), and that kind of does it, but it actually still calls my function many times (since the mouse cursor moves quite a bit on the icon) before the tray menu is opened. I could put another menuitem to call the update function, but I'd much rather have just the minimal amount of user actions. Similarly I could have tray menu open with a double click (with TraySetClick()) and have the first click just call the update function, but can anyone tell me if there's a way to call the update once and then have the menu open without these gimmicks?

 

Opt("TrayOnEventMode", 1)

TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN, "UpdateWindowsList")

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

jpujut,

Interesting problem - here is my best effort so far:

#include <TrayConstants.au3>

Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown.

; Flag to show if update has occurred
Global $fUpdate = False

$cTray_Change_Item = TrayCreateItem("Item set at " & @SEC)
TrayCreateItem("")
$cTray_Exit_Item = TrayCreateItem("Exit")
TrayItemSetOnEvent($cTray_Exit_Item, "_Exit")

; Update tray when mouse first goes over
TraySetOnEvent($TRAY_EVENT_MOUSEOVER, "_Update_Tray")

While 1
    Sleep(10)
WEnd

Func _Update_Tray()
    ; If not already updated
    If Not $fUpdate Then
        ; Run update code
        ConsoleWrite("Updating at: " & @SEC & @CRLF)
        TrayItemSetText($cTray_Change_Item, "Item set at " & @SEC)
        ; Set flag to show update completed
        $fUpdate = True
        ; Register Adlib to clear flag after 1 sec
        AdlibRegister("_Reset_UpDate", 1000)
        ConsoleWrite("Adlib set" & @CRLF)
    EndIf
EndFunc

Func _Reset_UpDate()

    AdlibUnRegister("_Reset_Update")
    $fUpdate = False
    ConsoleWrite("Reset" & @CRLF)
EndFunc

Func _Exit()
    Exit
EndFunc

It updates the tray when the mouse first goes over the icon, sets a flag to prevent multiple updates, and sets a timer to clear the flag after 1 sec - this timer does not run until after the menu is closed, so the delay could be even shorter.

Please ask if you have any questions.

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for the quick response! Also that seems to have done it! So thanks a lot, I really appreciate it! :) As a newbie I tried to get similar effect with Sleep(), but couldn't get it to work. Didn't know about AdlibRegister until now, so thanks for that, too!

Tho is it possible to have the same effect with just a function of my own (using a flag and a timer like you did) and how exactly does Adlib -functions differ from any other function? Tried googling and function reference, but still didn't quite get it. If you can explain or point me to a good reference, I'd be a bit wiser still! Thanks!

Edited by Melba23
Removed quote

Share this post


Link to post
Share on other sites

#4 ·  Posted

jpujut,

When you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - I know what I wrote and it just pads the thread unnecessarily.

I do not really understand the "just a function of my own" statement - that code already uses its own own functions in that code - so can you please explain a little more what it is that want to do.

As to how Adlib works - you tell AutoIt to run the defined function after a specified time and that is what it does. The trick here is to tell AutoIt to stop running the function after it is called the first time. That way you can wait for a while to do something without blocking the whole script.

M23

 


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

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

  • Similar Content

    • ur
      By ur
      I need to read the status of OneDrive icon running in the taskbar and update the status in a log file.
      2nd part I can complete
      But the first part, how to read the status of tray menu icon.

      I need the text above from the tray icon.
      I looked into , but not that helpful.
       
       
    • WoodGrain
      By WoodGrain
      Hi All,
      I'm using UseTraySetIcon("shell32.dll", xx) to set my tray icon to a specific icon while running, is there a way I can specify this icon when I compile my script into an exe so my exe icon is the same?
      Thanks!
    • 9252Survive
      By 9252Survive
      Hello Guys, 
      Is there a way to have Auto it Exe messages/line number when you hover over the tray icon just at it shows when you run Au3.?
      For an example, if I the script has paused due to WinWait or Sleep I could hover over the icon and it shows the line where it's at. Is there a way to achieve the same with the compiled exe? 
      Many thanks for the help! 
       
    • Kyou
      By Kyou
      I have a working code here. 
      #include <GUIConstants.au3> #include <Constants.au3> Opt("GUIOnEventMode",1) $title = "WINDOW" $text="" $fullTest = WinExistsExact($title, $text) $PASSWORD = "PASS" $retryCount = 0 $stop = 1 ;<<<<<<<<<<<<<<<<<<<<<<<<< While 1 if $retryCount > 0 then Exit $input = InputBox("Password Protected", "Enter the password to continue", "", "*") If @error Or $input <> $PASSWORD Then MsgBox(4096,"Error", "Incorrect Password") $retryCount = $retryCount + 1 Else ;MsgBox(4096,"Success", "Password Accepted") ExitLoop EndIf Wend If $fullTest = 1 Then ;MsgBox(0, "WINDOW", "Press Ok to continue") GUICreate("WINDOW", 241, 78) ;$label = GUICtrlCreateInput ("test", 10, 10, 50, 20) GuiSetOnEvent($GUI_EVENT_CLOSE,"Quit") $button = GUICtrlCreateButton ("Start", 18, 31, 100, 25) $button1 = GUICtrlCreateButton ("Hide", 128, 31, 91, 25) ; <----- THIS IS THE BUTTON GuiCtrlSetOnEvent($button,"DoScript") GUISetState (@SW_SHOW) GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") ;<<<<<<<<<<<<<<<<<<<<< Else MsgBox(0, "WINDOW", "You need to run WINDOW client first.") EndIf While 1 Sleep(10) Wend Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) If BitAND($wParam, 0x0000FFFF) = $button Then $stop = not $stop If $stop = 0 Then GUICtrlSetData($button, "Stop") If $stop = 1 Then GUICtrlSetData($button, "Start") EndIf Return $GUI_RUNDEFMSG EndFunc Func WinExistsExact($title, $text) AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHEXACT ) $res = WinExists($title, $text) AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHSTART ) Return $res EndFunc Func DoScript() While 1 For $i = 1 to 300 Sleep (10) If $stop = 1 Then Return ; GuiCtrlSetData($label, $i) Next ControlSend ( $title, "", 0, "{F12}") $a = Random(300, 900, 1) For $i = 1 to $a Sleep (10) If $stop = 1 Then Return Next ControlSend ( $title, "", 0, "{Del}") $a = Random(100, 500, 1) For $i = 1 to $a Sleep (10) If $stop = 1 Then Return Next WEnd EndFunc Func Quit() Exit EndFunc What I wanted to do is to make the 'Hide' button minimized the entire window when clicked and while it is on tray, there will be a menu if a user right clicked on it. Exactly what the code below should do.
      #NoTrayIcon #include <guiconstantsex.au3> #include <constants.au3> Opt("TrayOnEventMode", 1) Opt("TrayMenuMode", 3) ; Create tray menu items and set the events $About = TrayCreateItem("About") TrayItemSetOnEvent($About, "_About") TrayCreateItem("") ; Create a separator line. $Exit = TrayCreateItem("Exit") TrayItemSetOnEvent($Exit, "_Exit") ; Set the action if you left click on the tray icon TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "SpecialEvent") ; Show the menu if the icon is right clicked TraySetClick(8) Global $MainGui = GUICreate("TestGUI", 392, 316, -1, -1) GUISetState() ;Test() While 1 Switch GUIGetMsg() Case $GUI_EVENT_MINIMIZE ; Hide the GUI GUISetState(@SW_HIDE) ; Show the icon TraySetState(1) Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Func SpecialEvent() ; Show the GUI GUISetState(@SW_SHOW) GUISetState(@SW_RESTORE) ; Hide the icon TraySetState(2) EndFunc ;==>SpecialEvent Func _About() MsgBox(0,0,"Test") EndFunc Func _Exit() Exit EndFunc My problem is I cannot integrate this code to the one I have. Can anyone help me with this? I would be very much thankful as I am just starting to learn.
    • WoodGrain
      By WoodGrain
      Hi All,
      I have a custom icon for the tray menu icon, which was working until I copied in the example code for a custom menu when clicking on the tray icon, now it just displays the default AutoIt tray icon. The menu works fine, but the tray icon initiates as the AutoIt default icon and the icon won't change on event.
      Can anyone advise what I need to do to use my custom icon while retaining the tray custom menu?
      Thanks!
      Also, the While 1 ... WEnd makes me nervous, how does this not lock up the system? If anyone would like to comment on that - thanks.
      Opt("TrayMenuMode", 3) #include <TrayConstants.au3> Local $idAbout = TrayCreateItem("About") Local $idExit = TrayCreateItem("Exit") TraySetState($TRAY_ICONSTATE_SHOW) While 1 Switch TrayGetMsg() Case $idAbout MsgBox(1,"test","test msg") Case $idExit Exit EndSwitch WEnd If Not FileExists (@TempDir & "\MyIcons") Then DirCreate(@TempDir & "\MyIcons") FileInstall(".\Grey-1.ico", @TempDir & "\MyIcons\Grey-1.ico", 1) FileInstall(".\Yellow-1.ico", @TempDir & "\MyIcons\Yellow-1.ico", 1) TraySetIcon(@TempDir & "\MyIcons\Grey-1.ico");Change icon to grey initially ; If X happens then change tray icon to yellow ; If Y happens then change tray icon to grey