Click here to Skip to main content
Click here to Skip to main content
Go to top

Shell Tray Info - Arrange your system tray icons

, 26 Jun 2005
Rate this:
Please Sign up or sign in to vote.
A tool with full source code that enumerates tray icons and allows you to reposition them as well as send mouse messages.


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.


  • 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);
        hWnd = ::FindWindowEx(hWnd,NULL,_T("TrayNotifyWnd"), NULL);
            hWnd = ::FindWindowEx(hWnd,NULL,_T("SysPager"), NULL);
                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) :-

    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 Frown | :-(

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());        

    DWORD dwProcessId = 0;

    tifo.sProcessPath = GetFilenameFromPid(dwProcessId);        

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

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

    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.



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


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

Comments and Discussions

QuestionC # version? PinmemberMember 914268823-Nov-12 5:06 
SuggestionMaking it work on WoW64 [modified] Pinmembermklencke12-Aug-11 4:04 
Here's some code to get the process IDs of icons from your 32-bit application running on a 64-bit OS. You can actually read memory of a 64-bit process from a 32-bit process, as long as the addresses are below the 4GB barrier.
BOOL IsWow64()
	static bool isset = false;
    static BOOL bIsWow64 = FALSE;
	if (isset) {
		return bIsWow64;
    //IsWow64Process is not available on all supported versions of Windows.
    //Use GetModuleHandle to get a handle to the DLL that contains the function
    //and GetProcAddress to get a pointer to the function if available.

    LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
    if(NULL != fnIsWow64Process)
        if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
            //TODO handle error?
			return FALSE;
	isset = true;
    return bIsWow64;
typedef struct _TBBUTTON64 {
    int iBitmap;
    int idCommand;
    BYTE fsState;
    BYTE fsStyle;
    BYTE bReserved[6];
	DWORD64 dwData;
    DWORD64 iString;
typedef const TBBUTTON64 *LPCTBBUTTON64;
bool EnumSystemTray() {	
	bool bFound = false;
	// find system tray window
	HWND trayWnd = FindWindow(_T("Shell_TrayWnd"), NULL);
	if (trayWnd) {
		trayWnd = FindWindowEx(trayWnd, NULL,_T("TrayNotifyWnd"), NULL);
		if (trayWnd) {
			trayWnd = FindWindowEx(trayWnd, NULL,_T("SysPager"), NULL);
			if (trayWnd) {				
				trayWnd = FindWindowEx(trayWnd, NULL,_T("ToolbarWindow32"), NULL);
				bFound = true;
	DWORD dwTrayPid;
	GetWindowThreadProcessId(trayWnd, &dwTrayPid);
	int count = (int) SendMessage(trayWnd, TB_BUTTONCOUNT, 0, 0);
	HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwTrayPid);
	if (!hProcess) {
		return true;
	BOOL bIsWow64 = IsWow64();
	SIZE_T dwSize = bIsWow64 ? sizeof(TBBUTTON64) : sizeof(TBBUTTON);
	LPVOID lpData = VirtualAllocEx(hProcess, NULL, dwSize, MEM_COMMIT, PAGE_READWRITE);
	if (!lpData) {
		return true;
	// Loop through all systray icons
	for (int i = 0; i < count; i++) {
		HWND hwnd32;
		SendMessage(trayWnd, TB_GETBUTTON, i, (LPARAM)lpData);
		if ( bIsWow64 ) {
			// Try to read memory from 64-bit Explorer process. Hope the address of the traybar data is below 4GB

			TBBUTTON64 tbb;
			if (!ReadProcessMemory(hProcess, lpData, (LPVOID)&tbb, sizeof(TBBUTTON64), NULL)) {
			DWORD64 hwnd;
			// First member of TRAYDATA structure is HWND, so we can just use the address of the struct to read the member
			if (!ReadProcessMemory(hProcess, (LPCVOID)tbb.dwData, (LPVOID)&hwnd, sizeof(DWORD64), NULL)) {
			// Hope this does not get truncated, but we shouldn't have that many windows
			hwnd32 = (HWND)hwnd;
		} else {
			TBBUTTON tbb;
			if (!ReadProcessMemory(hProcess, lpData, (LPVOID)&tbb, sizeof(TBBUTTON), NULL)) {
			DWORD32 hwnd;
			// First member of TRAYDATA structure is HWND, so we can just use the address of the struct to read the member
			if (!ReadProcessMemory(hProcess, (LPCVOID)tbb.dwData, (LPVOID)&hwnd, sizeof(DWORD32), NULL)) {
			hwnd32 = (HWND)hwnd;
		DWORD dwProcessId = 0;
		GetWindowThreadProcessId(hwnd32, &dwProcessId);
		// XXX - DO SOMETHING WITH dwProcessId
	VirtualFreeEx(hProcess, lpData, NULL, MEM_RELEASE);
	return true;

modified on Monday, August 15, 2011 7:32 AM

GeneralMy vote of 5 Pinmemberswdev.bali16-Feb-11 17:31 
GeneralMy vote of 5 Pinmemberzhangfancc19-Jul-10 20:02 
GeneralNice demo app.. Pinmemberxsoftwerx15-Feb-10 16:40 
GeneralRe: Nice demo app.. Pinmemberfangyanxiang26-Apr-11 16:32 
GeneralWrong code and method Pinmemberkilt14-Sep-09 0:18 
GeneralVista 32 and Vista 64 PinmemberChris Havelick10-Apr-09 3:25 
QuestionWhy right click menu are sometime sticky? Pinmemberlerognon21-Feb-09 9:26 
AnswerRe: Why right click menu are sometime sticky? PinmemberAndreone19-Aug-09 3:49 
QuestionCan we hide a particular icon in the system tray? [modified] PinmemberKishore_Vuppala4-Feb-09 17:54 
Newsabout "GetIconInfo" Pinmemberoneg6615-Jan-09 5:26 
Generalnotes Pinmemberjo0ls16-Dec-08 1:50 
Questionclass Pinmembergq_the_fallen_angel16-Jun-08 6:49 
Questionhide a single tray icon from system tray PinmemberJayapal Chandran15-Mar-08 0:27 
GeneralSome icons' handle of tray buttions are invalid Pinmembergshine61012-Aug-07 20:06 
GeneralWorks on Vista PinmemberThomassen16-Jun-07 0:31 
QuestionAnyone know of a similar program that just lists the systray icons? Pinmemberbadbob00111-May-07 6:36 
AnswerRe: Anyone know of a similar program that just lists the systray icons? Pinmemberlerognon21-Feb-09 9:23 
GeneralJust what I needed! PinmvpHans Dietrich9-Apr-07 1:14 
GeneralRe: Just what I needed! PinmvpNishant Sivakumar9-Apr-07 1:54 
GeneralRe: Just what I needed! PinmvpHans Dietrich9-Apr-07 2:00 
QuestionHowto make ShellTrayInfo work automatically at boot? Pinmember5h17h34d13-Feb-07 17:19 
AnswerRe: Howto make ShellTrayInfo work automatically at boot? PinmemberS.H.Bouwhuis18-Jun-07 13:14 
GeneralCompiling Error Help Pinmemberswarup9-Dec-06 0:28 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 27 Jun 2005
Article Copyright 2005 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid