Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do various clipboard/drag-and-drop formats mean?

I was playing around with drag and drop. I made a sample application and dropped a file from folder 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

What do each of these mean and how to decode and use them?

Googling each of them didn't yield any useful information.

like image 249
CannibalSmith Avatar asked Aug 13 '10 16:08

CannibalSmith


2 Answers

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.

like image 66
CannibalSmith Avatar answered Sep 22 '22 15:09

CannibalSmith


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.

like image 23
Hans Passant Avatar answered Sep 22 '22 15:09

Hans Passant