C#: How to fake a drag and drop action of a file to a certain drop-target of a window?

I don't believe there's any documented way You could send WM_DROPFILES but this (1) requires you to put the filename into an undocumented HDROP structure in the target process memory ahead of time, and (2) is the older method for accepting files, it won't work if the application uses the newer IDropTarget APIs IDropTarget is actually designed to be more general than just dragging files from explorer: there's a Win32 API DoDragDrop which allows an application to act as a drag source so the user can drop its data into other applications. Unfortunately DoDragDrop watches mouse movement directly, so you'd need to simulate mouse movement and button release in order to control where the item dropped. Even this won't work unless the other window is on top of the z-order, since DoDragDrop finds the window under the cursor, and will interfere with any mouse activity the user is doing.So it should be used as a last resort.

I don't believe there's any documented way. You could send WM_DROPFILES, but this (1) requires you to put the filename into an undocumented HDROP structure in the target process memory ahead of time, and (2) is the older method for accepting files, it won't work if the application uses the newer IDropTarget APIs. IDropTarget is actually designed to be more general than just dragging files from explorer: there's a Win32 API, DoDragDrop, which allows an application to act as a drag source so the user can drop its data into other applications.

Unfortunately DoDragDrop watches mouse movement directly, so you'd need to simulate mouse movement and button release in order to control where the item dropped. Even this won't work unless the other window is on top of the z-order, since DoDragDrop finds the window under the cursor, and will interfere with any mouse activity the user is doing. So it should be used as a last resort.

– Hedge Nov 22 '10 at 16:31 I looked it it up, the operations of this program use OLE. Does that mean I can use WM_DROPFILES? – Hedge Nov 22 '10 at 16:52 @Hedge: Likely not -- OLE IDropTarget replaces WM_DROPFILES.

Open the application in Dependency Walker and see whether it imports RegisterDragDrop (used with IDropTarget) or DragAcceptFiles (used with WM_DROPFILES). You could also use Spy++ and watch for the WM_DROPFILES message. – Ben Voigt Nov 22 '10 at 19:05 Spy++ can't find any messages at all (for the window of the app) when I watch for them.

I found RegisterDragDrop in Ole32. Dll marked with a blue 'C' (whatever that means). So I have no way to fake the drag and drop-operations?

– Hedge Nov 22 '10 at 8:11.

This tutorial will explain how to handle dragand drop events sent to one of the windows in your program by theWindows shell. In other words, this tutorial will teach you how toallow users to drag and drop files onto your form so you can do stuffwith them. This was bugging me for a little while, because it was sosimple in VB and for some reason it took me a while to figure it out inC#…anyway, on to the tutorial!

To start, create a new C# Windows Applicationproject called DragDropFiles. Put a ListBox control somewhere on theform. Set its AllowDrop property to "true".

Click the Events lightningbolt to get to the events of the ListBox. Now, to be able to handle files being dropped,at least two events need to be dealt with. The first is DragEnter,where basically all you have to do is allow the drag operation tocontinue.

If you don't allow the operation to continue, say, becausethe user is dragging text into the control, the mouse cursor will staya "NO" symbol instead of becoming the "Copy Here" symbol (the pointerwith a "+" next to it). The second event that must be handled(assuming they get past the DragEnter event) is DragDrop. This is whereyou actually read the files in the list and, in this application, addthem to a ListBox.

So, scroll down in the events list (make sureyou have the ListBox selected, not your form) until you find DragEnterand doubleclick it. // make sure they're actually dropping files (not text or anything else)if( e.Data. FileDrop, false) == true )// allow them to continue// (without this, the cursor stays a "NO" symbole.

Effect = DragDropEffects. The comments should explain the code. Next, weneed to actually handle the drop.

Get back to the graphical formediting interface and select the ListBox. Make sure you are viewing theEvents for the ListBox and scroll down to the DragDrop event anddoubleclick it. // transfer the filenames to a string array// (yes, everything to the left of the "=" can be put in the // foreach loop in place of "files", but this is easier to understand.)string files = (string)e.Data.

// loop through the string array, adding each filename to the ListBoxforeach( string file in files ){listBox1.Items. That was simple Go run theprogram and drag and drop some files (My Computer and the Recycle Bindon't count as files) onto the ListBox and see what happens. It willadd the filename with FULL PATH to the ListBox, so if you want just thefilename you'll need to parse it out (something I won't talk abouthere).

The example code shows this in action with everything done foryou, if you're that kind of person The example code alsodemonstrates how to grab the size of the file and put it into a label– something I was too lazy to explain here. Have fun, and happycoding!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions