Click here to Skip to main content
Click here to Skip to main content

Drag and Drop on a Tray Icon

, 6 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
How to detect that an item was dropped on a specific tray icon
Screenshot - desktop.jpg


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!


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

HWND FindTrayToolbarWindow()
    HWND hWnd = FindWindow("Shell_TrayWnd", NULL);
        hWnd = FindWindowEx(hWnd,NULL,"TrayNotifyWnd", NULL);
            hWnd = FindWindowEx(hWnd,NULL,"SysPager", NULL);
                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.



  • 2007-08-07 Initial release


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


About the Author

Sven So.
Software Developer (Senior) biggest furniture dealer in the world
Germany Germany
No Biography provided

Comments and Discussions

Answerworking method of dragdrop onto a systray notification icon (without hooking) Pinmemberarnoudmulder21-Mar-11 14:53 
Questionquery dropped filenames? PinmemberIBM CH DevGroup11-Oct-08 0:31 
AnswerRe: query dropped filenames? PinmemberSven So.12-Oct-08 20:44 
GeneralRe: query dropped filenames? PinmemberBen Voigt26-Oct-10 17: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# PinmemberMember 301674124-May-08 13:55 

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 | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 7 Aug 2007
Article Copyright 2007 by Sven So.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid