protected void CreateDragHelper( Drawing.Bitmap bitmap, Point startPoint, System.Windows.DataObject dataObject) { ShDragImage shdi = new ShDragImage(); Win32Size size; size.cx = bitmap.Width; size.cy = bitmap.Height; shdi.sizeDragImage = size; Win32Point wpt; wpt.x = (int)startPoint.X; wpt.y = (int)startPoint.Y; shdi.ptOffset = wpt; shdi.crColorKey = Drawing.Color.Magenta.ToArgb(); // This HBITMAP will be managed by the DragDropHelper // as soon as we pass it to InitializeFromBitmap. If we fail // to make the hand off, we'll delete it to prevent a mem leak. IntPtr hbmp = bitmap.GetHbitmap(); shdi.hbmpDragImage = hbmp; try { IDragSourceHelper sourceHelper = (IDragSourceHelper) new DragDropHelper(); try { sourceHelper.InitializeFromBitmap(ref shdi, (System.Runtime.InteropServices.ComTypes.IDataObject)dataObject); } catch (NotImplementedException ex) { throw new Exception("A NotImplementedException was caught. This could be because you forgot to construct your DataObject using a DragDropLib.DataObject", ex); } } catch { // We failed to initialize the drag image, so the DragDropHelper // won't be managing our memory. Release the HBITMAP we allocated. GDI32.DeleteObject(hbmp); } }
private void MFUMouseMove(object sender, MouseEventArgs e) { if (fMousePressed) { if (!fDragnDrop) { fDragnDrop = true; int Index = fMainForm.fIndexToDragFromMFU; if (Index >= 0 && Index < Items.Count) { fMainForm.fFromMFU = true; DoDragDrop(Index.ToString(), DragDropEffects.Copy); } #if ADVANCED_DRAG_AND_DROP Bitmap bmp = new Bitmap(100, 100, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.Magenta); using (Pen pen = new Pen(Color.Blue, 4f)) { g.DrawEllipse(pen, 20, 20, 60, 60); } } DataObject data = new DataObject(new DragDropLib.DataObject()); ShDragImage shdi = new ShDragImage(); Win32Size size; size.cx = bmp.Width; size.cy = bmp.Height; shdi.sizeDragImage = size; Point p = e.Location; Win32Point wpt; wpt.x = p.X; wpt.y = p.Y; shdi.ptOffset = wpt; shdi.hbmpDragImage = bmp.GetHbitmap(); shdi.crColorKey = Color.Magenta.ToArgb(); IDragSourceHelper sourceHelper = (IDragSourceHelper) new DragDropHelper(); sourceHelper.InitializeFromBitmap(ref shdi, data); DoDragDrop(data, DragDropEffects.Copy); #endif } } }
/// <summary> /// Sets the drag image. /// </summary> /// <param name="dataObject">The DataObject to set the drag image on.</param> /// <param name="image">The drag image.</param> /// <param name="cursorOffset">The location of the cursor relative to the image.</param> public static void SetDragImage(this IDataObject dataObject, Image image, System.Drawing.Point cursorOffset) { ShDragImage shdi = new ShDragImage(); Win32Size size; size.cx = image.Width; size.cy = image.Height; shdi.sizeDragImage = size; Win32Point wpt; wpt.x = cursorOffset.X; wpt.y = cursorOffset.Y; shdi.ptOffset = wpt; shdi.crColorKey = Color.Magenta.ToArgb(); // This HBITMAP will be managed by the DragDropHelper // as soon as we pass it to InitializeFromBitmap. If we fail // to make the hand off, we'll delete it to prevent a mem leak. IntPtr hbmp = GetHbitmapFromImage(image); shdi.hbmpDragImage = hbmp; try { IDragSourceHelper sourceHelper = (IDragSourceHelper) new DragDropHelper(); try { sourceHelper.InitializeFromBitmap(ref shdi, (ComIDataObject)dataObject); } catch (NotImplementedException ex) { throw new Exception("A NotImplementedException was caught. This could be because you forgot to construct your DataObject using a DragDropLib.DataObject", ex); } } catch { DeleteObject(hbmp); } }