chr
2024-03-07 40213047b1cee48144c5a1eee1778ada6f609ca1
优化InnerWindow的Resize;
12个文件已修改
416 ■■■■■ 已修改文件
Controls/IconTabItem.cs 1 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Controls/InnerWindow.xaml 1 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Controls/InnerWindow.xaml.cs 96 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MainWindow.xaml.cs 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Models/Program.cs 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Models/Sequence.cs 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Models/Step.cs 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Models/Variable.cs 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
UI/Test/ProgramList.xaml.cs 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
UI/WorkControl.xaml.cs 116 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Utils/Resize.cs 76 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test.xml 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Controls/IconTabItem.cs
@@ -44,7 +44,6 @@
            set => _Body = value;
        }
        private BitmapImage _icon;
        public BitmapImage Icon
        {
            get { return (BitmapImage)GetValue(IconProperty); }
Controls/InnerWindow.xaml
@@ -8,7 +8,6 @@
             mc:Ignorable="d" 
             HorizontalAlignment="Stretch"
             HorizontalContentAlignment="Stretch"
             SizeChanged="UserControl_SizeChanged"
             MouseLeftButtonDown="Grid_MouseLeftButtonDown"
             Background="#FFF"
             Focusable="True">
Controls/InnerWindow.xaml.cs
@@ -1,4 +1,5 @@
using System;
using HandyControl.Controls;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
@@ -20,6 +21,7 @@
    /// </summary>
    public partial class InnerWindow : UserControl, INotifyPropertyChanged
    {
        #region INotifyPropertyChanged
        public virtual event PropertyChangedEventHandler PropertyChanged;
        protected virtual void RaisePropertyChanged(string name)
@@ -35,14 +37,28 @@
                RaisePropertyChanged(propertyName);
            }
        }
        #endregion
        public CanvasPosition canvasPosition;
        #region 属性
        #region Common
        public DragType DragType { get; set; } = DragType.Canvas;
        public CanvasPosition canvasPosition;
        private Canvas canvas;
        public WindowState WindowState { get; set; } = WindowState.Normal;
        public AdornerDecorator ResizeAdornerDecorator { get => VisualTreeHelper.GetChild(this, 0) as AdornerDecorator; }
        private string _Title;
        public string Title
        {
            get { return _Title; }
            set => RaiseAndSetIfChanged(ref _Title, value);
        }
        public int Zindex
        {
            get {
            get
            {
                return Canvas.GetZIndex(this);
            }
            set
@@ -50,25 +66,16 @@
                Canvas.SetZIndex(this, value);
            } 
        }
        #endregion
        public AdornerDecorator ResizeAdornerDecorator { get => VisualTreeHelper.GetChild(this, 0) as AdornerDecorator; }
        #region 事件
        public event EventHandler MinEvent;
        public event EventHandler MaxEvent;
        public event EventHandler CloseEvent;
        public event EventHandler ZIndexEvent;
        #endregion
        public WindowState WindowState { get; set; } = WindowState.Normal;
        private string _Title;
        public string Title
        {
            get { return _Title; }
            set => RaiseAndSetIfChanged(ref _Title, value);
        }
        #region DependencyProperty
        public Brush HeaderBackground
        {
            get { return (Brush)GetValue(HeaderBackgroundProperty); }
@@ -88,6 +95,9 @@
        public static readonly DependencyProperty HeaderFontColorProperty =
            DependencyProperty.Register("HeaderFontColor", typeof(Brush), typeof(ProgramList),
                new PropertyMetadata(new SolidColorBrush(Color.FromRgb(255, 255, 225))));
        #endregion
        #endregion
        public InnerWindow() : this(null)
        {
@@ -105,26 +115,7 @@
            DataContext = this;
            InitializeComponent();
            SizeChanged += InnerWindow_SizeChanged;
            Loaded += InnerWindow_Loaded;
        }
        private void InnerWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //throw new NotImplementedException();
        }
        private void InitThumb()
        {
        }
        // 方形工厂
        FrameworkElementFactory GetFactory(Brush back)
        {
            FrameworkElementFactory fac = new FrameworkElementFactory(typeof(Rectangle));
            fac.SetValue(Rectangle.FillProperty, back);
            return fac;
        }
        private void InnerWindow_Loaded(object sender, RoutedEventArgs e)
