Minimal Drag'n'Drop application sample for Windows

Hello,

A little while ago, I began implementing dragging and dropping files from the Windows explorer to an application window for my current project.

After struggling to find the relevant information in the Microsoft documentation - including a broken link to a sample application - I decided to make my own application sample, so that the minimal knowledge to have for supporting dragging and dropping files in an application window is included in a short and easily buildable application:

https://github.com/alexGuntha/mindragndropwin

The most useful bit of info I found was here, under "Extracting the File Names from the Data Object":
https://docs.microsoft.com/en-us/windows/desktop/shell/datascenarios

If you have any issue with it, I'd like to have your feedback (is it really minimal? Isn't there a dirty thing still present in there?)

Personally I like drag'n'drop based tools, like Toycon, I think it's a good alternative to command-line only tools, and it got me started importing files in my current project without having to implement a fully-fledged file explorer :)

I hope it will help,

Have a good day :)

Edited by Guntha on
That... is not minimal.
If all you want is to get list of dropped file names, then you don't need to use IDropTarget and all COM & ole craziness.
You just need to process WM_DROPFILES message instead. Like this: https://gist.github.com/mmozeiko/5b1700c558d37cb2bb2f00328c2982f6
As a bonus, this is much easier to use in C, not C++, code. In C code inheriting COM is pain. IDropTarget comes from "newer" Windows API that allows you to customize drop action and other extras. But there's no reason to use it, if you don't need its features.

...I think it's a good alternative to command-line only tools...

While implementing drag&drop is perfectly fine and you should do that, drag&drop is not a good alternative to command-line. It is terrible alternative. If you are making application that accepts some input to process and gives some output (text, image, sound, code, whatever...) then it should have cli interface. Sooner or later you will want to automate your workflow and then you'll want to run application automatically - either on each build, on each file you download, or some automated testing, or just on remote server. Making this work with GUI only application is huge pain. Not impossible, but still - painful.

Another note - saying that you release your code in public domain and at the same time saying in source code that it is "copyrighted" by you is kind of contradiction. Public domain means that you want code to be free of any copy-rights. You still can say that you are the author, or this code is developed by you, but there should not be any copyright for public domain code.

Edited by Mārtiņš Možeiko on
Thanks for your feedback, that's what I wanted to know, I found it crazy that the only working way I found to handle drag'n'drop was to create and register a class dedicated to that ^^' Handling the WM_DROPFILES message is actually the first thing I tried but I couldn't make it work, showing that the Microsoft documentations are clearly lacking.

Also, I did not say drag'n'drop is a good REPLACEMENT but a good ALTERNATIVE to a command-line interface.
I think that this is a very good candidate for the wiki; at the very least referencing the links.

I also tried the WM_DROPFILES way and couldn't get it to work, but now I'll at least have something to look at to see what I was doing wrong. Thank you both!

mmozeiko
While implementing drag&drop is perfectly fine and you should do that, drag&drop is not a good alternative to command-line. It is terrible alternative. If you are making application that accepts some input to process and gives some output (text, image, sound, code, whatever...) then it should have cli interface. Sooner or later you will want to automate your workflow and then you'll want to run application automatically - either on each build, on each file you download, or some automated testing, or just on remote server. Making this work with GUI only application is huge pain. Not impossible, but still - painful.


In my mind, I always saw drag&drop as a user interface kind of thing. It's harder to implement(in terms of the existing documentation of Win32) than command-line arguments, but it's a nice thing to have if you're building something that is (non-savvy?) user oriented.