基本信息
源码名称:WPF 最基础的组件拖动、改变大小
源码大小:0.16M
文件格式:.rar
开发语言:C#
更新时间:2020-08-30
   源码介绍

最基础的操作,就80多行,很简单。这类东西使用Thumb是最基础的方法。

如写xaml文件,则可直接在Thumb组件设置模板来添加组件,

如写cs文件直接操作Thumb,则需用FrameworkElementFactory来添加组件,也可采用本例方法。


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Test_MoveSize
{
    public partial class MainWindow : Window
    {
        Thumb thumb_Move, thumb_Size; // 移动元素、改变大小元素
        Canvas Move_Box; // 组件
        double obj_minesize = 30; // 组件最小尺寸

        public MainWindow()
        {
            InitializeComponent();
            // 放置一个组件,并加上背景图
            ImageBrush bg = new ImageBrush();
            bg.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Images/10.jpg", UriKind.RelativeOrAbsolute));
            Move_Box = new Canvas() { Width = 250, Height = 250, Cursor = Cursors.SizeAll, Background = bg };
            Canvas.SetLeft(Move_Box, 10);
            Canvas.SetTop(Move_Box, 10);
            mainBox.Children.Add(Move_Box);
            // 初始化移动元素
            thumb_Move = new Thumb() { Width = 250, Height = 250, Opacity = 0 };
            Canvas.SetLeft(thumb_Move, 0);
            Canvas.SetTop(thumb_Move, 0);
            thumb_Move.DragDelta  = Move_DragDelta; // 拖动事件
            Move_Box.Children.Add(thumb_Move);
            // 初始化改变尺寸元素
            thumb_Size = new Thumb() { Width = 20, Height = 20, Cursor = Cursors.SizeNWSE };
            Canvas.SetLeft(thumb_Size, 260);
            Canvas.SetTop(thumb_Size, 260);
            thumb_Size.DragDelta  = Size_DragDelta; // 拖动事件
            mainBox.Children.Add(thumb_Size);
        }

        private void Move_DragDelta(object sender, DragDeltaEventArgs e)
        {
            FrameworkElement designerItem = e.Source as FrameworkElement;
            FrameworkElement parent = designerItem.Parent as FrameworkElement;

            double left = Canvas.GetLeft(parent);
            double top = Canvas.GetTop(parent);
            double X = left   e.HorizontalChange;
            double Y = top   e.VerticalChange;
            
            X = X < 0 ? 0 : X;
            Y = Y < 0 ? 0 : Y;
            X = X   designerItem.Width > mainBox.Width ? mainBox.Width - designerItem.Width : X;
            Y = Y   designerItem.Height > mainBox.Height ? mainBox.Height - designerItem.Height : Y;

            Canvas.SetLeft(parent, X);
            Canvas.SetTop(parent, Y);

            Canvas.SetLeft(thumb_Size, X   parent.Width);
            Canvas.SetTop(thumb_Size, Y   parent.Height);
        }

        private void Size_DragDelta(object sender, DragDeltaEventArgs e)
        {
            FrameworkElement designerItem = e.Source as FrameworkElement;
            FrameworkElement parent = designerItem.Parent as FrameworkElement;

            double X = Canvas.GetLeft(designerItem)   e.HorizontalChange;
            double Y = Canvas.GetTop(designerItem)    e.VerticalChange;

            X = X < Canvas.GetLeft(Move_Box)   obj_minesize ? Canvas.GetLeft(Move_Box)   obj_minesize : X;
            Y = Y < Canvas.GetTop(Move_Box)   obj_minesize ? Canvas.GetTop(Move_Box)   obj_minesize : Y;
            X = X   designerItem.Width > parent.Width   designerItem.Width ? parent.Width - designerItem.Width   designerItem.Width : X;
            Y = Y   designerItem.Height > parent.Height   designerItem.Width ? parent.Height - designerItem.Height   designerItem.Width : Y;

            Canvas.SetLeft(designerItem, X);
            Canvas.SetTop(designerItem, Y);

            Move_Box.Width = X - Canvas.GetLeft(Move_Box);
            Move_Box.Height = Y - Canvas.GetTop(Move_Box); ;
            thumb_Move.Width = Move_Box.Width;
            thumb_Move.Height = Move_Box.Height;
        }
    }
}