@@ -142,13 +133,7 @@
            Loaded -= InnerWindow_Loaded;
        }
        private Canvas canvas;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Visibility = Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
        }
        #region 拖拽移动相关
        private void DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            switch (DragType)
@@ -195,11 +180,7 @@
            canvasPosition.Top = Canvas.GetTop(this);
            canvasPosition.Left = Canvas.GetLeft(this);
        }
        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
        }
        #endregion
        /// <summary>
        /// 添加到画布上
@@ -223,6 +204,21 @@
            }
            Canvas.SetLeft(this, canvasPosition == null ? 0 : canvasPosition.Left);
            Canvas.SetTop(this, canvasPosition == null ? 0 : canvasPosition.Top);
            canvas.SizeChanged += Canvas_SizeChanged;
        }
        private void Canvas_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Size newSize = e.NewSize;
            Size previousSize = e.PreviousSize;
            double xRate = newSize.Width / previousSize.Width;
            double yRate = newSize.Height / previousSize.Height;
            Canvas.SetLeft(this, Canvas.GetLeft(this) * xRate);
            Canvas.SetTop(this, Canvas.GetTop(this) * yRate);
            this.Width = this.Width * xRate;
            this.Height = this.Height * yRate;
        }
        private void Min_Click(object sender, RoutedEventArgs e)
@@ -254,12 +250,6 @@
        {
        }
    }
    public class InnerWindowProp : NotifyBase
    {
    }
    public enum DragType
