Skip to content

Commit

Permalink
#78 Add to obtain the current angle
Browse files Browse the repository at this point in the history
 Add to obtain the current angle #78
  • Loading branch information
yanjinhuagood committed Jan 4, 2024
1 parent 86b27f1 commit 247b102
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
Expand All @@ -14,6 +15,29 @@ namespace WPFDevelopers.Samples.Controls
{
public class ElementAdorner : Adorner
{
public event RoutedPropertyChangedEventHandler<double> AngleChanged;

protected virtual void OnAngleChanged(double oldValue, double newValue)
{
RoutedPropertyChangedEventArgs<double> args = new RoutedPropertyChangedEventArgs<double>(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;
Expand Down Expand Up @@ -244,7 +268,7 @@ private Thumb CreateRotateThumb()
return thumb;
}


private Brush GetFactoryRotate()
{
var lan =
Expand All @@ -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();
Expand Down Expand Up @@ -293,9 +316,7 @@ private void Thumb_DragStarted(object sender, DragStartedEventArgs e)
initialAngle = 0;
}
else
{
initialAngle = rotateTransform.Angle;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)));
Expand All @@ -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<double> e)
{
Angle = e.NewValue;
}

private void TransformThumb_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is bool isVisible)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
Canvas.Left="305"
Canvas.Top="205"
Width="280"
Height="100">
Height="100" x:Name="MyTransformThumb">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="40"
Foreground="White"
Text="WPF开发者" />
Foreground="White">
<Run>WPF开发者</Run>
<Run Text="{Binding ElementName=MyTransformThumb,Path=Angle,StringFormat=0.00}"></Run>
</TextBlock>
</controls:TransformThumb>
<controls:TransformThumb
Canvas.Left="100"
Expand Down

0 comments on commit 247b102

Please sign in to comment.