From 3b877b5501c3c0e4ccb7e4cfd37ddae60f670e4f Mon Sep 17 00:00:00 2001
From: chenghaoran <chenghaoran@ipilotwhale.com>
Date: 星期三, 21 二月 2024 22:36:24 +0800
Subject: [PATCH] 完善基础组件;
---
Controls/InnerWindow.xaml.cs | 30 ++++++++-
UI/WorkControl.xaml.cs | 2
Controls/InnerWindow.xaml | 44 +++++++-------
Utils/CanvasHelper.cs | 9 +-
Utils/Resize.cs | 79 +++++++++++++++++++++-----
5 files changed, 117 insertions(+), 47 deletions(-)
diff --git a/Controls/InnerWindow.xaml b/Controls/InnerWindow.xaml
index 53aa544..e9f17ca 100644
--- a/Controls/InnerWindow.xaml
+++ b/Controls/InnerWindow.xaml
@@ -32,32 +32,34 @@
<UserControl.Template>
<ControlTemplate TargetType="UserControl">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="30"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
+ <AdornerDecorator HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
+ <Grid GotFocus="Grid_GotFocus">
+ <Grid.RowDefinitions>
+ <RowDefinition Height="30"/>
+ <RowDefinition Height="*"/>
+ </Grid.RowDefinitions>
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="*"/>
- <ColumnDefinition Width="Auto"/>
- </Grid.ColumnDefinitions>
+ <Grid>
+ <Grid.ColumnDefinitions>
+ <ColumnDefinition Width="*"/>
+ <ColumnDefinition Width="Auto"/>
+ </Grid.ColumnDefinitions>
- <Thumb Style="{StaticResource SliderThumbStyle}" Grid.Column="0" x:Name="thumb1" DragDelta="DragDelta"
+ <Thumb Style="{StaticResource SliderThumbStyle}" Grid.Column="0" x:Name="thumb1" DragDelta="DragDelta"
DragStarted="DragStarted" DragCompleted="DragCompleted"/>
- <StackPanel Grid.Column="1" Orientation="Horizontal">
- <Button Style="{StaticResource ChildWindowButton}" Click="Min_Click"></Button>
- <Button Style="{StaticResource ChildWindowButton}" Click="Max_Click" ></Button>
- <Button Style="{StaticResource ChildWindowCloseButton}" Click="CLose_Click"></Button>
- </StackPanel>
- </Grid>
+ <StackPanel Grid.Column="1" Orientation="Horizontal">
+ <Button Style="{StaticResource ChildWindowButton}" Click="Min_Click"></Button>
+ <Button Style="{StaticResource ChildWindowButton}" Click="Max_Click" ></Button>
+ <Button Style="{StaticResource ChildWindowCloseButton}" Click="CLose_Click"></Button>
+ </StackPanel>
+ </Grid>
- <Border Grid.Row="1" VerticalAlignment="Stretch" BorderThickness="2,0,2,2" BorderBrush="{Binding HeaderBackground}">
- <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></ContentPresenter>
- </Border>
- </Grid>
+ <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>
\ No newline at end of file
diff --git a/Controls/InnerWindow.xaml.cs b/Controls/InnerWindow.xaml.cs
index db9ee1c..bdd3b49 100644
--- a/Controls/InnerWindow.xaml.cs
+++ b/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;
@@ -103,6 +110,19 @@
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)
@@ -187,7 +207,7 @@
{
if (true)
{
- Resize.SetIsResizeable(this, true);
+ Resize.SetIsResizeable(this, true);
}
canvas.Children.Add(this);
this.canvas = canvas;
diff --git a/UI/WorkControl.xaml.cs b/UI/WorkControl.xaml.cs
index 146c005..c759d76 100644
--- a/UI/WorkControl.xaml.cs
+++ b/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)
diff --git a/Utils/CanvasHelper.cs b/Utils/CanvasHelper.cs
index 63bf333..6950430 100644
--- a/Utils/CanvasHelper.cs
+++ b/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;
}
}
diff --git a/Utils/Resize.cs b/Utils/Resize.cs
index 10591b5..63c9c73 100644
--- a/Utils/Resize.cs
+++ b/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;
// 鍒濆鍖杢humb
@@ -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,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)
@@ -369,7 +405,7 @@
}
break;
case ResizeType.Canvas:
- {
+ {
FrameworkElement target = _resizeTarget;
Canvas parent = GetCanvas(target);
FrameworkElement thumb = sender as FrameworkElement;
@@ -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);
+
+ }
+
+ }
+
}
}
--
Gitblit v1.9.1