Click here to Skip to main content
Click here to Skip to main content

Shell Tray Info - Arrange your system tray icons

By , 26 Jun 2005
 

Overview

The Tray Icon Info application lets you enumerate your system tray icons and rearrange their positions, so that you can have your more frequently used icons positioned to the left most side (or right most depending on your personal preference). I wrote this as I got used to having the MSN Messenger icon on the left most side of the tray and found it annoying and inconvenient when newly added icons pushed it to the right. I had to exit and restart MSN Messenger to reposition it where I wanted. This application simplifies things for me.

Supported OS

This application only works on Windows XP. It may run on Windows 2003 too, but since I wasn't sure and since I didn't have the option to test it out, I have a version check and the program exits if it's a non-XP OS. If anyone's interested, they can comment out the version check and run it in on 2003 - but I have no idea as to whether it'll work or not.

Notes

  • For some tray icons, I am unable to retrieve the icon, so I show a red octagon with a white question mark.
  • Using the toolbar or the menu, you can send a left click, right click or a double click message to the tray icon.
  • You can use the << and >> icons to move the icons around the tray.
  • Copy (Ctrl-C) will copy some textual info to the clipboard (includes both the tool-tip text as well as the owner process path).
  • Double clicking an entry in the list view is equivalent to sending a double-click message.
  • The tray has hidden icons - mostly put there by Explorer. These icons won't have tool-tips.
  • And er, if you are wondering why the toolbar icons look so ghastly, guess who designed them!

Technical notes

The trick used here is to enumerate the buttons of the ToolbarWindow32 window that represents the system tray. The following code is used to locate this window (routine FindWindow/FindWindowEx stuff) :-

HWND FindTrayToolbarWindow()
{
    HWND hWnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);
    if(hWnd)
    {
        hWnd = ::FindWindowEx(hWnd,NULL,_T("TrayNotifyWnd"), NULL);
        if(hWnd)
        {
            hWnd = ::FindWindowEx(hWnd,NULL,_T("SysPager"), NULL);
            if(hWnd)
            {                
                hWnd = ::FindWindowEx(hWnd, NULL,_T("ToolbarWindow32"), NULL);
            }
        }
    }
    return hWnd;
}

Now I retrieve the count of tray icons :-

int count = (int)::SendMessage(m_hTrayWnd, TB_BUTTONCOUNT, 0, 0);

The number won't match the number of visible icons because of some hidden icons inserted by Explorer + the Hide Inactive Icons setting may be enabled.

BTW to retrieve toolbar info for each button, I use my CProcessData class. [CProcessData is a template class that makes it easy to use data allocated in a different process, and is useful when making inter-process SendMessage/PostMessage calls]

The dwData member of each TBBUTTON structure of the toolbar points to an undocumented structure. The first few bytes of the structure are as follows (on XP anyway) :-

struct TRAYDATA
{
    HWND hwnd;                
    UINT uID;                
    UINT uCallbackMessage;    
    DWORD Reserved[2];        
    HICON hIcon;                
};

There's more info, but I am not sure what the rest of it means. Reserved[0] has something to do with the visibility state of an icon when the Hide Inactive Icons setting is enabled, but it's behavior was too sporadic for me to give it a proper meaning and since I didn't really want that info, I didn't bother too much. All my Google searches on this undocumented structure resulted in nothing. It's times like this when you wish Windows provided full source code :-(

Anyway here's the code I use to retrieve the rest of the information I require.

CProcessData<TBBUTTON> data(dwTrayPid);
TBBUTTON tb = {0};
TRAYDATA tray = {0};
TrayItemInfo tifo = {0};

