Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Drag and Drop on a Tray Icon

2.80/5 (4 votes)
6 Aug 2007CPOL2 min read 1   891  
How to detect that an item was dropped on a specific tray icon
Screenshot - desktop.jpg

Introduction

If you ever wondered how to catch drag and drop on a tray icon, this article might help you to accomplish this. With the help of other CodeProject contributions, I was able to figure out most of the functionality. One thing I have not been able to get working is to successfully get the HDROP structure that usually ships with the WM_DROPFILES message. If anyone can help me here, it would be very great. Please post it here, thanks!

Background

So, how does one find the right window containing the tray icons? You can use Microsoft Spy++ that ships with Visual Studio. Look for a window called "Shell_TrayWnd" containing another window called "TrayNotifyWnd" which has another child window called "SysPager". The "SysPager" contains the actual window holding all icons called "ToolbarWindow32".

Screenshot - MSSpy__.jpg

C++
HWND FindTrayToolbarWindow()
{
    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);
            }
        }
    }
    return hWnd;
}

After the window is found, you need to figure out the correct icon. This is done by using the POINT send with the WM_DROPFILES and looping through all icons, which are actually buttons. This way, one can figure out on which icon you dropped the files.

Using the Code

The code is split into 2 files. One EXE file that represents the tray icon app and a message hook DLL. The DLL installs a message hook to catch the WM_DROPFILES message and will check if we dropped anything on our tray icon. Then I send the message to the window that belongs to our tray icon app.

To run the code, I recommend using dbgview (in the zip file), before trying the app. I used it to catch the debug messages during the drag and drop operation.

Still as mentioned before, I am missing the part where I should get the a pointer to a HDROP structure from the WM_DROPFILES messages. Somehow the message doesn't contain that even though it should. If one could figure out what's wrong there, I would be quite happy.

References

History

  • 2007-08-07 Initial release

License

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