chr
2024-03-07 303e6180d4f3d88e0feafc9515cae3be8117d2e7
Utils/Resize.cs
@@ -6,6 +6,8 @@
using System.Windows.Media;
using System;
using System.Windows.Shapes;
using WPFProject.Controls;
using System.Xml.Linq;
namespace WPFProject.Utils
{
@@ -107,6 +109,18 @@
        static readonly DependencyProperty ResizeableProperty =
           DependencyProperty.RegisterAttached("Resizeable", typeof(Resizeable), typeof(Resize), new PropertyMetadata(null));
        static int i = 0;
        static SolidColorBrush[] colors = new SolidColorBrush[] {
            new SolidColorBrush(Colors.White),
            new SolidColorBrush(Colors.Red),
            new SolidColorBrush(Colors.Blue),
            new SolidColorBrush(Colors.Green),
            new SolidColorBrush(Colors.LightBlue),
            new SolidColorBrush(Colors.Orange),
            new SolidColorBrush(Colors.Yellow),
            new SolidColorBrush(Colors.Black),
        };
        public class Resizeable
        {
            ResizeType _resizeType;
@@ -140,22 +154,32 @@
                    _thumbsParent = VisualTreeHelper.GetChild(thumbsRoot, 0) as FrameworkElement;
                }
                if (thumbsRoot != null)
                {
                    UIElement target = _resizeTarget is Window _windowTarget ? _windowTarget.Content as UIElement : _resizeTarget;
                    if (target != null)
                    UIElement element = _resizeTarget is Window _windowTarget ? _windowTarget.Content as UIElement : _resizeTarget;
                    if (element != null)
                    {
                        AdornerLayer layer = AdornerLayer.GetAdornerLayer(target);
                        if (layer == null) throw new Exception("获取控件装饰层失败,控件可能没有装饰层!");
                        layer.Add(new ResizeAdorner(target, thumbsRoot));
                        if (element is InnerWindow _inner)
                        {
                            AdornerLayer layer = _inner.ResizeAdornerDecorator.AdornerLayer;
                            if (layer == null)
                                throw new Exception("获取控件装饰层失败,控件可能没有装饰层!");
                            layer.Add(new ResizeAdorner(layer, thumbsRoot));
                        }
                        else
                        {
                            AdornerLayer layer = AdornerLayer.GetAdornerLayer(element);
                            if (layer == null)
                                throw new Exception("获取控件装饰层失败,控件可能没有装饰层!");
                            layer.Add(new ResizeAdorner(element, thumbsRoot));
                        }
                    }
                }
                // 使用缺省样式
                if (thumbsRoot == null)
                {
                    Thumb leftThumb = null, topThumb = null, rightThumb = null, bottomThumb = null,
                    Thumb leftThumb = null, topThumb = null, rightThumb = null, bottomThumb = null,
                        lefTopThumb = null, rightTopThumb = null, rightBottomThumb = null, leftbottomThumb = null;
                    int thumnSize = 5;
                    // 初始化thumb
@@ -251,6 +275,7 @@
                    leftbottomThumb.Width = thumnSize;
                    leftbottomThumb.Height = thumnSize;
                    Grid grid = new Grid();
                    grid.Children.Add(leftThumb);
                    grid.Children.Add(topThumb);
@@ -271,10 +296,20 @@
                    }
                    if (element != null)
                    {
                        var layer = AdornerLayer.GetAdornerLayer(element);
                        if (layer == null)
                            throw new Exception("获取控件装饰层失败,控件可能没有装饰层!");
                        layer.Add(new ResizeAdorner(element, grid));
                        if (element is InnerWindow _inner)
                        {
                            AdornerLayer layer = _inner.ResizeAdornerDecorator.AdornerLayer;
                            if (layer == null)
                                throw new Exception("获取控件装饰层失败,控件可能没有装饰层!");
                            layer.Add(new ResizeAdorner(layer, grid));
                        }
                        else
                        {
                            AdornerLayer layer = AdornerLayer.GetAdornerLayer(element);
                            if (layer == null)
                                throw new Exception("获取控件装饰层失败,控件可能没有装饰层!");
                            layer.Add(new ResizeAdorner(element, grid));
                        }
                    }
                    thumbsRoot = grid;
                    _thumbsParent = thumbsRoot;
@@ -308,12 +343,15 @@
            private void Thumb_DragStarted(object sender, DragStartedEventArgs e)
            {
                _resizeType = GetResizeType();
                if (_resizeType == ResizeType.Canvas)
                if (_resizeType == ResizeType.Canvas && _resizeTarget is InnerWindow _inner)
                {
                    Canvas parent = GetCanvas(_resizeTarget);
                    CanvasHelper.SetTopZIndex(_resizeTarget, parent, _resizeTarget.GetType());
                    Canvas parent = GetCanvas(_inner);
                    // 如果是个InnerWindow,就把InnerWindowZIndex置顶
                    // TODO 可以改成接口或者DependencyPrope来做
                    CanvasHelper.SetTopZIndex(_inner, parent, _inner.GetType());
                }
            }
            private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
            {
                switch (_resizeType)
@@ -369,7 +407,7 @@
                        }
                        break;
                    case ResizeType.Canvas:
                        {
                        {
                            FrameworkElement target = _resizeTarget;
                            Canvas parent = GetCanvas(target);
                            FrameworkElement thumb = sender as FrameworkElement;
@@ -379,42 +417,80 @@
                                return;
                            }
                            double oldLeft = double.IsNaN(Canvas.GetLeft(target)) ? 0 : Canvas.GetLeft(target),
                                oldTop = double.IsNaN(Canvas.GetTop(target)) ? 0 : Canvas.GetTop(target),
                                oldWidth = double.IsNaN(target.ActualWidth) ? 0 : target.ActualWidth,
                                oldHeight = double.IsNaN(target.ActualHeight) ? 0 : target.ActualHeight;
                            double left, top, width, height;
                            if (thumb.HorizontalAlignment == HorizontalAlignment.Left)
                            double x = e.HorizontalChange, y = e.VerticalChange;
                            bool toLeft = x < 0, toTop = y < 0;
                            bool isLeft = thumb.HorizontalAlignment == HorizontalAlignment.Left,
                                isTop = thumb.VerticalAlignment == VerticalAlignment.Top;
                            if (isLeft)
                            {
                                left = double.IsNaN(Canvas.GetLeft(target)) ? 0 : Canvas.GetLeft(target) + e.HorizontalChange;
                                width = (double.IsNaN(target.ActualWidth) ? 0 : target.ActualWidth) - e.HorizontalChange;
                                // 左侧的拖拽
                                left = oldLeft + x;
                                width = oldWidth - x;
                                if (left < 0) { left = 0; width = oldWidth - (left - oldLeft); }
                                width = width < MinWidth ? MinWidth : width;
                            }
                            else
                            {
                                left = Canvas.GetLeft(target);
                                width = (double.IsNaN(target.ActualWidth) ? 0 : target.ActualWidth) + e.HorizontalChange;
                                // 右侧的拖拽
                                left = oldLeft >= 0 ? oldLeft : 0;
                                width = oldWidth + x;
                                width = width < MinWidth ? MinWidth : width;
                                if (left + width > parent.ActualWidth) width = parent.ActualWidth - left;
                            }
                            if (thumb.VerticalAlignment == VerticalAlignment.Top)
                            if (isTop)
                            {
                                top = double.IsNaN(Canvas.GetTop(target)) ? 0 : Canvas.GetTop(target) + e.VerticalChange;
                                height = (double.IsNaN(target.ActualHeight) ? 0 : target.ActualHeight) - e.VerticalChange;
                                // 上边的拖拽
                                top = oldTop + y;
                                height = oldHeight - y;
                                if (top < 0) { top = 0; height = oldHeight - (top - oldTop); }
                                height = height < MinHeight ? MinHeight : height;
                            }
                            else
                            {
                                top = Canvas.GetTop(target);
                                height = (double.IsNaN(target.ActualHeight) ? 0 : target.ActualHeight) + e.VerticalChange;
                                // 下边的拖拽
                                top = oldTop > 0 ? oldTop : 0;
                                height = oldHeight + y;
                                height = height < MinHeight ? MinHeight : height;
                                if (top + height > parent.ActualHeight) height = parent.ActualHeight - top;
                            }
                            if (thumb.HorizontalAlignment != HorizontalAlignment.Center && thumb.HorizontalAlignment != HorizontalAlignment.Stretch)
                            if (thumb.HorizontalAlignment != HorizontalAlignment.Center &&
                                thumb.HorizontalAlignment != HorizontalAlignment.Stretch)
                            {
                                if (e.HorizontalChange > 0 || (width >= MinWidth && left >= 0))
                                {
                                    Canvas.SetLeft(target, left);
                                    target.Width = width;
                                }
                                // 水平方向最终判断
                                Canvas.SetLeft(target, left);
                                target.Width = width;
                                //if ((isLeft && toLeft && left >= 0) ||
                                //    (isLeft && !toLeft && width >= MinWidth) ||
                                //    (!isLeft && toLeft && width >= MinWidth) ||
                                //    (!isLeft && !toLeft && (left + width) <= parent.ActualWidth))
                                //{
                                //    Canvas.SetLeft(target, left);
                                //    target.Width = width;
                                //}
                            }
                            if (thumb.VerticalAlignment != VerticalAlignment.Center && thumb.VerticalAlignment != VerticalAlignment.Stretch)
                            if (thumb.VerticalAlignment != VerticalAlignment.Center &&
                                thumb.VerticalAlignment != VerticalAlignment.Stretch)
                            {
                                if (e.VerticalChange > 0 || (height >= MinHeight && top >= 0))
                                {
                                    Canvas.SetTop(target, top);
                                    target.Height = height;
                                }
                                // 垂直方向最终判断
                                Canvas.SetTop(target, top);
                                target.Height = height;
                                //if ((isTop && toTop && top >= 0) ||
                                //    (isTop && !toTop && height >= MinHeight) ||
                                //    (!isTop && toTop && height >= MinHeight) ||
                                //    (!isTop && !toTop && (top + height) <= parent.ActualHeight))
                                //{
                                //    Canvas.SetTop(target, top);
                                //    target.Height = height;
                                //}
                            }
                        }
                        break;
@@ -501,7 +577,7 @@
            }
            // 装饰器
            class ResizeAdorner : Adorner
            public class ResizeAdorner : Adorner
            {
                // 布局容器,如果不使用布局容器,则需要给上述8个控件布局,实现和Grid布局定位是一样的,会比较繁琐且意义不大。
                UIElement _child;
@@ -522,6 +598,17 @@
                    _child.Arrange(new Rect(new Point(0, 0), finalSize));
                    return finalSize;
                }
                public void ttt(int index)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(_child); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(_child, i);
                        Canvas.SetZIndex(child as UIElement, index);
                    }
                }
            }
        }