for(int i=0; i<count; i++)
{        
    ::SendMessage(m_hTrayWnd, TB_GETBUTTON, i, (LPARAM)data.GetData());        
    data.ReadData(&tb);            
    data.ReadData<TRAYDATA>(&tray,(LPCVOID)tb.dwData);

    DWORD dwProcessId = 0;
    GetWindowThreadProcessId(tray.hwnd,&dwProcessId);

    tifo.sProcessPath = GetFilenameFromPid(dwProcessId);        

    wchar_t TipChar;
    wchar_t sTip[1024] = {0};
    wchar_t* pTip = (wchar_t*)tb.iString;        

    if(!(tb.fsState&TBSTATE_HIDDEN))
    {            
        int x = 0;
        do 
        {    
            if(x == 1023)
            {
                wcscpy(sTip,L"[ToolTip was either too long or not set]");    
                break;
            }
            data.ReadData<wchar_t>(&TipChar, (LPCVOID)pTip++); 
        }while(sTip[x++] = TipChar);
    }
    else
        wcscpy(sTip,L"[Hidden Icon]");                

    USES_CONVERSION;
    tifo.sTip = W2T(sTip);

    tifo.hwnd = tray.hwnd;
    tifo.uCallbackMessage = tray.uCallbackMessage;
    tifo.uID = tray.uID;

    tifo.bVisible = !(tb.fsState & TBSTATE_HIDDEN);

    int iconindex = 0;
    ICONINFO  iinfo;
    if(GetIconInfo(tray.hIcon,&iinfo) != 0)
    {            
        iconindex = m_Image16List.Add(tray.hIcon);
    }

For the rest of the code, see the included source code zip.

Thanks

History

  • June 21, 2005 : Began work on the app.
  • June 27, 2005 : Published on The Code Project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nish Sivakumar
United States United States
Member
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
 
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.
 
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
 
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
 
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionCan we hide a particular icon in the system tray? [modified]memberKishore_Vuppala4 Feb '09 - 17:54 
I need to hide or unhide certain bluetooth applications' icons that are there in system tray based on whether bluetooth is on/off. Can we hide/unhide the system tray icons? It's an urgent requirement!!
 
modified on Thursday, February 5, 2009 1:12 AM

Newsabout "GetIconInfo"memberoneg6615 Jan '09 - 5:26 
why D'Oh! | :doh: the "GetIconInfo" return ZERO in some system's Trayicons that they is exist?
Generalnotesmemberjo0ls16 Dec '08 - 1:50 
Looks like TRAYDATA is NOTIFYICONDATA without the cbsize field.
GETMODULEFILENAMEEX is an easier way to get the normal path from the device path.
Still works in Vista.
I'm doing it with managed code, but the TBBUTTON size varies between x64 and x86 - which is a pain as I can't see a way to lay it out that works with both.
Questionclassmembergq_the_fallen_angel16 Jun '08 - 6:49 
Hello,
 
I'd like to use this code in vb.net but have now idea what to get with it. Unfortunately i have no experience with C++ so if anyone could create a dll from this to be able to use these functions (to get the icons and manipulate them) from vb.net would be really appreciated.
 
Anyways, just needed to delete the windows version checking and works on Vista, too - i tried it.
 
Thanks;
GQ
Questionhide a single tray icon from system traymemberJayapal Chandran15 Mar '08 - 0:27 
hi, i need to hide a particular tray icon from the system tray instead of using the reg key NoTrayItemsDisplay. i am with win32 api C style...
 
Today's Beautiful Moments are
Tomorrow's Beautiful Memories

GeneralSome icons' handle of tray buttions are invalidmembergshine61012 Aug '07 - 20:06 
Some icons' handle of tray buttions are invalid ?
 
When I ran the binary file(ShellTrayInfo.exe), I found that some tray buttons' icon were incorrect, such as MSN(Windows Live Messenger),Kaspersky Antivirus, etc.
 
After I debuged those codes, I found that some icons' handle of tray buttions are invalid.
In those cases, we will fail to call the GetIconInfo API and the error code got by GetLastError is 1402.
 
Is there any difference in those applications?
How can I solve this problem?
 
Hope to receive your reply soon.
 
Thanks in advance!
 

 
Kevin.
GeneralWorks on VistamemberThomassen16 Jun '07 - 0:31 
This app seem to work on Vista. I had to put it in compatibility mode to bypass the version check, but I was able to reposition icons etc.
 
Maybe make the version check forward compatible? Only disallowing older OS's which is known not to work?
QuestionAnyone know of a similar program that just lists the systray icons?memberbadbob00111 May '07 - 6:36 
I'm looking for a commandline utility to just list the icons and tooltips in the system tray and spit it out to a file or stdout so I can parse it. Before I try to tackle modifying this app's code to do what I want, is there already something like what I'm looking for?
 
Thanks!
AnswerRe: Anyone know of a similar program that just lists the systray icons?memberlerognon21 Feb '09 - 9:23 
Try http://exodusdev.com/products/windows-system-tray-scan-utility[^]
GeneralJust what I needed!mvpHans Dietrich9 Apr '07 - 1:14 
After last month's disastrous HD crash, I wanted to start keeping tabs on the temperatures for both of my HD drives. I use HDD Thermometer, a great free tool that shows the temps (one for each HDD) in the tray. Unfortunately, at startup the temps sometimes do not appear next to each other. Your utility is the perfect answer!
 
I agree with 5h17h34d - having Shell Tray Info run at startup would be very nice.
 
Thanks!
 
Best wishes,
Hans
GeneralRe: Just what I needed!mvpNishant Sivakumar9 Apr '07 - 1:54 
I've been meaning to update the source to VC++ 2005, and I guess when I do that I'll also add an installer that'll add this to the startup. Though I could avoid the installer and add an option within the app that'll allow people to specify if they want it to run on startup - it's always good to avoid installers.
 
Regards,
Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
C++/CLI in Action (*E-Book is out, Print version April 6th*)
Fly on your way like an eagle
Fly as high as the sun
On your wings like an eagle
Fly and touch the sun

GeneralRe: Just what I needed!mvpHans Dietrich9 Apr '07 - 2:00 
Nishant Sivakumar wrote:
I could avoid the installer and add an option within the app that'll allow people to specify if they want it to run on startup

 
I agree - an option within the app would be better.

QuestionHowto make ShellTrayInfo work automatically at boot?member5h17h34d13 Feb '07 - 17:19 
Like this little app except the fact that I must redo it after
every boot.
 
Perhaps I am missing something obvious?
 
Thank you for this little gem of a program for a utility
junky like me!
 
SH
AnswerRe: Howto make ShellTrayInfo work automatically at boot?memberS.H.Bouwhuis18 Jun '07 - 13:14 
My thoughts exactly!
Actually, a simple 'auto alphabetic sort' every 5 minutes or so would be enough.
 
Since the source code is supplied, it should be an easy thing.
I'm currently too busy/lazy to do this, but if there are people out there who REALLY want this, I'll consider it (PM me with request).
GeneralCompiling Error Helpmemberswarup9 Dec '06 - 0:28 
hi guys i am getting some errors while Compiling, can anyone help me out,
 
the errors are
 
Cerror C2552: 'tifo' : non-aggregates cannot be initialized with initializer list
Cerror C2275: 'TRAYDATA' : illegal use of this type as an expression
error C2275: 'wchar_t' : illegal use of this type as an expression
 
these 3 can be solved by using atlbase.h but what about the rest
 
error C2065: 'USES_CONVERSION' : undeclared identifier
error C2065: 'W2T' : undeclared identifier
error C2593: 'operator =' is ambiguous
 
ya one more thing
GetProcessImageFileName is it in psapi, then which is the correct version and can somepne post the 3 files, psapi.h psapi.lib n psapi.dll
because i m getting the error if i comment all the pervious errors
unresolved external symbol _GetProcessImageFileNameW@12
 
Thanks a lot
Swarup
Questionhow to make Static Executeable/portable executeablemembermurtazadhari11 Nov '06 - 3:19 
i want to know how i can make static executable file.
 
Murtaza Tahir Ali Dhari
Generali want to refer code written by MFC(Visual C++6.0) about "Programmable Calculator"memberamatuer_vn0315 Sep '06 - 16:52 
please help me some code this "program stilmulator Calculator same Windows"
 
i like design Web

QuestionDid you ever find out why some icons dont appear?memberplehxp6 Sep '06 - 1:20 
Did you ever find out why some icons (MSN Messenger for example) do not draw properly?
 
I am having the same problem in a similar project and i dont know why some icons dont return a valid hIcon.
QuestionNon-MFCmemberspamna19 Aug '06 - 6:21 
I am attempting to write a function to determine if my tray icon is still present. It irritates me greatly when explorer crashes and drops my icon and I feel it is a poor solution to simply delete and re-add the icon periodically as has been suggested to me. I am implementing this in a non-MFC application, so I am afraid I cannot use the CProcessData class you have used in your example. I was wondering if you could explain how to obtain the specific TRAYDATA object for each icon without using the CProcessData class. Here is my code:
 
bool IsTrayIconPresent(UINT uid) {
TBBUTTON tb = {0};
TRAYDATA td = {0};
HWND hWnd = ::FindWindow("Shell_TrayWnd", NULL);
if(hWnd) {
hWnd = ::FindWindowEx(hWnd, NULL, "TrayNotifyWnd", NULL);
if(hWnd) {
hWnd = ::FindWindowEx(hWnd, NULL, "SysPager", NULL);
if(hWnd) {
hWnd = ::FindWindowEx(hWnd, NULL, "ToolbarWindow32", NULL);
}
}
}
 
if(!hWnd) //error could not find Toolbar
return false;
 
int count = (int)::SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);
for(int i=0;i
AnswerRe: Non-MFCstaffNishant Sivakumar19 Aug '06 - 6:28 
spamna wrote:
I am implementing this in a non-MFC application, so I am afraid I cannot use the CProcessData class you have used in your example.

 
CProcessData is not MFC dependent. You can use it for non-MFC projects too.
 
Regards,
Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications.
Also visit the Ultimate Toolbox blog (New)

GeneralRe: Non-MFCmemberspamna19 Aug '06 - 10:39 
Thank you for your quick reply. I saw your class name was prefixed with "C" and assumed it was MFC. Here is the resulting code in case anyone else wanted a simple and quick snippet to determine if their tray icon was still in the tray:
 
//determines if the icon with given uid is present in the tray
bool IsTrayIconPresent(UINT uid) {
register int i;
int count;
TBBUTTON tb = {0};
TRAYDATA td = {0};
DWORD dwTrayPid = 0;
HANDLE hTrayProc = 0;
LPVOID lpData = 0;
 
HWND hWnd = ::FindWindow("Shell_TrayWnd", NULL);
if(hWnd) {
hWnd = ::FindWindowEx(hWnd, NULL, "TrayNotifyWnd", NULL);
if(hWnd) {
hWnd = ::FindWindowEx(hWnd, NULL, "SysPager", NULL);
if(hWnd) {
hWnd = ::FindWindowEx(hWnd, NULL, "ToolbarWindow32", NULL);
}
}
}
 
if(!hWnd) //error could not find Toolbar
return false;
 
//get the pid
if(GetWindowThreadProcessId(hWnd, &dwTrayPid) == NULL)
return false;
 
//open the corrent process
if((hTrayProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwTrayPid)) == NULL)
return false;
 
//virtualalloc
if((lpData = VirtualAllocEx(hTrayProc, NULL, sizeof(TBBUTTON), MEM_COMMIT, PAGE_READWRITE)) == NULL)
goto cleanup;
 
count = (int)::SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);
for(i=0;i
 
Thanks for this excellent article. Now I have to figure out how to manipulate buttons on the taskbar = P
 
Regards,
Nate
AnswerRe: Non-MFCmemberKelesis7728 Sep '06 - 7:04 
Hello.
You do not need to periodically query the Tray area to know if your icon is still present. There's a better way.
 
Every time explorer.exe crashes, it destroys and recreates the Taskbar.
As soon as the Taskbar window has been fully rebuilt, the System broadcasts a special message to all the applications that have requested to receive the notification.
 
The message is:
"TaskbarCreated" (<-- case sensitive)
 
and you request the notification simply by calling:
UINT iMyMsg = RegisterWindowMessage ("TaskbarCreated");
 
where iMyMsg is your own variable that receives the Sistem-wide value uniquely identifiyng the "TaskbarCreated" message.
Then, inside your WndProc() you handle this message like any other.
For example:
 
LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (Msg == iMyMsg)
{
// The Windows Taskbar has just been rebuilt.
 
// Do your stuff here (like adding a new tray icon).
.
.
.
}
 
// Other WM_*** messages, as usual.
switch (Msg)
{
case ...
}
 
return DefWindowProc (hWnd, Msg, wParam, lParam);
}

 
Being the Taskbar brand new, there's no old tray icon to remove.
Simply add a new one.
 
A few notes:
You need to request the "TaskbarCreated" message notification only once.
Requesting it multiple times is useless, and always returns the same identifier.
If different apps request the notification, they all get returned the same identifier.
There's no way (and no mean) to unregister the message once your app terminates. So don't worry about it.
 
Hope it helps.
 
Regards,
Ciao ciao Smile | :)
 
