• wpf 拖拽有阴影效果


    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);
        }
    }
  • 相关阅读:
    poj1014 Dividing (多重背包)
    HDOJ 1316 How Many Fibs?
    最大字串和
    WHY IE AGAIN?
    Codeforces Round #143 (Div. 2) (ABCD 思维场)
    自用组帧工具
    菜鸟学EJB(二)——在同一个SessionBean中使用@Remote和@Local
    shell 块注释
    检测到在集成的托管管道模式下不适用的 ASP.NET 设置的解决方法
    Windows Myeclipse 10 安装 Perl 插件
  • 原文地址:https://www.cnblogs.com/congqiandehoulai/p/12721396.html
Copyright © 2020-2023  润新知