Jump to content

TrayItemGetHandle doesn't work


Recommended Posts


AutoIt help says



Returns the handle for a tray menu(item).

TrayItemGetHandle ( controlID )


controlID The control identifier (controlID) as returned by a TrayCreateItem or TrayCreateMenu function.



but it doesn't work if the controlID is a control created with TrayCreateItem. You can see it In the example


Opt("TrayMenuMode", 3)

Local $idMenu = TrayCreateMenu("Menu")
Local $idItem = TrayCreateItem("Item")
MsgBox(Default,"Test", _
            "Menu " & $idMenu & " " & TrayItemGetHandle($idMenu) _
            & @CRLF & _
            "Item " & $idItem & " " & TrayItemGetHandle($idItem) _


Is it a bug?

Link to comment
Share on other sites

I got the code in the manual it works fine on my computer:

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <TrayConstants.au3> ; Required for the $TRAY_ICONSTATE_SHOW constant.

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.

Global Const $MIM_APPLYTOSUBMENUS = 0x80000000, $MIM_BACKGROUND = 0x00000002 ; Constants required for SetMenuColor


Func Example()
        Local $idSettings = TrayCreateMenu("Settings") ; Create a tray menu sub menu with two sub items.
        Local $idDisplay = TrayCreateItem("Display", $idSettings)
        Local $idPrinter = TrayCreateItem("Printer", $idSettings)
        TrayCreateItem("") ; Create a separator line.

        Local $idAbout = TrayCreateItem("About")
        TrayCreateItem("") ; Create a separator line.

        Local $idExit = TrayCreateItem("Exit")

        TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu.

        SetMenuColor(0, 0xEEBB99) ; BGR color value, '0' refers to the tray menu itself.
        SetMenuColor($idSettings, 0x66BB99); BGR color value for the 'Settings' menu.

        While 1
                Switch TrayGetMsg()
                        Case $idAbout ; 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.

                        Case $idDisplay, $idPrinter
                                MsgBox($MB_SYSTEMMODAL, "", "A sub menu item was selected from the tray menu.")

                        Case $idExit ; Exit the loop.
EndFunc   ;==>Example

Func SetMenuColor($iMenuID, $iColor)
        Local $hMenu = TrayItemGetHandle($iMenuID) ; Get the internal menu handle

        Local $hBrush = DllCall("gdi32.dll", "hwnd", "CreateSolidBrush", "int", $iColor)
        $hBrush = $hBrush[0]

        Local $tMenuInfo = DllStructCreate("dword;dword;dword;uint;ptr;dword;ptr")
        DllStructSetData($tMenuInfo, 1, DllStructGetSize($tMenuInfo))
        DllStructSetData($tMenuInfo, 2, BitOR($MIM_APPLYTOSUBMENUS, $MIM_BACKGROUND))
        DllStructSetData($tMenuInfo, 5, $hBrush)

        DllCall("user32.dll", "int", "SetMenuInfo", "hwnd", $hMenu, "struct*", $tMenuInfo)
EndFunc   ;==>SetMenuColor



Link to comment
Share on other sites

@Trong You got it wrong.  The example gets the handle of a tray menu (not a tray item).  Issue found by @Tredi51 is about retrieving the handle of a trey item (see his example).

@jpm might be able to answer better than me.

Link to comment
Share on other sites

I don't think tray items have handles since they're not separate controls. The standard tray menu itself is a control that has a handle accessible via TrayItemGetHandle(0) but I looked through the MSDN info and couldn't find any reference to handles for menu items themselves.

If you create a menu on a GUI using GUICtrlCreateMenu and GUICtrlCreateMenuItem, you can get the handle of the menu control with GUICtrlGetHandle but you can't get a handle for a menu item using GUICtrlGetHandle.

And if you look at the _GUICtrlMenu... functions, none of them reference a handle for a menu item directly. It's always the handle of the menu control, and then the ID number of the menu item on that control.

So it sounds like the help info for TrayItemGetHandle could use some tweaking to clarify what it can and can't return.

Edited by TimRude
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

  • Recently Browsing   0 members

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