0

I'm coding an MFC/C++ app that places several icons on the system tray. (The number of icons is controlled by a user and can reach up to 10 or so if the user wishes to do so -- each per certain function of the app.)

My question is, can I control the order at which those icons are placed on the tray?

What happens now is that when I call

Shell_NotifyIcon(NIM_ADD, &nid);

in a certain order, the order at which icons appear on the actual tray is different.

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • 4
    I would love to be proven wrong, but I don't think this is possible. Why do you want to add 10 icons in the first place? Doesn't a menu or something make more sense? – Thomas May 21 '14 at 20:04
  • @Thomas: OK. That evidently answers my question. Thank you. – c00000fd May 21 '14 at 21:40
  • 1
    You do not have control over the order in which the icons appear. – Raymond Chen May 21 '14 at 22:15
  • Just learned something. It turns out a user can drag tray icons with the mouse to change their order. Works on Windows 7 and on. – c00000fd May 24 '14 at 06:59

1 Answers1

0

The task tray is just a ToolBar32 control. You can get a handle to it with something like this:

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

Then you should be able to call this:

::SendMessage(hWnd, TB_MOVEBUTTON, nFrom, nTo);
Jamal
  • 763
  • 7
  • 22
  • 32
  • Can I really send `TB_MOVEBUTTON` to a window that is not in my process? – c00000fd May 23 '14 at 01:10
  • 2
    You are relying on implementation details which can change at any time. – Raymond Chen May 23 '14 at 01:36
  • @c00000fd: yes, because you are only passing integer values directly in the message parameters, not pointers that are dependant on any particular process address space. – Remy Lebeau May 23 '14 at 02:58
  • @RaymondChen: Theoretically true, but it isn't going to change for any given version. Still the OP ought to test it on each OS and maybe make adjustments based on which version you're running on. For example, this won't be of much use if running Win8+ with the Metro Skin. – Rick Murtagh Jun 04 '14 at 21:50
  • Any Windows Update can change it. And it's not clear how the OP can test on Windows 10, seeing as it doesn't exist yet. – Raymond Chen Jun 04 '14 at 22:31