资源简介

最基础的操作,就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;
        }
    }
}


资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Test_MoveSize
{
    /// 
    /// App.xaml 的交互逻辑
    /// 

    public partial class App : Application
    {
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     57344  2020-08-30 12:38  Test_MoveSize\.vs\Test_MoveSize\v16\.suo

     文件        189  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\App.config

     文件        380  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\App.xaml

     文件        340  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\App.xaml.cs

     文件      83456  2020-08-30 12:38  Test_MoveSize\Test_MoveSize\bin\Debug\Test_MoveSize.exe

     文件        189  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\bin\Debug\Test_MoveSize.exe.config

     文件      38400  2020-08-30 12:38  Test_MoveSize\Test_MoveSize\bin\Debug\Test_MoveSize.pdb

     文件      72501  2020-01-21 22:28  Test_MoveSize\Test_MoveSize\Images\10.jpg

     文件        592  2020-08-30 12:21  Test_MoveSize\Test_MoveSize\MainWindow.xaml

     文件       4096  2020-08-30 12:37  Test_MoveSize\Test_MoveSize\MainWindow.xaml.cs

     文件       2314  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\Properties\AssemblyInfo.cs

     文件       2839  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\Properties\Resources.Designer.cs

     文件       5612  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\Properties\Resources.resx

     文件       1100  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\Properties\Settings.Designer.cs

     文件        201  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\Properties\Settings.settings

     文件       4262  2020-08-30 10:30  Test_MoveSize\Test_MoveSize\Test_MoveSize.csproj

     文件       1145  2020-08-30 09:02  Test_MoveSize\Test_MoveSize.sln

     目录          0  2020-08-30 09:02  Test_MoveSize\.vs\Test_MoveSize\v16

     目录          0  2020-08-30 12:38  Test_MoveSize\Test_MoveSize\bin\Debug

     目录          0  2020-08-30 12:38  Test_MoveSize\Test_MoveSize\obj\Debug

     目录          0  2020-08-30 09:02  Test_MoveSize\.vs\Test_MoveSize

     目录          0  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\bin

     目录          0  2020-08-30 10:27  Test_MoveSize\Test_MoveSize\Images

     目录          0  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\obj

     目录          0  2020-08-30 09:02  Test_MoveSize\Test_MoveSize\Properties

    ...D.H.         0  2020-08-30 09:02  Test_MoveSize\.vs

     目录          0  2020-08-30 12:38  Test_MoveSize\Test_MoveSize

     目录          0  2020-08-30 09:02  Test_MoveSize

----------- ---------  ---------- -----  ----

               274960                    28

............此处省略1个文件信息

评论

共有 条评论