日期:2014-05-17  浏览次数:20982 次

C# 实现任意控件的随意拖动

目的实现窗体里面的所有控件可以随意拖动

C#布局代码如下:

<Window x:Class="DragLabel.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" MouseMove="Window_MouseMove" MouseUp="Window_MouseUp">
    <Grid>
        <Canvas Height="227" HorizontalAlignment="Left" Margin="20,31,0,0" Name="canvas1" VerticalAlignment="Top" Width="447">
            <Label Canvas.Left="84" Canvas.Top="120" Content="Label" Height="28" Name="label1" MouseDown="label1_MouseDown" />
            <StackPanel Canvas.Left="242" Canvas.Top="96" Height="100" Name="stackPanel1" Width="142" MouseDown="stackPanel1_MouseDown">
                <Label Content="Label" Height="28" Name="label2" />
                <Label Content="Label" Height="28" Name="label3" />
            </StackPanel>
        </Canvas>
    </Grid>
</Window>

C#实现代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DragLabel
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        bool IsMouseDown = false;
        Point mousePoint;
        object mouseCtrl = null;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    Point theMousePoint = e.GetPosition(this.canvas1);
                    Canvas.SetLeft((UIElement)mouseCtrl, theMousePoint.X - (mousePoint.X - Canvas.GetLeft(((UIElement)mouseCtrl))));
                    Canvas.SetTop((UIElement)mouseCtrl, theMousePoint.Y - (mousePoint.Y - Canvas.GetTop(((UIElement)mouseCtrl))));
                    mousePoint = theMousePoint;
                }
            }
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (IsMouseDown)
            {
                IsMouseDown = false;
            }  
        }

        private void label1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                IsMouseDown = true;
                mousePoint = e.GetPosition(this.canvas1);
                mouseCtrl = sender;
            }       
        }

        private void stackPanel1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                IsMouseDown = true;
                mousePoint = e.GetPosition(this.canvas1);
                mouseCtrl = sender;
            }
        }
    }
}