Click here to Skip to main content
15,878,945 members
Articles / Programming Languages / C++

Drag and Drop on a Tray Icon

Rate me:
Please Sign up or sign in to vote.
2.80/5 (4 votes)
6 Aug 2007CPOL2 min read 53.8K   885   23   5
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)


Written By
Software Developer (Senior) a large company
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Answerworking method of dragdrop onto a systray notification icon (without hooking) Pin
arnoudmulder21-Mar-11 13:53
arnoudmulder21-Mar-11 13:53 
Questionquery dropped filenames? Pin
IBM CH DevGroup10-Oct-08 23:31
IBM CH DevGroup10-Oct-08 23:31 
AnswerRe: query dropped filenames? Pin
Sven So.12-Oct-08 19:44
Sven So.12-Oct-08 19:44 
GeneralRe: query dropped filenames? Pin
Ben Voigt26-Oct-10 16:25
Ben Voigt26-Oct-10 16:25 
Did you figure it out? It's almost certainly because the HDROP uses a pointer that's only valid in the process which originally got WM_DROPFILES. If you simply forward WM_DROPFILES using the same parameters, the pointer will be meaningless inside your application.

Instead, you might query the filenames inside your hook DLL and then use a mailslot to pass that information to your application.
GeneralC# Pin
Member 301674124-May-08 12:55
Member 301674124-May-08 12:55 

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.