WPF Thumb、控件、窗体、内容、Shape拖动

实例地址:WPFThumb、控件、窗体、内容拖动实例-C#文档类资源-CSDN下载

WPF常见拖动方式:

1、窗体拖动:使用窗体自带拖动方法,DragMove()。

2、Thumb控件拖动:WPF拖动控件,MSDN的描述,Represents a control that can be dragged by the user.(表示可由用户拖动的控件),由DragStarted、DragDelta、DragCompleted着三个事件完成控件的拖动。

3、控件内容拖动:修改控件的AllowDrop属性,AllowDrop=true,允许拖动。

4、Shape拖动:在Canvas上拖动,记录鼠标位置并实时设置Shape位置。

5、控件拖动:需要用AddHandler添加Button.MouseLeftButtonDown等事件,不然无法触发,因为Button.Clicked事件Supress掉了MouseLeftButtonDown。(两种方式:1.在Canvas上拖动 2.在Grid上拖动)

窗体拖动:

<!--xaml代码-->
<Window x:Class="DragDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DragDemo"
        mc:Ignorable="d"
        Title="窗体拖动" Height="450" Width="800" MouseLeftButtonDown="Window_MouseLeftButtonDown">
</Window>

/// <summary>
/// 窗体拖动后台端代码
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.DragMove();
    }
}

Thumb控件拖动:

<!--xaml代码-->
<Window x:Class="DragDemo.ThumbDragWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DragDemo"
        mc:Ignorable="d"
        Title="Thumb拖动" Height="450" Width="800">
    <Canvas>
        <Thumb Name="thumb1" Background="Green" Height="50" Width="100" DragDelta="DragDelta" DragStarted="DragStarted" DragCompleted="DragCompleted" Canvas.Left="335" Canvas.Top="121">
        </Thumb>
    </Canvas>
</Window>

/// <summary>
/// Thumb拖动后端代码
/// </summary>
public partial class ThumbDragWindow : Window
{
    /*
        DragDelta——当 Thumb 控件具有逻辑焦点和鼠标捕获时,随着鼠标位置更改发生一次或多次。
        DragStarted——在 Thumb 控件接收逻辑焦点和鼠标捕获时发生。
        DragCompleted——在 Thumb 控件失去鼠标捕获时发生。
    */
    public ThumbDragWindow()
    {
        InitializeComponent();
    }
    private void DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
    {
        Canvas.SetLeft(thumb1, Canvas.GetLeft(thumb1) + e.HorizontalChange);
        Canvas.SetTop(thumb1, Canvas.GetTop(thumb1) + e.VerticalChange);
    }
    private void DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
    {
        thumb1.Background = Brushes.Red;
    }
    private void DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
    {
        thumb1.Background = Brushes.Green;
    }
}

控件内容拖动:

<!--xaml代码-->
<Window x:Class="DragDemo.ContentDropWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DragDemo"
        mc:Ignorable="d"
        Title="ContentDropWindow" Height="450" Width="800">
    <Grid>
        <Label Name ="label1" Content="TestDrop" Background="Red" Height ="28" HorizontalAlignment="Left" Margin="166,157,0,0"  VerticalAlignment="Top" MouseDown="label1_MouseDown" RenderTransformOrigin="0.54,-0.431"  />
        <Label Name="label2"  Content="ToHere"  Background="Green" Height="28" HorizontalAlignment="Left" Margin ="400,256,0,0"  VerticalAlignment="Top" AllowDrop ="True" Drop="tagert_drop"   />
        <TextBox Width="100" Height="30" Margin="375,97,317,292" />
    </Grid>
</Window>

/// <summary>
/// 控件内容拖动
/// </summary>
public partial class ContentDropWindow : Window
{
    public ContentDropWindow()
    {
        InitializeComponent();
    }
    //拖拽label1到label上,把label1的text赋值给label2
    private void label1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        System.Windows.Controls.Label lbl = (System.Windows.Controls.Label)sender;
        DragDrop.DoDragDrop(lbl, lbl.Content, System.Windows.DragDropEffects.Copy);
    }

    private void tagert_drop(object sender, System.Windows.DragEventArgs e)
    {
        ((System.Windows.Controls.Label)sender).Content = e.Data.GetData(System.Windows.DataFormats.Text);
    }
}

Shape拖动:

<!--xaml代码-->
<Window x:Class="DragDemo.ShapeDragInCanvas"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DragDemo"
        mc:Ignorable="d"
        Title="ShapeDragInCanvas" Height="450" Width="800">
    <Canvas x:Name="canvas1" Background="Green">
        <Canvas  Background="Yellow" Canvas.Left="85" Canvas.Top="51" Height="100" Name="canvas2" Width="105" MouseLeftButtonDown="canvas2_MouseDown"   MouseMove="canvas2_MouseMove" MouseLeftButtonUp="canvas2_MouseLeftButtonUp">
            <Button x:Name="btn" Width="100" Height="30" Canvas.Top="20"  Click="btn_Click"/>
        </Canvas>
    </Canvas>
</Window>

