using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; namespace WpfApp37 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private AdornerLayer mAdornerLayer; private DragDropAdorner adorner; public MainWindow() { InitializeComponent(); } private void UIElement_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (sender is Border bd) { bd.CaptureMouse(); // DragDrop.DoDragDrop(bb, "", DragDropEffects.Copy); } } private void UIElement_OnMouseMove(object sender, MouseEventArgs e) { var pos = e.GetPosition(this); Console.WriteLine("move:" + pos.X + pos.Y); if (sender is Border bd) { Console.WriteLine("move0000000------000"); if (bd.IsMouseCaptured) { Move(); } } } private void Move() { adorner = new DragDropAdorner(tb); adorner.IsHitTestVisible = false; mAdornerLayer = AdornerLayer.GetAdornerLayer(cv); mAdornerLayer.IsHitTestVisible = false; mAdornerLayer.Add(adorner); DragDrop.DoDragDrop(tb, "", DragDropEffects.Copy); mAdornerLayer.Remove(adorner); mAdornerLayer = null; } private void UIElement_OnDragEnter(object sender, DragEventArgs e) { } private void UIElement_OnDragOver(object sender, DragEventArgs e) { e.Effects = DragDropEffects.Move | DragDropEffects.Copy; } private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { } private void Bb_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (sender is Border bd) { bd.ReleaseMouseCapture(); // DragDrop.DoDragDrop(bb, "", DragDropEffects.Copy); } } private int i = 0; private void Bb_OnQueryContinueDrag(object sender, QueryContinueDragEventArgs e) { if (e.KeyStates==(DragDropKeyStates.LeftMouseButton|DragDropKeyStates.AltKey) ) { e.Action = DragAction.Cancel; e.Handled = true; } mAdornerLayer.Update(); i++; Console.WriteLine("update "+i); } } public class DragDropAdorner : Adorner { public Brush CustomBrush = null; public DragDropAdorner(UIElement parent) : base(parent) { IsHitTestVisible = false; mDraggedElement = parent as FrameworkElement; } protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); Console.WriteLine("Render///"); if (mDraggedElement != null) { Win32.POINT screenPos = new Win32.POINT(); if (Win32.GetCursorPos(ref screenPos)) { Point pos = PointFromScreen(new Point(screenPos.X, screenPos.Y)); Rect rect = new Rect(pos.X, pos.Y, mDraggedElement.ActualWidth, mDraggedElement.ActualHeight); drawingContext.PushOpacity(1.0); Brush highlight = null; if (CustomBrush == null) { highlight = mDraggedElement.TryFindResource(SystemColors.HighlightBrushKey) as Brush; } else { highlight = CustomBrush; } if (highlight != null) { drawingContext.DrawRectangle(highlight, new Pen(Brushes.Transparent, 0), rect); } Console.WriteLine($"{rect.X},{rect.Y},{rect.Width},{rect.Height}"); drawingContext.DrawRectangle(new VisualBrush(mDraggedElement), new Pen(Brushes.Transparent, 0), rect); drawingContext.Pop(); } else { Console.WriteLine("false"); } } } private FrameworkElement mDraggedElement = null; } public static class Win32 { public struct POINT { public Int32 X; public Int32 Y; } [DllImport("user32.dll")] public static extern bool GetCursorPos(ref POINT point); } }