11

I have a very simple method that writes a file locally but I only want to fire it if the user looks like they are going to drop outside of the app because firing it every time they start dragging would result in lots of unnecessary files being written.

So my question is: if a user drags something within the app outside of the app, is it possible to detect when they drag over a valid drop target (e.g. the desktop or windows explorer)?

EDIT: At a more general level, my question is: how can I respond to mouse/drag events that occur outside of my app?

MoonBoots89
  • 355
  • 2
  • 16
  • As i understand you want to catch mouse events outside of your app? Isn't it? – Narek Sep 09 '14 at 09:34
  • @Narek That would be great. Anything that would allow me to run code in response to a users drag actions outside of the app would be very helpful, especially if I can receive information regarding what the cursor is currently over. – MoonBoots89 Sep 09 '14 at 10:16
  • Maybe this article helps you [link](http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C) – Narek Sep 09 '14 at 11:20
  • I'm not too familiar with wpf, but can't you listen on the mouse drag event and then determine if the mouse is outside of the boundaries of your main window? Once it crosses outside, write your file and set a boolean flag so you don't do it again. – Philip Pittle Sep 09 '14 at 12:42

3 Answers3

3

Not entirely sure what it is you're exactly trying to achieve, but this may help:

WPF: Drag and drop virtual files into Windows explorer

For the most part the drag / drop events should fire regardless of where you're dropping to (I think), but you can certainly be notified when a drop has been performed.

As @Quarzy stated, unless you're in communication with the other app, there may be no direct way of testing for data that the underlying windows drag / drop system doesn't expose.

More specifically that question points to this article: http://blogs.msdn.com/b/delay/archive/2009/11/04/creating-something-from-nothing-asynchronously-developer-friendly-virtual-file-implementation-for-net-improved.aspx

I post this purely because I wonder if maybe it may lead to other things, apart from that you might be able to get the Hwnd of the control under the cursor - possibly http://social.msdn.microsoft.com/Forums/windows/en-US/3df9fc84-4020-4ae1-8b4f-942bce65568f/find-the-object-under-the-mouse?forum=winforms as a starting point.

There may then be a way to query whether that particular control is a valid drop target through interop as well.

Good luck!

Community
  • 1
  • 1
Clint
  • 6,133
  • 2
  • 27
  • 48
3

This might be a possible answer for your question: Register a global hook to detect whether mouse dragging files/text

How ever the following suggestion might help (Require you to create c++ external lib):

  1. Capture other possible processes window message (Global hook WH_GETMESSAGE) (See this link How to Create a global WH_GETMESSAGE HOOK without DLL)
  2. Listen for WM_DROPFILES

see the following link: http://www.experts-exchange.com/Programming/Languages/CPP/Q_10203575.html

Community
  • 1
  • 1
Ahmad Al Sayyed
  • 596
  • 5
  • 13
2

You have also an helping answer here How can I capture mouse events that occur outside of a (WPF) window?

But I do not see any way to detect if the target is valid, as long as you have no communication with the potential targets.

Community
  • 1
  • 1
Ouarzy
  • 3,015
  • 1
  • 16
  • 19