Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C++
Article

Shell Tray Info - Arrange your system tray icons

Rate me:
Please Sign up or sign in to vote.
4.93/5 (40 votes)
26 Jun 2005CPOL3 min read 354.8K   10.3K   92   46
A tool with full source code that enumerates tray icons and allows you to reposition them as well as send mouse messages.

Image 1

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)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
Questionhow to make Static Executeable/portable executeable Pin
murtaza dhari11-Nov-06 3:19
murtaza dhari11-Nov-06 3:19 
Generali want to refer code written by MFC(Visual C++6.0) about "Programmable Calculator" Pin
amatuer_vn0315-Sep-06 16:52
amatuer_vn0315-Sep-06 16:52 
QuestionDid you ever find out why some icons dont appear? Pin
plehxp6-Sep-06 1:20
plehxp6-Sep-06 1:20 
QuestionNon-MFC Pin
NateD19-Aug-06 6:21
NateD19-Aug-06 6:21 
AnswerRe: Non-MFC Pin
Nish Nishant19-Aug-06 6:28
sitebuilderNish Nishant19-Aug-06 6:28 
GeneralRe: Non-MFC Pin
NateD19-Aug-06 10:39
NateD19-Aug-06 10:39 
AnswerRe: Non-MFC Pin
_Kel_28-Sep-06 7:04
_Kel_28-Sep-06 7:04 
QuestionRe: Non-MFC Pin
dfhgesart8-Jul-07 12:04
dfhgesart8-Jul-07 12:04 
GeneralGetIconInfo(tray.hIcon,&amp;iinfo) -&gt; You have forgotten to delete the bitmaps Pin
michele_cv5-Sep-05 1:30
michele_cv5-Sep-05 1:30 
Generaldo not work under WinXP Pin
Country Man2-Aug-05 21:04
Country Man2-Aug-05 21:04 
GeneralRe: do not work under WinXP Pin
Nish Nishant19-Aug-06 6:27
sitebuilderNish Nishant19-Aug-06 6:27 
Questionhow to hide/unhide icons in the systray? Pin
jojo291185-Jul-05 8:48
jojo291185-Jul-05 8:48 
AnswerRe: how to hide/unhide icons in the systray? Pin
swarup4-Jan-07 5:55
swarup4-Jan-07 5:55 
GeneralExcellent! Pin
Paul Vickery27-Jun-05 6:05
professionalPaul Vickery27-Jun-05 6:05 
GeneralRe: Excellent! Pin
Nish Nishant3-Jul-05 20:01
sitebuilderNish Nishant3-Jul-05 20:01 
GeneralNice work Pin
Shog927-Jun-05 5:17
sitebuilderShog927-Jun-05 5:17 
GeneralRe: Nice work Pin
Nish Nishant3-Jul-05 20:00
sitebuilderNish Nishant3-Jul-05 20:00 
QuestionWhat about other OS Pin
KarstenK26-Jun-05 21:57
mveKarstenK26-Jun-05 21:57 
AnswerRe: What about other OS Pin
Nish Nishant27-Jun-05 0:23
sitebuilderNish Nishant27-Jun-05 0:23 
GeneralFAST, VERY FAST Pin
ThatsAlok26-Jun-05 20:20
ThatsAlok26-Jun-05 20:20 
GeneralRe: FAST, VERY FAST Pin
Nish Nishant26-Jun-05 20:26
sitebuilderNish Nishant26-Jun-05 20:26 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.