1

My C# Windows application accepts drag operations, and I'd like to be able to identify the hWnd and/or Process ID of the source. Is this possible, and how would I go about determining it?

I've tried getting the active window when the drag-over commences, but if the user has activated other windows during the drag operation (for example, by pressing Alt+Tab one or more times), that isn't very reliable.

I've also tried inspecting the formats on the IDataObject, but that signature isn't enough to reliably identify the source window.

ThunderFrame
  • 9,352
  • 2
  • 29
  • 60
  • 1
    I don't think there's an official way to do this (although the OS would obviously know which thread is currently in the `DoDragDrop()` loop, I don't remember ever seeing a function to query this). – Jonathan Potter Nov 28 '16 at 01:34
  • 1
    Unless you manually hook `DoDragDrop()` in every running process, there is no other way to discover the source. *This is by design!* The drag&drop framework is abstracted to intentionally hide those details from the target. Even if you could identify the source, *why* do you need that information? What do you intend to do with it? – Remy Lebeau Nov 28 '16 at 20:25
  • Thanks @RemyLebeau - I thought that might be the case. I'm trying to get some context about the dragged item(s), and while I have been able to use IAccessible to get that detail when only a single instance of the source application is open, it isn't always reliable if there are multiple instances of the source application. – ThunderFrame Nov 29 '16 at 00:46
  • @ThunderFrame: "*I'm trying to get some context about the dragged item(s)*" - but for what purpose? Since you can't discover the source, all you have to go on is the content of the dragged `IDataObject`. – Remy Lebeau Nov 29 '16 at 00:53
  • @RemyLebeau Some items I'm dragging have enough detail in iDataObject, but for others, I'd ideally have a little extra context that only the source can provide. If I can get the source using IAccessible, I can usually get that detail. I'm just trying to cover the edge-case of not being able to determine which IAccessible object is the correct one. – ThunderFrame Nov 29 '16 at 01:09
  • @ThunderFrame: if the source *does not want* you to have that context, you can't reliably get it, period. You must act on the data you are actually given. If it is not detailed enough for your needs, find a different solution that does not involve drag&drop. – Remy Lebeau Nov 29 '16 at 01:45

0 Answers0