views:

40

answers:

3

So I was playing around with drag and drop. I made a sample application and dropped a file from My Music onto my application. Here's what e.Data.GetFormats() returned:

  • Shell IDList Array
  • UsingDefaultDragImage
  • DragImageBits
  • DragContext
  • DragSourceHelperFlags
  • InShellDragLoop
  • FileDrop
  • FileNameW
  • FileName
  • IsShowingLayered
  • DragWindow
  • IsComputingImage
  • DropDescription
  • DisableDragText
  • ComputedDragImage
  • IsShowingText

Do you guys know what each of these mean and how to decode and use them? Googling each of them yields no useful information.

A: 

Not sure if you mean a WinForms app or a WPF app.

In any case, try having a look Here for a good article on how drag and drop works in win forms

And there's a very good MSDN Article which explains how the clipboard can be used in different scenarios

Basiclife
A: 

FileDrop is standard, covered by the DataFormats class. Shell IDList and FileName/W are common for Windows Explorer. The rest of them are all custom. By their names, it sounds like they enhance the D+D feedback when you drag to other Microsoft apps, like Media Player. This stuff is poorly documented, possibly intentional.

Hans Passant
A: 

DragImageBits describes the image that's displayed when you drag stuff. Its header is described by SHDRAGIMAGE.

var data = e.Data.GetData("DragImageBits") as MemoryStream;
var buffer = new byte[24];
data.Read(buffer, 0, 24);
int w = buffer[0] + (buffer[1] << 8) + (buffer[2] << 16) + (buffer[3] << 24);
int h = buffer[4] + (buffer[5] << 8) + (buffer[6] << 16) + (buffer[7] << 24);
// Stride accounts for any padding bytes at the end of each row. For 32 bit
// bitmaps there are none, so stride = width * size of each pixel in bytes.
int stride = width * 4;
// x and y is the relative position between the top left corner of the image and
// the mouse cursor.
int x = buffer[8] + (buffer[9] << 8) + (buffer[10] << 16) + (buffer[11] << 24);
int y = buffer[12] + (buffer[13] << 8) + (buffer[14] << 16) + (buffer[15] << 24);
buffer = new byte[stride * h];
// The image is stored upside down, so we flip it as we read it.
for (int i = (h - 1) * stride; i >= 0; i -= stride) data.Read(buffer, i, stride);
BitmapSource.Create(w, h, 96, 96, PixelFormats.Bgra32, null, buffer, stride);

One should still use DragDropHelper instead for better compatibility.

CannibalSmith