MainWindow.xaml.cs
@@ -2,6 +2,7 @@
using System.Windows;
using System.Windows.Media;
using WPFProject.Events;
using WPFProject.Utils;
namespace WPFProject
{
@@ -27,12 +28,12 @@
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Resize.SetIsResizeable(this, true);
        }
        private void mainControl_OnTabChange(object sender, RoutedEventArgs e)
        {
            string ss = "s";
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
Models/Program.cs
@@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace WPFProject.Models
{
@@ -20,6 +21,16 @@
        }
        /// <summary>
        /// 序列集合
        /// </summary>
        private ObservableCollection<Sequence> _Sequences = new ObservableCollection<Sequence>();
        public ObservableCollection<Sequence> Sequences
        {
            get { return _Sequences; }
            set => RaiseAndSetIfChanged(ref _Sequences, value);
        }
        /// <summary>
        /// 程序所需参数
        /// </summary>
        private ObservableCollection<Variable> _Variables;
@@ -30,34 +41,36 @@
        }
        /// <summary>
        /// 序列集合
        /// 初始参数参数
        /// </summary>
        private ObservableCollection<Sequence> _Sequences;
        public ObservableCollection<Sequence> Sequences
        private Dictionary<string, Variable> _InitVars;
        [XmlIgnore]
        public Dictionary<string, Variable> InitVars
        {
            get { return _Sequences; }
            set => RaiseAndSetIfChanged(ref _Sequences, value);
        }
        /// <summary>
        /// 所需参数
        /// </summary>
        private Dictionary<string, Variable> _Vars;
        public Dictionary<string, Variable> Vars
        {
            get { return _Vars; }
            set { _Vars = value; }
            get { return _InitVars; }
            set { _InitVars = value; }
        }
        /// <summary>
        /// 计算临时参数
        /// </summary>
        private Dictionary<string, Variable> _TempVars;
        [XmlIgnore]
        public Dictionary<string, Variable> TempVars
        {
            get { return _TempVars; }
            set { _TempVars = value; }
        }
        public Program AddSequnce(params Sequence[] sequences)
        {
            foreach (Sequence sequence in sequences)
            {
                if (sequence == null) continue;
                sequence.Program = this;
                Sequences.Add(sequence);
            }
            return this;
        }
    }
}
Models/Sequence.cs
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
namespace WPFProject.Models
{
@@ -17,10 +13,12 @@
            set => RaiseAndSetIfChanged(ref _Name, value);
        }
        private ObservableCollection<Step> _Steps = new ObservableCollection<Step>
        {
            new Step{ Name="000"},
            new Step{ Name="111"}
        [XmlIgnore]
        public Program Program { get; set; }
        private ObservableCollection<Step> _Steps = new ObservableCollection<Step>() {
            new Step{ Name = "000"},new Step{ Name="111"}
        };
        public ObservableCollection<Step> Steps
        {
@@ -28,5 +26,16 @@
            set => RaiseAndSetIfChanged(ref _Steps, value);
        }
        public Sequence AddStep(params Step[] steps)
        {
            foreach (Step step in steps)
            {
                if (step == null) continue;
                step.Program = Program;
                Steps.Add(step);
            }
            return this;
        }
    }
}
Models/Step.cs
@@ -1,14 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace WPFProject.Models
{
    public class Step : NotifyBase
    {
        #region 字段
        [XmlIgnore]
        public Program Program { get; set; }
        private int _Index;
        public int Index
Models/Variable.cs
@@ -4,6 +4,7 @@
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace WPFProject.Models
{
@@ -26,12 +27,14 @@
        public string TypeName
        {
            get { return _TypeName; }
            set {
            set
            {
                RaiseAndSetIfChanged(ref _TypeName, value);
                Type = value == null ? null : Type.GetType(value);
            }
        }
        [XmlIgnore]
        public Type Type { get; private set; }
        /// <summary>
UI/Test/ProgramList.xaml.cs
@@ -31,35 +31,21 @@
            }
        }
        private ObservableCollection<Program> _Programs = new ObservableCollection<Program> {
            new Program{ Name="123",
            Sequences = new ObservableCollection<Sequence>
            {
                new Sequence{
            new Program{ Name="123"}.AddSequnce(new Sequence{
                    Name="S123"
                },
                new Sequence
                },new Sequence
                {
                    Name="S223"
                }
            }
            },
            new Program{ Name="223",
             Sequences = new ObservableCollection<Sequence>
            {
                new Sequence{
                }),
            new Program{ Name="223"}.AddSequnce(new Sequence{
                    Name="D123"
                },
                new Sequence
                },new Sequence
                {
                    Name="D223"
                }
            }
            }
                })
        };
        public ObservableCollection<Program> Programs
        {
            get { return _Programs; }
UI/WorkControl.xaml.cs
@@ -116,6 +116,12 @@
        }
        public int CanvasChildrenCount { get; private set; } = 0;
        #region InnerWindow操作逻辑
        /// <summary>
        /// 设置InnerWindow
        /// </summary>
        /// <param name="inner"></param>
        private void HandleInnerWindow(InnerWindow inner)
        {
            inner.MinEvent += InnerWindowMin;
@@ -126,24 +132,31 @@
            inner.Zindex = CanvasChildrenCount++;
        }
        /// <summary>
        /// InnerWindow焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Inner_GotFocus(object sender, RoutedEventArgs e)
        {
            CanvasHelper.SetTopZIndex(sender as InnerWindow, WorkArea, typeof(InnerWindow));
        }
        /// <summary>
        /// InnerWindow ZIndex切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Inner_ZIndexEvent(object sender, EventArgs e)
        {
            CanvasHelper.SetTopZIndex(sender as InnerWindow, WorkArea, typeof(InnerWindow));
        }
        private void HandleMinInnerWindow(InnerWindow inner)
        {
            MinInnerWindow min = new MinInnerWindow(inner);
            min.MaxEvent += MinInnerWindowMax;
            min.CloseEvent += MinInnerWindowClose;
            BottomBar.AddMinWindow(min);
        }
        /// <summary>
        /// InnerWindow最小化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InnerWindowMin(object sender, System.EventArgs e)
        {
            InnerWindow inner = sender as InnerWindow;
@@ -151,6 +164,11 @@
            HandleMinInnerWindow(inner);
        }
        /// <summary>
        /// InnerWindow最大化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InnerWindowMax(object sender, System.EventArgs e)
        {
            InnerWindow inner = sender as InnerWindow;
@@ -163,7 +181,8 @@
                inner.Height = inner.canvasPosition.Height;
                inner.WindowState = WindowState.Normal;
            }
            else{
            else
            {
                Canvas.SetLeft(inner, 0);
                Canvas.SetTop(inner, 0);
                inner.Width = WorkArea.ActualWidth;
@@ -172,6 +191,58 @@
            }
        }
        /// <summary>
        /// 重设大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WorkControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //Size newSize = e.NewSize;
            //Size previousSize = e.PreviousSize;
            //double xRate = newSize.Width / previousSize.Width;
            //double yRate = newSize.Height / previousSize.Height;
            //for (int i = 0; i < VisualTreeHelper.GetChildrenCount(WorkArea); i++)
            //{
            //    UIElement child = VisualTreeHelper.GetChild(WorkArea, i) as UIElement;
            //    Canvas.SetLeft(child, Canvas.GetLeft(child) * xRate);
            //    Canvas.SetTop(child, Canvas.GetTop(child) * yRate);
            //}
            //for (int i = 0; i < VisualTreeHelper.GetChildrenCount(WorkArea); i++)
            //{
            //    UIElement child = VisualTreeHelper.GetChild(WorkArea, i) as UIElement;
            //    if (Canvas.GetLeft(child) >= newSize.Width)
            //    {
            //        Canvas.SetLeft(child, Canvas.GetLeft(child) + newSize.Width - previousSize.Width);
            //    }
            //    if (Canvas.GetTop(child) >= newSize.Height)
            //    {
            //        Canvas.SetTop(child, Canvas.GetTop(child) + newSize.Height - previousSize.Height);
            //    }
            //}
        }
        #endregion
        #region 底部MinInnerWindow相关
        /// <summary>
        /// 底部MinInnerWindow处理
        /// </summary>
        /// <param name="inner"></param>
        private void HandleMinInnerWindow(InnerWindow inner)
        {
            MinInnerWindow min = new MinInnerWindow(inner);
            min.MaxEvent += MinInnerWindowMax;
            min.CloseEvent += MinInnerWindowClose;
            BottomBar.AddMinWindow(min);
        }
        /// <summary>
        /// 底部MinInnerWindow展开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MinInnerWindowMax(object sender, System.EventArgs e)
        {
            MinInnerWindow min = sender as MinInnerWindow;
@@ -180,32 +251,17 @@
            BottomBar.RemoveMinWindow(min);
        }
        /// <summary>
        /// 底部MinInnerWin关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MinInnerWindowClose(object sender, System.EventArgs e)
        {
            MinInnerWindow min = sender as MinInnerWindow;
            BottomBar.RemoveMinWindow(min);
        }
        private void WorkControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Size newSize = e.NewSize;
            Size previousSize = e.PreviousSize;
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(WorkArea); i++)
            {
                UIElement child = VisualTreeHelper.GetChild(WorkArea, i) as UIElement;
                if (Canvas.GetLeft(child) >= newSize.Width)
                {
                    Canvas.SetLeft(child, Canvas.GetLeft(child) + newSize.Width - previousSize.Width);
                }
                if (Canvas.GetTop(child) >= newSize.Height)
                {
                    Canvas.SetTop(child, Canvas.GetTop(child) + newSize.Height - previousSize.Height);
                }
            }
        }
        #endregion
        public void OnTabChange(object sender, SelectionChangedEventArgs args)
        {
Utils/Resize.cs
@@ -346,6 +346,8 @@
                if (_resizeType == ResizeType.Canvas && _resizeTarget is InnerWindow _inner)
                {
                    Canvas parent = GetCanvas(_inner);
                    // 如果是个InnerWindow,就把InnerWindowZIndex置顶
                    // TODO 可以改成接口或者DependencyPrope来做
                    CanvasHelper.SetTopZIndex(_inner, parent, _inner.GetType());
                }
            }
@@ -415,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;
                                //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;
                                }
                                //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;
test.xml
@@ -7,9 +7,11 @@
        <Name>S123</Name>
        <Steps>
          <Step>
            <Index>0</Index>
            <Name>000</Name>
          </Step>
          <Step>
            <Index>0</Index>
            <Name>111</Name>
          </Step>
        </Steps>
@@ -18,9 +20,11 @@
        <Name>S223</Name>
        <Steps>
          <Step>
            <Index>0</Index>
            <Name>000</Name>
          </Step>
          <Step>
            <Index>0</Index>
            <Name>111</Name>
          </Step>
        </Steps>
@@ -34,9 +38,11 @@
        <Name>D123</Name>
        <Steps>
          <Step>
            <Index>0</Index>
            <Name>000</Name>
          </Step>
          <Step>
            <Index>0</Index>
            <Name>111</Name>
          </Step>
        </Steps>
@@ -45,9 +51,11 @@
        <Name>D223</Name>
        <Steps>
          <Step>
            <Index>0</Index>
            <Name>000</Name>
          </Step>
          <Step>
            <Index>0</Index>
            <Name>111</Name>
          </Step>
        </Steps>