Exemplo n.º 1
0
 private void DragInitHandler(object sender, MouseButtonEventArgs e)
 {
     DragInitCommand.Execute(Figure);
 }
Exemplo n.º 2
0
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (Figure.Name != "Empty")
                {
                    DragInitCommand.Execute(Figure);
                    DataObject data = new DataObject();
                    data.SetData("ChessFigure", FigureProperty);
                    data.SetData("Object", this);

                    UIElement control = FigImage;
                    // convert FrameworkElement to PNG stream
                    var pngStream = new MemoryStream();
                    control.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    Rect rect = new Rect(0, 0, control.DesiredSize.Width, control.DesiredSize.Height);
                    RenderTargetBitmap rtb = new RenderTargetBitmap((int)control.DesiredSize.Width, (int)control.DesiredSize.Height, 96, 96, PixelFormats.Pbgra32);

                    control.Arrange(rect);
                    rtb.Render(control);

                    PngBitmapEncoder png = new PngBitmapEncoder();
                    png.Frames.Add(BitmapFrame.Create(rtb));
                    png.Save(pngStream);

                    #region Some png shit at the begining
                    var cursorStream = new MemoryStream();
                    cursorStream.Write(new byte[2] {
                        0x00, 0x00
                    }, 0, 2);
                    cursorStream.Write(new byte[2] {
                        0x02, 0x00
                    }, 0, 2);
                    cursorStream.Write(new byte[2] {
                        0x01, 0x00
                    }, 0, 2);
                    cursorStream.Write(new byte[1] {
                        (byte)control.DesiredSize.Width
                    }, 0, 1);
                    cursorStream.Write(new byte[1] {
                        (byte)control.DesiredSize.Height
                    }, 0, 1);
                    cursorStream.Write(new byte[1] {
                        0x00
                    }, 0, 1);
                    cursorStream.Write(new byte[1] {
                        0x00
                    }, 0, 1);
                    cursorStream.Write(new byte[2] {
                        (byte)32.0, 0x00
                    }, 0, 2);
                    cursorStream.Write(new byte[2] {
                        (byte)32.0, 0x00
                    }, 0, 2);
                    cursorStream.Write(new byte[4] {
                        (byte)((pngStream.Length & 0x000000FF)),
                        (byte)((pngStream.Length & 0x0000FF00) >> 0x08),
                        (byte)((pngStream.Length & 0x00FF0000) >> 0x10),
                        (byte)((pngStream.Length & 0xFF000000) >> 0x18)
                    }, 0, 4);
                    cursorStream.Write(new byte[4] {
                        (byte)0x16,
                        (byte)0x00,
                        (byte)0x00,
                        (byte)0x00,
                    }, 0, 4);
                    #endregion

                    // copy PNG stream to cursor stream
                    pngStream.Seek(0, SeekOrigin.Begin);
                    pngStream.CopyTo(cursorStream);

                    // return cursor stream
                    cursorStream.Seek(0, SeekOrigin.Begin);
                    var customCursor = new Cursor(cursorStream);

                    Mouse.SetCursor(customCursor);

                    //Hide the element to make the illusion that the control is moving,
                    // while it's just the cursor which is changed
                    FigImage.Opacity = 0.5;

                    var effects = DragDrop.DoDragDrop(this, data, DragDropEffects.Move);

                    FigImage.Opacity = 1;

                    Mouse.SetCursor(Cursor);
                }
            }
        }