Jump to content

Clear tray-menu completely


Recommended Posts

Simple question, but I can't seem to find a method of clearing the whole of a system-tray menu.

Whilst you can clear submenus by calling TrayItemDelete() on each submenu controlID, any items in the root menu will remain, and since you have no way of reliably enumerating these you can't remove them. Presumably there might be a controlID you can give to TrayItemDelete() which represents the root menu. If so, what?

Link to comment
Share on other sites

TrayMenu()
while 1
sleep(1000)
wend
exit

func TrayMenu()
 Opt("TrayMenuMode",11) 

 global $menuID=-1 

; $submenuID=TrayCreateMenu("Sub Menu") ;Uncomment these two lines to test with a submenu > Works OK.
; global $menuID=$submenuID 

 TrayItemSetOnEvent(-1,"TrayClick")
 $t_2= TrayCreateItem("Reload Menu",$menuID)
 TrayItemSetOnEvent(-1,"TrayClick")
 $t_2= TrayCreateItem("Exit",$menuID)
 TrayItemSetOnEvent(-1,"TrayClick")
 Opt("TrayIconHide",0)
 opt("TrayOnEventMode",1)
 TraySetState()
endfunc

func TrayClick()
$t_id=@tray_id
$t_msg=TrayItemGetText(@tray_id)
  Select
   Case $t_msg = "Reload Menu"
   ; Here we need to clear the menu so that it can be rebuilt with new values from a config file.
   ; If we are dealing with a submenu, this is no problem. 
   ; If the root menu, it IS a problem since we can only delete individual items here. 
   ; Note that in a real case we don't know in advance how many items there will be, or what their control IDs are.
   TrayItemDelete($menuID)
   TrayMenu()
   if $menuID<>-1 then 
     msgbox(64,"Reload","Menu items reloaded",5)
   else
     msgbox(16,"Error","Menu items duplicated, due to inability to clear the root menu.",5)
   endif
   Case $t_msg = "Exit"
   exit
  endselect
endfunc

Link to comment
Share on other sites

Merry Christmas!

Opt("TrayIconHide", 0)
Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 11)

Global $aTrayItems[1]

_TrayMenu()

While 1
    Sleep(10)
WEnd

Func _TrayMenu()
    Global $aTrayItems[Random(3, 11, 1)]

    $aTrayItems[0] = TrayCreateItem("Reload Menu") ; first item
    TrayItemSetOnEvent(-1, "_TrayClick")
    For $n = 1 to UBound($aTrayItems) - 2
        $aTrayItems[$n] = TrayCreateItem("Random Items: " & $n) ; random items
        TrayItemSetOnEvent(-1, "_TrayClick")
    Next
    $aTrayItems[UBound($aTrayItems) - 1] = TrayCreateItem("Exit") ; last item
    TrayItemSetOnEvent(-1, "_TrayClick")
    TraySetState()
EndFunc   ;==>TrayMenu

Func _TrayClick()
    Local $idTray = @TRAY_ID
    Local $sMsg = TrayItemGetText(@TRAY_ID)
    Switch $sMsg
        Case "Reload Menu"
            _TrayItemsDelete()
            _TrayMenu()

        Case "Exit"
            Exit

        Case Else
            MsgBox(64, "Tray Item", "You clicked '" & $sMsg & "'", 5)
    EndSwitch
EndFunc   ;==>TrayClick

Func _TrayItemsDelete()
    For $n = 0 To UBound($aTrayItems) - 1
        TrayItemDelete($aTrayItems[$n])
    Next
EndFunc

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thx for suggestion. I had considered something similar but it would have been a lot of work to modify the code so an array is updated each time a menu-item is added. Anyway you got me thinking, and here's a neat (Well, I think it is :mellow:) UDF that does the job with a simple change of function-name to TrayCreateItemEx:

TrayMenu()
while 1
sleep(1000)
wend
exit

func TrayMenu()
 Opt("TrayMenuMode",11) 
 TrayCreateItemEx("Reload Menu")
 TrayCreateItemEx("")
 TrayCreateItemEx("Exit")
 TrayCreateItemEx("Exit","Sub-Menu")
 Opt("TrayIconHide",0)
 opt("TrayOnEventMode",1)
 TraySetState()
endfunc

func TrayClick()
$t_id=@tray_id
$t_msg=TrayItemGetText(@tray_id)
  Select
   Case $t_msg = "Reload Menu"
   TrayClearMenu(1)
   TrayMenu()
   Case $t_msg = "Exit"
   exit
  endselect
endfunc

func TrayCreateItemEx($_text,$_submenu="",$_trayAction="TrayClick")
 $_menuID=-1
 if not IsDeclared("_trayItems")then 
  global $_trayItems[201]
  $_trayItems[0]=0
 endif

 if $_submenu<>"" then 
   if not IsDeclared("_trayMenus")then 
     $_menuID=TrayCreateMenuEx($_submenu)
   else
     for $_ct=1 to $_trayMenus[0]
       if TrayItemGetText($_trayMenus[$_ct])=$_submenu then 
         $_menuID=$_trayMenus[$_ct]
       endif
     next
     if $_menuID=-1 then $_menuID=TrayCreateMenuEx($_submenu)
   endif
   ;if no matching submenu found, create it: 
 endif; submenu section
 
 $_ctlID=TrayCreateItem($_text,$_menuID)
 TrayItemSetOnEvent(-1,$_trayAction)
 if $_submenu="" then
  $_trayItems[0]=$_trayItems[0]+1
  $_trayItems[$_trayItems[0]]=$_ctlID
 endif 
 return $_ctlID
endfunc

func TrayCreateMenuEx($_text,$_menuID=-1)
 if not IsDeclared("_trayMenus")then 
  global $_trayMenus[31]
  $_trayMenus[0]=0
 endif
 local $_ctlID=TrayCreateMenu($_text,$_menuID)
 $_trayMenus[0]+=1
 $_trayMenus[$_trayMenus[0]]=$_ctlID
 return $_ctlID
endfunc

func TrayClearMenu($_allMenus=0)
 ; 1 clears submenus as well as root menu
 local $_items=0
 if IsDeclared("_trayItems")then 
   $_items=$_trayItems[0]
   for $_ct=1 to $_trayItems[0]
    TrayItemDelete($_trayItems[$_ct])
   next
   $_trayItems[0]=0
 endif
 if $_allMenus>0 then 
   if IsDeclared("_trayMenus")then 
     $_items+=$_trayMenus[0]
     for $_ct=1 to $_trayMenus[0]
       TrayItemDelete($_trayMenus[$_ct])
     next
     $_trayMenus[0]=0
   endif
 endif
 return $_items
endfunc

This way, a menu can be built with a single command per option,

TrayCreateItemEx(OptionText[,SubMenu][,ClickAction])

-and you don't even need to specifically create the submenus, the code works-out if one needs creating.

When you're finished with it, a single call to TrayClearMenu(1) nukes the lot.

-Presently uses fixed-size arrays (200 items, 30 submenus) and only allows one-deep submenus.

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...