[edit:]
I forgot to mention that the "TaskbarCreated" message hasn't changed since Win98. I don't know about Win95.
 
Ciao ciao Smile | :)
QuestionRe: Non-MFCmemberdfhgesart8 Jul '07 - 12:04 
Nishant Sivakumar wrote:
CProcessData is not MFC dependent. You can use it for non-MFC projects too.

 
How so?
GeneralGetIconInfo(tray.hIcon,&amp;iinfo) -&gt; You have forgotten to delete the bitmapsmembermichele_cv5 Sep '05 - 1:30 
Hi,
 
GetIconInfo creates bitmaps for the hbmMask and hbmColor members of ICONINFO. The calling application must manage these bitmaps and delete them when they are no longer necessary.
 
//---- old code ---
//
int iconindex = 0;
ICONINFO iinfo;
if(GetIconInfo(tray.hIcon,&iinfo) != 0)
{
iconindex = m_Image16List.Add(tray.hIcon);
}
 
//--- new code ----
//
int iconindex = 0;
ICONINFO iinfo;
if(GetIconInfo(tray.hIcon,&iinfo) != 0)
{
iconindex = m_Image16List.Add(tray.hIcon);
 
if (iinfo.hbmMask != NULL)
DeleteObject(iinfo.hbmMask);
if (iinfo.hbmColor != NULL)
DeleteObject(iinfo.hbmColor);
}
 