/// <summary>
/// Shape,Canvas 在Canvas中拖动后台代码
/// </summary>
public partial class ShapeDragInCanvas : Window
{
    public ShapeDragInCanvas()
    {
        InitializeComponent();
    }
    Point oldPoint = new Point();
    bool isMove = false;
    private void canvas2_MouseMove(object sender, MouseEventArgs e)
    {
        if (isMove)
        {
            //canvas2.Background = Brushes.White;

            FrameworkElement currEle = sender as FrameworkElement;
            if (currEle.GetType() == typeof(Canvas))
            {
                double xPos = e.GetPosition(null).X - oldPoint.X + (double)currEle.GetValue(Canvas.LeftProperty);
                double yPos = e.GetPosition(null).Y - oldPoint.Y + (double)currEle.GetValue(Canvas.TopProperty);
                currEle.SetValue(Canvas.LeftProperty, xPos);
                currEle.SetValue(Canvas.TopProperty, yPos);
            }
            else
            {
                var temp = VisualTreeHelper.GetParent(currEle);
                var temp1 = VisualTreeHelper.GetChild(currEle, 1);
            }
            oldPoint = e.GetPosition(null);
        }
    }
    private void canvas2_MouseDown(object sender, MouseButtonEventArgs e)
    {
        isMove = true;
        oldPoint = e.GetPosition(null);
    }
    private void canvas2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        isMove = false;
        //canvas2.Background = Brushes.Yellow;
    }       
    private void btn_Click(object sender, RoutedEventArgs e)
    {
        Point sidebarPoint = this.PointToScreen(new Point(this.ActualWidth, this.ActualHeight));
    }
}

控件拖动:1.在Canvas上拖动 2.在Grid上拖动

1.在Canvas上拖动控件:

<!--xaml代码-->
<Window x:Class="DragDemo.ControlDragInCanvas"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DragDemo"
        mc:Ignorable="d" 
        Title="ControlDragInCanvas" Height="450" Width="800">
    <Canvas x:Name="canvas1" Background="Green">
        <Button  x:Name="btn" Background="Yellow" Canvas.Left="85" Canvas.Top="51" Height="50"  Width="105" />
    </Canvas>
</Window>

/// <summary>
/// 控件在Canvas中拖动
/// </summary>
public partial class ControlDragInCanvas : Window
{
    public ControlDragInCanvas()
    {
        InitializeComponent();


        btn.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Element_MouseLeftButtonDown), true);
        btn.AddHandler(Button.MouseMoveEvent, new MouseEventHandler(Element_MouseMove), true);
        btn.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Element_MouseLeftButtonUp), true);

        btn.MouseMove += new MouseEventHandler(Element_MouseMove);
        btn.MouseLeftButtonDown += new MouseButtonEventHandler(Element_MouseLeftButtonDown);
        btn.MouseLeftButtonUp += new MouseButtonEventHandler(Element_MouseLeftButtonUp);
    }

    bool isDragDropInEffect = false;
    Point pos = new Point();

    void Element_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDragDropInEffect)
        {
            FrameworkElement currEle = sender as FrameworkElement;
            double xPos = e.GetPosition(null).X - pos.X + (double)currEle.GetValue(Canvas.LeftProperty);
            double yPos = e.GetPosition(null).Y - pos.Y + (double)currEle.GetValue(Canvas.TopProperty);
            currEle.SetValue(Canvas.LeftProperty, xPos);
            currEle.SetValue(Canvas.TopProperty, yPos);
            pos = e.GetPosition(null);
        }
    }

    void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {

        FrameworkElement fEle = sender as FrameworkElement;
        isDragDropInEffect = true;
        pos = e.GetPosition(null);
        fEle.CaptureMouse();
        fEle.Cursor = Cursors.Hand;
    }

    void Element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (isDragDropInEffect)
        {
            FrameworkElement ele = sender as FrameworkElement;
            isDragDropInEffect = false;
            ele.ReleaseMouseCapture();
        }
    }
}

2.在Grid上拖动控件

<!--xaml代码-->
<Window x:Class="DragDemo.ControlDragInGrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DragDemo"
        mc:Ignorable="d"
        Title="ControlDragInGrid" Height="450" Width="800">
    <Grid>
        <Button x:Name="btn" Content="DragButton" Width="120" Height="40" Background="Green"
              />
    </Grid>
</Window>

/// <summary>
/// 控件在Grid中拖动
/// </summary>
public partial class ControlDragInGrid : Window
{
    bool isDragDropInEffect = false;
    Point pos = new Point();
    public ControlDragInGrid()
    {
        InitializeComponent();
        //需要用AddHandler添加Button.MouseLeftButtonDown等事件,不然无法触发,因为Button.Clicked事件Supress掉了MouseLeftButtonDown。
        btn.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(btn_MouseLeftButtonDown), true);
        btn.AddHandler(Button.MouseMoveEvent, new MouseEventHandler(btn_MouseMove), true);
        btn.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(btn_MouseLeftButtonUp), true);
    }

    private void btn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Button fEle = sender as Button;
        isDragDropInEffect = true;
        pos = e.GetPosition(null);
        fEle.CaptureMouse();
        fEle.Cursor = Cursors.Hand;
    }

    private void btn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (isDragDropInEffect)
        {
            Button ele = sender as Button;
            isDragDropInEffect = false;
            ele.ReleaseMouseCapture();
        }
    }

    private void btn_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDragDropInEffect)
        {
            Button currEle = sender as Button;
            double xPos = e.GetPosition(null).X - pos.X + currEle.Margin.Left;
            double yPos = e.GetPosition(null).Y - pos.Y + currEle.Margin.Top;
            currEle.Margin = new Thickness(xPos, yPos, 0, 0);
            pos = e.GetPosition(null);
        }
    }
}

参考:https://www.cnblogs.com/m7777/p/7110402.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无熵~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值