| Controls/InnerWindow.xaml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| Controls/InnerWindow.xaml.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| UI/WorkControl.xaml.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| Utils/CanvasHelper.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| Utils/Resize.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
Controls/InnerWindow.xaml
@@ -32,7 +32,8 @@ <UserControl.Template> <ControlTemplate TargetType="UserControl"> <Grid> <AdornerDecorator HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > <Grid GotFocus="Grid_GotFocus"> <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition Height="*"/> @@ -54,10 +55,11 @@ </StackPanel> </Grid> <Border Grid.Row="1" VerticalAlignment="Stretch" BorderThickness="2,0,2,2" BorderBrush="{Binding HeaderBackground}"> <Border Grid.Row="1" Background="LightGray" VerticalAlignment="Stretch" BorderThickness="2,0,2,2" BorderBrush="{Binding HeaderBackground}"> <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></ContentPresenter> </Border> </Grid> </AdornerDecorator> </ControlTemplate> </UserControl.Template> </UserControl> Controls/InnerWindow.xaml.cs
@@ -4,10 +4,15 @@ using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; using WPFProject.Models; using WPFProject.UI.Test; using WPFProject.Utils; using static WPFProject.Utils.Resize.Resizeable; namespace WPFProject.Controls { @@ -36,13 +41,15 @@ public DragType DragType { get; set; } = DragType.Canvas; private int _Zindex; public int Zindex { get { return _Zindex; } set => RaiseAndSetIfChanged(ref _Zindex, value); set { Canvas.SetZIndex(this, value); } } public AdornerDecorator ResizeAdornerDecorator { get => VisualTreeHelper.GetChild(this, 0) as AdornerDecorator; } public event EventHandler MinEvent; @@ -105,6 +112,19 @@ //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) { if (canvasPosition == null) UI/WorkControl.xaml.cs
@@ -130,7 +130,7 @@ private void Inner_ZIndexEvent(object sender, EventArgs e) { CanvasHelper.SetTopZIndex(sender, WorkArea, typeof(InnerWindow)); CanvasHelper.SetTopZIndex(sender as InnerWindow, WorkArea, typeof(InnerWindow)); } private void HandleMinInnerWindow(InnerWindow inner) Utils/CanvasHelper.cs
@@ -2,25 +2,26 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Media; using WPFProject.Controls; namespace WPFProject.Utils { public class CanvasHelper { public static void SetTopZIndex(object element, DependencyObject parent, Type targetClass) public static void SetTopZIndex(InnerWindow element, DependencyObject parent, Type targetClass) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { UIElement child = VisualTreeHelper.GetChild(parent, i) as UIElement; InnerWindow child = VisualTreeHelper.GetChild(parent, i) as InnerWindow; if (child != null && targetClass == child.GetType()) { if (element == child) { Canvas.SetZIndex(child, 1); child.Zindex = 1; } else { Canvas.SetZIndex(child, 0); child.Zindex = 0; } } 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,15 +154,25 @@ _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)); } } } @@ -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 (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,13 @@ 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); CanvasHelper.SetTopZIndex(_inner, parent, _inner.GetType()); } } private void Thumb_DragDelta(object sender, DragDeltaEventArgs e) { switch (_resizeType) @@ -501,7 +537,7 @@ } // 装饰器 class ResizeAdorner : Adorner public class ResizeAdorner : Adorner { // 布局容器,如果不使用布局容器,则需要给上述8个控件布局,实现和Grid布局定位是一样的,会比较繁琐且意义不大。 UIElement _child; @@ -522,6 +558,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); } } } }