WPF中拖动控件,实现位置随意摆放[1]

一般的拖动程序,都是实现 MouseLeftButtonDown,MouseLeftButtonUp,MouseMove 这三个事件,大多数的情况下,拖动过程中,都是在 MouseMove 这个函数里面设置控件的坐标。

以下的代码,只有一点点的不同,在拖动过程中,原控件还是在原来位置,只是新产生了一个按控件外形生成的阴影图片,然后设置该阴影图片的位置,最后,鼠标离开的时候,设置原控件的位置。。。

    private void ContainerPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if ((e.ClickCount <= 1) && (sender != e.Source))
        {
            ElementUI source = e.Source as ElementUI;
            this.SelectedItem = source;
            NodeUI eui = e.Source as NodeUI;
            if ((eui != null) && eui.DataSource.CanDrag)
            {
                NodeUI nodeUI = (NodeUI) e.Source;
                Point point = new Point(nodeUI.CenterX, nodeUI.CenterY);
                this.startPoint = base.ToPlot(e.GetPosition(this));
                this.offsetVector = (Vector) (this.startPoint - point);
                this.CreateDragShade(nodeUI);
                this.dragShade.CaptureMouse();//阴影捕获鼠标
                e.Handled = true;
            }
        }
    }

    private void ContainerPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount <= 1)
        {
            NodeUI selectedItem = this.SelectedItem as NodeUI;
            if ((selectedItem != null) && this.dragShade.IsMouseCaptured)
            {
                Point point = base.ToPlot(e.GetPosition(this));
                double dx = point.X - this.startPoint.X;
                double dy = point.Y - this.startPoint.Y;
                Node dataSource = selectedItem.DataSource;
                if ((dataSource != null) && dataSource.CanDrag)
                {
                    dataSource.PerformDrag(dx, dy);
                }
            }
            this.dragShade.ReleaseMouseCapture();
            this.dragShade.Visibility = Visibility.Hidden;
            base.Children.Remove(this.dragShade);
        }
    }

    private void ContainerPanel_MouseMove(object sender, MouseEventArgs e)
    {
        if ((e.LeftButton == MouseButtonState.Pressed) && this.dragShade.IsMouseCaptured)
        {
            Point point = base.ToPlot(e.GetPosition(this));
            PlotPanel.SetCenterX(this.dragShade, point.X - this.offsetVector.X);
            PlotPanel.SetCenterY(this.dragShade, point.Y - this.offsetVector.Y);
        }
    }

    private void CreateDragShade(NodeUI nodeUI)
    {
        VisualBrush brush = new VisualBrush();
        brush.Stretch = Stretch.Fill;
        brush.Visual = nodeUI;
        brush.Opacity = 0.6;
        this.dragShade.Width = nodeUI.ActualWidth;
        this.dragShade.Height = nodeUI.ActualHeight;
        this.dragShade.Stroke = Brushes.Transparent;
        this.dragShade.Fill = brush;
        this.dragShade.Stretch = Stretch.Fill;
        PlotPanel.SetCenterX(this.dragShade, nodeUI.CenterX);
        PlotPanel.SetCenterY(this.dragShade, nodeUI.CenterY);
        this.dragShade.Visibility = Visibility.Visible;
        base.Children.Add(this.dragShade);
    }


评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值