Cheers,
michi

Generaldo not work under WinXPmemberCountry Man2 Aug '05 - 21:04 
after starting the binary direct this error message will shown:
 
GetProcessImageFileNameA not in PSAPI.DLL
 
I use WinXP (without any servicepack) german edition
GeneralRe: do not work under WinXPstaffNishant Sivakumar19 Aug '06 - 6:27 
Country Man wrote:
GetProcessImageFileNameA not in PSAPI.DLL
 
I use WinXP (without any servicepack) german edition

 
You'd need the Unicode build.
 
Regards,
Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications.
Also visit the Ultimate Toolbox blog (New)

Questionhow to hide/unhide icons in the systray?memberjojo291185 Jul '05 - 8:48 
Hi,
 
First, excuse my bad english, it's not my native language (I'm French).
Then, is it possible, starting with your code, to create a function which could hide/unhide any process' icon in the system tray too (not only in the taskbar)???
How must I do?
 
Regards.

AnswerRe: how to hide/unhide icons in the systray?memberswarup4 Jan '07 - 5:55 
NOTIFYICONDATA tbc;
tbc.cbSize=sizeof(NOTIFYICONDATA);
tbc.hWnd=m_TifoVec[index].hwnd;
tbc.uID=m_TifoVec[index].uID;
tbc.uCallbackMessage=m_TifoVec[index].uCallbackMessage;
tbc.uFlags=NIF_STATE;
tbc.dwState=NIS_HIDDEN;
tbc.dwStateMask=NIS_HIDDEN ;
Shell_NotifyIcon(NIM_DELETE ,&tbc);
 

