Пример #1
0
 static DragImage()
 {
     try
     {
         Helper = new DragDropHelper() as IDropTargetHelper;
     }
     catch (COMException)
     {
     }
 }
Пример #2
0
        public static bool GetIDropTargetHelper(out IntPtr helperPtr, out IDropTargetHelper dropHelper)
        {
            if (ShellAPI.CoCreateInstance(
                    ref ShellAPI.CLSID_DragDropHelper,
                    IntPtr.Zero,
                    ShellAPI.CLSCTX.INPROC_SERVER,
                    ref ShellAPI.IID_IDropTargetHelper,
                    out helperPtr) == ShellAPI.S_OK)
            {
                dropHelper =
                    (IDropTargetHelper)Marshal.GetTypedObjectForIUnknown(helperPtr, typeof(IDropTargetHelper));

                return true;
            }
            else
            {
                dropHelper = null;
                helperPtr = IntPtr.Zero;
                return false;
            }
        }
Пример #3
0
        private void dataGridView1_DragLeave(object sender, EventArgs e)
        {
            IDropTargetHelper dropHelper = (IDropTargetHelper) new DragDropHelper();

            dropHelper.DragLeave();
        }
Пример #4
0
        public static bool GetIDropTargetHelper(out IntPtr helperPtr, out IDropTargetHelper dropHelper)
        {
            if (Ole32.CoCreateInstance(ref ShellGuids.DragDropHelper, IntPtr.Zero, CLSCTX.InProcServer, ref ShellGuids.IDropTargetHelper, out helperPtr) == 0)
            {
                dropHelper = (IDropTargetHelper)Marshal.GetTypedObjectForIUnknown(helperPtr, typeof(IDropTargetHelper));

                return true;
            }
            else
            {
                dropHelper = null;
                helperPtr = IntPtr.Zero;
                return false;
            }
        }
Пример #5
0
        /// <summary>
        /// Notifies the DragDropHelper that the current Window received
        /// a Drop event.
        /// </summary>
        /// <param name="dropHelper">The DragDropHelper instance to notify.</param>
        /// <param name="data">The DataObject containing a drag image.</param>
        /// <param name="cursorOffset">The current cursor's offset relative to the window.</param>
        /// <param name="effect">The accepted drag drop effect.</param>
        public static void Drop(this IDropTargetHelper dropHelper, System.Windows.IDataObject data, Point cursorOffset, DragDropEffects effect)
        {
            Win32Point pt = cursorOffset.ToWin32Point();

            dropHelper.Drop((IDataObject)data, ref pt, (int)effect);
        }
Пример #6
0
        /// <summary>
        /// Notifies the DragDropHelper that the current Window received
        /// a DragOver event.
        /// </summary>
        /// <param name="dropHelper">The DragDropHelper instance to notify.</param>
        /// <param name="cursorOffset">The current cursor's offset relative to the window.</param>
        /// <param name="effect">The accepted drag drop effect.</param>
        public static void DragOver(this IDropTargetHelper dropHelper, Point cursorOffset, DragDropEffects effect)
        {
            Win32Point pt = cursorOffset.ToWin32Point();

            dropHelper.DragOver(ref pt, (int)effect);
        }
Пример #7
0
        // ----------------------------------------------------------------------------------------------
        #region Impelmentation


        /// <summary>
        /// Create or handle a system-style Drag & Drop icon
        /// </summary>
        /// <param name="element">Element to represent or where the Drag & Drop operation occurs</param>
        /// <param name="dataObject">Data Object dragged during Drag & Drop</param>
        /// <param name="effects">Drag & Drop effects</param>
        /// <param name="scale">Scale of the element representation when creating icon (1.0: unchanged)</param>
        /// <param name="create">create the icon representation from the element, otherwise tries to continue the existing Drag & Drop from the dataObject</param>
        public DragSystemIcon(FrameworkElement element, IDataObject dataObject, DragDropEffects effects, double scale, bool create)
        {
            // retrieve window handle
            var window = PresentationSource.FromVisual(element).RootVisual;
            var hwnd   = ((HwndSource)PresentationSource.FromVisual(window)).Handle;

            if (create)
            {
                Element = element;

                if (element is ContentPresenter)
                {
                    element = WPF.FindFirstChild <FrameworkElement>(element);
                }

                // Convert the visual element to a Bitmap
                var dpi       = 96 * scale;
                var renderbmp = WPF.ProduceImageSourceForVisual(element, dpi, dpi);

                // Retrieve renderbmp pixel array
                const int pixsize = 4;                 // Pbgra32 implies 4 bytes per pixels
                int       stride  = renderbmp.PixelWidth * pixsize;
                byte[]    pix     = new byte[renderbmp.PixelHeight * stride];
                renderbmp.CopyPixels(pix, stride, 0);

                for (int i = 3; i < pix.Length; i += pixsize)
                {
                    if (pix[i] == 0)
                    {
                        // Convert fully transparent pixels to Magenta (this will become transparent in ShDragImage)
                        pix[i - 0] = 0xFF;                         // A
                        pix[i - 1] = 0xFF;                         // R
                        pix[i - 2] = 0x00;                         // G
                        pix[i - 3] = 0xFF;                         // B
                    }
                    else if (pix[i] == 0xFF && pix[i - 1] == 0xFF && pix[i - 2] == 0x00 && pix[i - 3] == 0xFF)
                    {
                        // change Magenta pixels to *almost* magenta, to avoid changing these to transparent in ShDragImage
                        pix[i - 2] = 0x01;
                    }
                }

                // Convert pixel array to BitmapSource
                var bitmapsrc = BitmapSource.Create(renderbmp.PixelWidth, renderbmp.PixelHeight,
                                                    96, 96, PixelFormats.Bgra32, null, pix, stride);

                // Convert BitmapSource to Bitmap
                System.Drawing.Bitmap bitmap;
                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    var encoder = new BmpBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bitmapsrc));
                    encoder.Save(stream);
                    bitmap = new System.Drawing.Bitmap(stream);
                }

                //LimeMsg.Debug("ClipDragDrop DragSystemIcon: {0}", bitmap.GetPixel(0, 0));

                // Compute destination size
                WPF.Win32Size size;
                size.cx = (int)(renderbmp.PixelWidth * scale);
                size.cy = (int)(renderbmp.PixelHeight * scale);

                WPF.SysPoint wpt;
                wpt.x = size.cx / 2;
                wpt.y = size.cy / 2;

                ShDragImage shdi = new ShDragImage
                {
                    sizeDragImage = size,
                    ptOffset      = wpt,
                    hbmpDragImage = bitmap.GetHbitmap(),
                    crColorKey    = System.Drawing.Color.Magenta.ToArgb()
                };


                var sourceHelper = (IDragSourceHelper2) new DragDropHelper();
                sourceHelper.SetFlags(1);                 // Enable Drop description

                // Not quite right
                var com = new ComDataObject();
                dataObject = new DataObject(com);

                sourceHelper.InitializeFromBitmap(ref shdi, (System.Runtime.InteropServices.ComTypes.IDataObject)dataObject);
            }
            else
            {
                Element = null;
            }

            ComData = dataObject;
            Effects = effects;
            Scale   = scale;


            // Create the System Drag Helper and show it at the mouse location
            WPF.GetCursorPos(out WPF.SysPoint spos);
            DDHelper = (IDropTargetHelper) new DragDropHelper();
            DDHelper.DragEnter(hwnd,
                               (System.Runtime.InteropServices.ComTypes.IDataObject)dataObject,
                               ref spos, Effects);
        }