wpf之DragDrop研究

想要实现一个效果,当鼠标拖拽的时候,有一个跟随的窗口,随着鼠标移动。

相关步骤如下:

1,启动拖拽

            DataObject dataobj = new DataObject("1");
            DragDrop.DoDragDrop(DirectoryTree, dataobj, DragDropEffects.Move);

2,创建一个窗体

            _Drag = new Window_Drag
            {
                Topmost = true,
                WindowStartupLocation = WindowStartupLocation.Manual
            };

            var pt = Mouse.GetPosition(null);
            _Drag.Top = pt.Y + DataCenter.Ins().FrameWindow.Top;
            _Drag.Left = pt.X + DataCenter.Ins().FrameWindow.Left;
            _Drag.ResName = Name;
            _Drag.ResId = ResId;

            _Drag.Show();

3,添加窗体跟随鼠标移动

添加拖拽移动事件

RealPlayGrid.AddHandler(DragOverEvent, new DragEventHandler(RealPlay_Vm.Grid_DragOver), true);

设置窗口的位置:

            if (null != _Drag)
            {
                if (!PeHostWin32.GetCursorPos(out PeHostWin32.POINT p))
                {
                    return;
                }
                Point relativePos = DataCenter.Ins().FrameWindow.PointFromScreen(new System.Windows.Point(p.X,p.Y));
                _Drag.Left = DataCenter.Ins().FrameWindow.Left+ relativePos.X;
                _Drag.Top = DataCenter.Ins().FrameWindow.Top + relativePos.Y;
            }

这里用到了一个windows api 获取鼠标的实时位置

public class PeHostWin32
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;

            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        public static Int32 GWL_EXSTYLE = -20;
        public static Int32 WS_EX_LAYERED = 0x00080000;
        public static Int32 WS_EX_TRANSPARENT = 0x00000020;


        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out POINT pt);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern Int32 GetWindowLong(IntPtr hWnd, Int32 nIndex);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern Int32 SetWindowLong(IntPtr hWnd, Int32 nIndex, Int32 newVal);
    }

****************************************最后居然发现一个问题************************************

DragDrop.DoDragDrop(DirectoryTree, dataobj, DragDropEffects.Move);

上面这个函数居然是阻塞的,等拖拽执行完,才会继续往下执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 WPF DragDrop 例子: XAML 代码: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="800"> <Grid> <StackPanel Orientation="Horizontal" Margin="10"> <TextBlock Text="Drag me:" Margin="0,0,10,0" /> <TextBlock Text="Hello, world!" Background="LightGray" Width="100" Height="50" PreviewMouseLeftButtonDown="TextBlock_PreviewMouseLeftButtonDown" PreviewMouseMove="TextBlock_PreviewMouseMove" PreviewMouseLeftButtonUp="TextBlock_PreviewMouseLeftButtonUp" /> </StackPanel> <StackPanel Orientation="Horizontal" Margin="10"> <TextBlock Text="Drop here:" Margin="0,0,10,0" /> <Border BorderBrush="Black" BorderThickness="1" Width="100" Height="50" AllowDrop="True" Drop="Border_Drop" /> </StackPanel> </Grid> </Window> ``` C# 代码: ```csharp using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace WpfApp1 { public partial class MainWindow : Window { private bool isDragging = false; private Point startPoint; public MainWindow() { InitializeComponent(); } private void TextBlock_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { startPoint = e.GetPosition(null); isDragging = true; } private void TextBlock_PreviewMouseMove(object sender, MouseEventArgs e) { if (isDragging && e.LeftButton == MouseButtonState.Pressed) { Point position = e.GetPosition(null); Vector offset = startPoint - position; if (Math.Abs(offset.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(offset.Y) > SystemParameters.MinimumVerticalDragDistance) { DataObject data = new DataObject("myFormat", "Hello, world!"); DragDrop.DoDragDrop((DependencyObject)sender, data, DragDropEffects.Copy); isDragging = false; } } } private void TextBlock_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { isDragging = false; } private void Border_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("myFormat")) { string text = (string)e.Data.GetData("myFormat"); ((Border)sender).Child = new TextBlock { Text = text, Background = Brushes.LightGray }; } } } } ``` 这个例子演示了如何在 WPF 中实现拖放操作。我们创建了一个包含两个 StackPanel 的 Window,第一个 StackPanel 包含一个 TextBlock,可以被拖动,第二个 StackPanel 包含一个 Border,可以接受拖放操作。当用户按下鼠标左键并移动 TextBlock 时,如果移动距离超过一定阈值,就会触发拖放操作。在拖放操作中,我们将一个字符串放入 DataObject 中,并指定数据格式为 "myFormat"。在 Border 的 Drop 事件中,我们检查数据格式是否为 "myFormat",如果是,就将字符串显示在 Border 中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值