this is one code another is
::SendMessage(hwnd, TB_DELETEBUTTON, index, 0);
 
try let me know.
GeneralExcellent!memberPaul S. Vickery27 Jun '05 - 6:05 
This is excellent - with this and my Taskbar Sorter[^], I can spend hours rearranging everything to be just the way I like it!
 

"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
GeneralRe: Excellent!staffNishant Sivakumar3 Jul '05 - 20:01 
Thanks Paul Smile | :)
GeneralNice workmemberShog927 Jun '05 - 5:17 
Can't say it's something i've wanted to do too often, but always nice to know it *can* be done. Smile | :)
 
You must be careful in the forest
Broken glass and rusty nails
If you're to bring back something for us
I have bullets for sale...

GeneralRe: Nice workstaffNishant Sivakumar3 Jul '05 - 20:00 
Thanks Shog Smile | :)
QuestionWhat about other OSmemberKarstenK26 Jun '05 - 21:57 
If there were some tips to older W9x Systems, it could have been execellent.Sniff | :^)
 
Try this @ home. (B&B)
AnswerRe: What about other OSstaffNishant Sivakumar27 Jun '05 - 0:23 
I am sorry about that - I don't know how this is implemented internally in pre-XP OSes. Frown | :-(
GeneralFAST, VERY FASTmemberThatsAlok26 Jun '05 - 20:20 
Kool Source Code,I too trying to make same thing from last sunday, Now I will use your Source Code Smile | :) Laugh | :laugh: .
 

"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

cheers,
Alok Gupta
GeneralRe: FAST, VERY FASTstaffNishant Sivakumar26 Jun '05 - 20:26 
ThatsAlok wrote:
Kool Source Code,I too trying to make same thing from last sunday, Now I will use your Source Code
 
Glad to hear that Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 27 Jun 2005
Article Copyright 2005 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid