Jump to content

TrayCreateItem and set icon or image


g52
 Share

Recommended Posts

Hi,

Will anyone advise how to set the icon or image for each item?

Local $idDisplay  = TrayCreateItem("Display")
Local $idPrinter   = TrayCreateItem("Printer")

 

Thank you very much for your help

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate forum, as the DEV forum clearly states

Quote

Do not create AutoIt-related topics here, use the AutoIt General Help and Support

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • 3 years later...

You mean like this?  The example uses embedded icons but can easily be changed to use external icons (like from shell32.dll).

 

Edited by TheXman
Link to comment
Share on other sites

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

Opt("TrayMenuMode", 3)

Example()

Func Example()
    Local $iSettings = TrayCreateMenu("Settings")
    Local $iDisplay = TrayCreateItem("Display", $iSettings)
    _TrayItemSetIcon(-1,"shell32.dll", 14)
    Local $iPrinter = TrayCreateItem("Printer", $iSettings)
    _TrayItemSetIcon(-1,"shell32.dll", 14)
    TrayCreateItem("")
    Local $idAbout = TrayCreateItem("About")
    TrayCreateItem("")
    Local $idExit = TrayCreateItem("Exit")
    TraySetState($TRAY_ICONSTATE_SHOW)
    While 1
        Switch TrayGetMsg()
            Case $idAbout
                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 $iDisplay, $iPrinter
                MsgBox($MB_SYSTEMMODAL, "", "A sub menu item was selected from the tray menu.")
            Case $idExit
                ExitLoop
        EndSwitch
    WEnd
EndFunc

Here is all my code, it still doesn't work 😞

Can anyone help me to fix it please?

Edited by g52
Link to comment
Share on other sites

  • Developers
On 4/26/2017 at 12:26 AM, g52 said:

I tried, but it does not work

GuiCtrlSetImage ( $ idDisplay , "shell32.dll" , 4 )
TrayItemSetState ( $ idDisplay , "shell32.dll" , 4 )

 

14 hours ago, g52 said:

Really no one will help?

funny ...  a bump after 3+ years ....   that must be a first! 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hi everybody :)
I would like to add a (long, as usual) comment to what has been said before, hoping it may help @g52 in the future in his methodology, when he'll try to solve an issue by himself.

Until today, I never used any script concerning tray menus and this thread made me start digging, after I discovered g52's code ("Here is all my code, it still doesn't work") and before Nine replied to him.

When I ran g52's code, I had this error :

==> Unknown function name : _TrayItemSetIcon(-1,"shell32.dll", 14)

So I googled on "AutoIt" "_TrayItemSetIcon" and it quickly led me to Holger's thread : I needed the include file "ModernMenuRaw.au3" to have g52's script running.

If I may give an advice now : when you see that OP's 1st post (Holger) and his download link are dated 2008, just have a quick look at all the replies found in his thread, maybe you'll find another known user who uploaded a reworked version of the script, or more examples, or maybe a version compatible with new AutoIt releases etc...

This happens in Holger's thread, because @AZJIO on page 15, immediately followed by @LarsJ also on page 15, both of them make it easier for us, AZJIO by giving detailed examples and LarsJ by reworking a new version (2015) of Holger script.

So, based on AZJIO's example : _TrayCreateMenu.au3
And based on the simple way Holger checks events with the same repeated line after each tray menu item creation :

GUICtrlSetOnEvent(-1, "MenuEvents")

...then it is easy to end with something like this, quickly done, with only 2 events checked for "About..." and "SubItem => Restart"

#include "ModernMenuRaw.au3"

#NoTrayIcon
Opt("GUIOnEventMode", 1)
Opt("TrayMenuMode", 7)

$nTrayIcon = _TrayIconCreate("My program", "shell32.dll", -209)

_TrayCreateContextMenu()
$bUseAdvTrayMenu = False ; True would display selected items differently

$hMenu = _TrayCreateMenu('Menu')
_TrayItemSetIcon(-1, "shell32.dll", -5)
_TrayCreateItem("")

_TrayCreateItem('Open', $hMenu)
_TrayItemSetIcon(-1, "shell32.dll", -5)
_TrayCreateItem("", $hMenu)

For $i = 1 To 10
    _TrayCreateItem('Item ' & $i, $hMenu)
    _TrayItemSetIcon(-1, "shell32.dll", -$i)
Next

$action = _TrayCreateMenu('Subitem')
_TrayItemSetIcon(-1, "shell32.dll", -177)

$nUpd = _TrayCreateItem('Restart', $action)
_TrayItemSetIcon(-1, "shell32.dll", -147)
GUICtrlSetOnEvent(-1, "MenuEvents")

$nSave = _TrayCreateItem('Save', $action)
_TrayItemSetIcon(-1, "shell32.dll", -195)
GUICtrlSetOnEvent(-1, "MenuEvents")

$nClose = _TrayCreateItem('Close', $action)
_TrayItemSetIcon(-1, "shell32.dll", -110)
GUICtrlSetOnEvent(-1, "MenuEvents")

$nAdd = _TrayCreateItem('Add', $action)
_TrayItemSetIcon(-1, "shell32.dll", -5)
GUICtrlSetOnEvent(-1, "MenuEvents")

$nFolder = _TrayCreateItem('Folder', $action)
_TrayItemSetIcon(-1, "shell32.dll", -4)
GUICtrlSetOnEvent(-1, "MenuEvents")

$nSet = _TrayCreateItem('Option', $action)
_TrayItemSetIcon(-1, "shell32.dll", -91)
GUICtrlSetOnEvent(-1, "MenuEvents")

$nAbout = _TrayCreateItem('About')
_TrayItemSetIcon(-1, "shell32.dll", -222)
GUICtrlSetOnEvent(-1, "MenuEvents")

$nExit = _TrayCreateItem('Exit')
_TrayItemSetIcon(-1, "shell32.dll", -216)
GUICtrlSetOnEvent(-1, "MenuEvents")

_TrayIconSetState()

While 1
    Sleep(10)
WEnd

Func MenuEvents()
    Local $Msg = @GUI_CtrlID ; great one !

    Switch $Msg
        Case $nExit
            _TrayIconDelete($nTrayIcon)
            Exit

        Case $nAbout
            Msgbox(64, "About", "About text here")

        Case $nUpd
            Msgbox(64, "Restart", "Restart text there")

        ; Case ...

    EndSwitch
EndFunc

Now if you don't want to script with Opt("GUIOnEventMode", 1) but with While... Wend loop mode, it's doable (see Holger's examples in LarsJ download link, which includes 4 subfolders, one per each contributor) but it seems simpler to script it like above.

Concerning the very same kind of script, @Melba23 helped a user in this link, indicating him that OnEvent mode and TrayGetMsg in a loop won't work together

A last recommandation : when you see that your script "doesn't work", try to add some debug lines to your code, it's often a big help.

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