Click here to Skip to main content
6,292,426 members and growing! (10,988 online)
Email Password   helpLost your password?
Web Development » Applications & Tools » CodeProject Tools     Intermediate

Shell Tray Info - Arrange your system tray icons

By Nishant Sivakumar

A tool with full source code that enumerates tray icons and allows you to reposition them as well as send mouse messages.
VC7.1WinXPVS.NET2003, Dev
Posted:26 Jun 2005
Views:127,289
Bookmarked:54 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
30 votes for this article.
Popularity: 6.44 Rating: 4.36 out of 5
2 votes, 6.7%
1

2

3
2 votes, 6.7%
4
26 votes, 86.7%
5

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nishant Sivakumar


Member
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

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.
Location: United States United States

Other popular Applications & Tools articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 38 (Total in Forum: 38) (Refresh)FirstPrevNext
GeneralVista 32 and Vista 64 PinmemberChris Havelick4:25 10 Apr '09  
QuestionWhy right click menu are sometime sticky? Pinmemberlerognon10:26 21 Feb '09  
GeneralCan we hide a particular icon in the system tray? [modified] PinmemberKishore_Vuppala18:54 4 Feb '09  
Newsabout "GetIconInfo" Pinmemberoneg6616:26 5 Jan '09  
Generalnotes Pinmemberjo0ls2:50 16 Dec '08  
Questionclass Pinmembergq_the_fallen_angel7:49 16 Jun '08  
Questionhide a single tray icon from system tray PinmemberJayapal Chandran1:27 15 Mar '08  
GeneralSome icons' handle of tray buttions are invalid Pinmembergshine61021:06 12 Aug '07  
GeneralWorks on Vista PinmemberThomassen1:31 16 Jun '07  
QuestionAnyone know of a similar program that just lists the systray icons? Pinmemberbadbob0017:36 11 May '07  
AnswerRe: Anyone know of a similar program that just lists the systray icons? Pinmemberlerognon10:23 21 Feb '09  
GeneralJust what I needed! PinmvpHans Dietrich2:14 9 Apr '07  
GeneralRe: Just what I needed! PinmvpNishant Sivakumar2:54 9 Apr '07  
GeneralRe: Just what I needed! PinmvpHans Dietrich3:00 9 Apr '07  
GeneralHowto make ShellTrayInfo work automatically at boot? Pinmember5h17h34d18:19 13 Feb '07  
GeneralRe: Howto make ShellTrayInfo work automatically at boot? PinmemberS.H.Bouwhuis14:14 18 Jun '07  
GeneralCompiling Error Help Pinmemberswarup1:28 9 Dec '06  
Generalhow to make Static Executeable/portable executeable Pinmembermurtazadhari4:19 11 Nov '06  
Generali want to refer code written by MFC(Visual C++6.0) about "Programmable Calculator" Pinmemberamatuer_vn0317:52 15 Sep '06  
GeneralDid you ever find out why some icons dont appear? Pinmemberplehxp2:20 6 Sep '06  
QuestionNon-MFC Pinmemberspamna7:21 19 Aug '06  
AnswerRe: Non-MFC PinstaffNishant Sivakumar7:28 19 Aug '06  
GeneralRe: Non-MFC Pinmemberspamna11:39 19 Aug '06  
AnswerRe: Non-MFC PinmemberKelesis778:04 28 Sep '06  
QuestionRe: Non-MFC Pinmemberdfhgesart13:04 8 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Jun 2005
Editor: Nishant Sivakumar
Copyright 2005 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project