Пример #1
0
        /// <summary>
        /// Constructor of the FrameworkElement representation, which can be moved anywhere on the screen
        /// </summary>
        /// <param name="element">Element to create an avatar from</param>
        /// <param name="scale">Scale of the represented avatar compared to its source (default: 1.0: equal size)</param>
        /// <param name="opacity">Opacity of the represented avatar compared to its source (default: 1.0)</param>
        public FreeFrameworkElementAvatar(FrameworkElement element, double scale = 1.0, double opacity = 1.0)
        {
            if (element.GetType() == typeof(ContentPresenter))
            {
                element = WPF.FindFirstChild <FrameworkElement>(element);
            }

            // Create the image
            Image = new Image()
            {
                // Icon Image
                Style   = new Style(typeof(Image)),
                Source  = WPF.ProduceImageSourceForVisual(element),
                Opacity = opacity,
            };

            // Create the visual
            DragIcon = new Popup()
            {
                AllowsTransparency = true,
                PopupAnimation     = PopupAnimation.None,
                Placement          = PlacementMode.Absolute,
                Width  = element.ActualWidth * scale,
                Height = element.ActualHeight * scale,
                Child  = Image
            };
        }
Пример #2
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);
        }