Drag and drop files / text into tray icon (C #, WindowsForms)

I am trying to enable drag and drop on the tray icon in my application.

I know it is not possible to do this using the C # WindowsForms top-level APIs, since NotifyIcon does not support drag and drop events.

So, a little helping a more experienced friend from Windows, I decided to try this through the Win32 API. The idea was to register the hook in the tray window handler (after installing DragAcceptFiles(hWnd,TRUE); in the SysPager window handler).

The catching and dropping part works from the tray in the DLL.

 LRESULT CALLBACK myHookProc (int code, WPARAM wParam, LPARAM lParam){ if (code == HC_ACTION) { PMSG msg = (PMSG) lParam; switch(msg->message){ case WM_DROPFILES: ::MessageBox(NULL, L"Dropped files!", L"Test", MB_OK); // call my app registered hook break; } return CallNextHookEx(oldHookProc, code, wParam, lParam); } 

As expected, I get a message box.

The problem is that now I need to call a function in my C # application (WindowsForms) to notify about this event. Here, where I ran into a brick wall.

When I register a callback from my application in the DLL, I store it; but when myHookProc is myHookProc , the value is NULL.

Turns out I don't understand how DLLs work; there is no common instance between my application and the tray area (they are copied or each has its own โ€œinstanceโ€ if you could name it), so I canโ€™t use any static variables or something like that to store the opposite call back to my application.

Spent a couple of hours researching this, and the only solution seems to be shared memory (tried #pragma data_seg , which I came across on some forum, but to no avail), but it starts to feel too much excess for such a โ€œsimpleโ€ use case .

So, the million dollar questions:

  • Is it really necessary to disable DLL binding?
  • Do I need to resort to shared memory for this?
  • (Bonus question) WM_DROPFILES only works for files; how can i get the drop event fired for text?

Please keep in mind that this is my first snapshot with .NET, C # and Win32 (less than a week); detailed answers explaining why - and not just an expression - we will be very grateful!

Thanks.

+4
source share
1 answer

Yes, you really need to do these things because the window belongs to another process. Global intercepts require a DLL that can be entered. For full D + D support, RegisterDragDrop and COM code is required. Icky COM Code.

And no, you really shouldn't do this, because someone else might have the same idea as you. And first they sent him. The appcompat command in MSFT must have a nightmare with it. Caution with Raymond Chen, he got a bad temperament.

+2
source

All Articles