From 247b1023cc1d55e762fef60442fec26e96d8fdf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=A9=9A=E9=8F=B5?= Date: Thu, 4 Jan 2024 23:55:34 +0800 Subject: [PATCH] #78 Add to obtain the current angle Add to obtain the current angle #78 --- .../Controls/ElementAdorner/ElementAdorner.cs | 31 ++++++++++++++++--- .../Controls/ElementAdorner/TransformThumb.cs | 26 ++++++++++++++++ .../ExampleViews/TransformThumbExample.xaml | 8 +++-- 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/WPFDevelopers.Samples.Shared/Controls/ElementAdorner/ElementAdorner.cs b/src/WPFDevelopers.Samples.Shared/Controls/ElementAdorner/ElementAdorner.cs index 59ab606f..bd002afb 100644 --- a/src/WPFDevelopers.Samples.Shared/Controls/ElementAdorner/ElementAdorner.cs +++ b/src/WPFDevelopers.Samples.Shared/Controls/ElementAdorner/ElementAdorner.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Diagnostics; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; @@ -14,6 +15,29 @@ namespace WPFDevelopers.Samples.Controls { public class ElementAdorner : Adorner { + public event RoutedPropertyChangedEventHandler AngleChanged; + + protected virtual void OnAngleChanged(double oldValue, double newValue) + { + RoutedPropertyChangedEventArgs args = new RoutedPropertyChangedEventArgs(oldValue, newValue); + AngleChanged?.Invoke(this, args); + } + + private double angle; + public double Angle + { + get { return angle; } + set + { + if (angle != value) + { + double oldValue = angle; + angle = value; + OnAngleChanged(oldValue, angle); + } + } + } + private const double ThumbSize = 16, ElementMiniSize = 20; private const double RotateThumbSize = 20; private readonly Thumb tLeft; @@ -244,7 +268,7 @@ private Thumb CreateRotateThumb() return thumb; } - + private Brush GetFactoryRotate() { var lan = @@ -262,9 +286,8 @@ private void Thumb_DragDelta(object sender, DragDeltaEventArgs e) { var currentPoint = Mouse.GetPosition(canvas); var deltaVector = Point.Subtract(currentPoint, centerPoint); - var angle = Vector.AngleBetween(startVector, deltaVector); - + Angle = angle; var rotateTransform = designerItem.RenderTransform as RotateTransform; rotateTransform.Angle = initialAngle + Math.Round(angle, 0); designerItem.InvalidateMeasure(); @@ -293,9 +316,7 @@ private void Thumb_DragStarted(object sender, DragStartedEventArgs e) initialAngle = 0; } else - { initialAngle = rotateTransform.Angle; - } } } } diff --git a/src/WPFDevelopers.Samples.Shared/Controls/ElementAdorner/TransformThumb.cs b/src/WPFDevelopers.Samples.Shared/Controls/ElementAdorner/TransformThumb.cs index 34ae92ea..36174b77 100644 --- a/src/WPFDevelopers.Samples.Shared/Controls/ElementAdorner/TransformThumb.cs +++ b/src/WPFDevelopers.Samples.Shared/Controls/ElementAdorner/TransformThumb.cs @@ -1,11 +1,24 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Documents; +using System.Xml.Linq; namespace WPFDevelopers.Samples.Controls { public class TransformThumb : ContentControl { + public double Angle + { + get { return (double)GetValue(AngleProperty); } + set { SetValue(AngleProperty, value); } + } + public static readonly DependencyProperty AngleProperty = + DependencyProperty.Register("Angle", typeof(double), typeof(TransformThumb), new PropertyMetadata(0d, OnAngleChanged)); + + private static void OnAngleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + + } static TransformThumb() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TransformThumb), new FrameworkPropertyMetadata(typeof(TransformThumb))); @@ -21,10 +34,23 @@ void CreateAdorner() var adornerLayer = AdornerLayer.GetAdornerLayer(this); if (adornerLayer != null) { + var adorners = adornerLayer.GetAdorners(this); + if (adorners != null) + foreach (var item in adorners) + if (item is ElementAdorner container) + adornerLayer.Remove(container); var adorner = new ElementAdorner(this); + adorner.AngleChanged -= Adorner_AngleChanged; + adorner.AngleChanged += Adorner_AngleChanged; adornerLayer.Add(adorner); } } + + private void Adorner_AngleChanged(object sender, RoutedPropertyChangedEventArgs e) + { + Angle = e.NewValue; + } + private void TransformThumb_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if (e.NewValue is bool isVisible) diff --git a/src/WPFDevelopers.Samples.Shared/ExampleViews/TransformThumbExample.xaml b/src/WPFDevelopers.Samples.Shared/ExampleViews/TransformThumbExample.xaml index 0b6dd36b..fda13c23 100644 --- a/src/WPFDevelopers.Samples.Shared/ExampleViews/TransformThumbExample.xaml +++ b/src/WPFDevelopers.Samples.Shared/ExampleViews/TransformThumbExample.xaml @@ -27,13 +27,15 @@ Canvas.Left="305" Canvas.Top="205" Width="280" - Height="100"> + Height="100" x:Name="MyTransformThumb"> + Foreground="White"> + WPF开发者 + +