AxisCanvas.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using System.Windows.Media.Animation;
  8. namespace MECF.Framework.UI.Client.ClientControls.Common
  9. {
  10. public class AxisCanvas : Canvas
  11. {
  12. private Storyboard currentStoryboard;
  13. public bool ShowAxisPoint
  14. {
  15. get { return (bool)GetValue(ShowAxisPointProperty); }
  16. set { SetValue(ShowAxisPointProperty, value); }
  17. }
  18. // Using a DependencyProperty as the backing store for ShowAxisPoint. This enables animation, styling, binding, etc...
  19. public static readonly DependencyProperty ShowAxisPointProperty =
  20. DependencyProperty.Register("ShowAxisPoint", typeof(bool), typeof(AxisCanvas), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  21. public static readonly DependencyProperty AxisLeftProperty;
  22. static AxisCanvas()
  23. {
  24. DefaultStyleKeyProperty.OverrideMetadata(typeof(AxisCanvas), new FrameworkPropertyMetadata(typeof(AxisCanvas)));
  25. AxisLeftProperty = DependencyProperty.Register("AxisLeft", typeof(int), typeof(AxisCanvas), new UIPropertyMetadata(8));
  26. }
  27. public int AxisLeft
  28. {
  29. get
  30. {
  31. return (int)GetValue(AxisLeftProperty);
  32. }
  33. set
  34. {
  35. SetValue(AxisLeftProperty, value);
  36. }
  37. }
  38. protected override void OnRender(DrawingContext dc)
  39. {
  40. base.OnRender(dc);
  41. if (ShowAxisPoint)
  42. {
  43. dc.DrawEllipse(Brushes.Black, new Pen(Brushes.White, 2), new Point(AxisLeft, Height / 2), 5, 5);
  44. }
  45. // var text = new FormattedText(
  46. //CurrentAngle.ToString(),
  47. //CultureInfo.GetCultureInfo("en-us"),
  48. //FlowDirection.LeftToRight,
  49. //new Typeface("Verdana"),
  50. //14,
  51. //Brushes.Black);
  52. // dc.DrawText(text, new Point(AxisLeft, 60));
  53. }
  54. public double CurrentAngle
  55. {
  56. get
  57. {
  58. if (RenderTransform is RotateTransform)
  59. {
  60. return (RenderTransform as RotateTransform).Angle;
  61. }
  62. return 0;
  63. }
  64. }
  65. public void Rotate(int angle, bool absolute = true, int ms = 0, double accelerationRatio = 0, double decelerationRatio = 0, Action onComplete = null)
  66. {
  67. var storyboard = new Storyboard();
  68. storyboard.Completed += (s, e) => onComplete?.Invoke();
  69. if (Rotate(storyboard, angle, absolute, ms, accelerationRatio, decelerationRatio, onComplete))
  70. {
  71. storyboard.Begin();
  72. }
  73. else
  74. {
  75. onComplete?.Invoke();
  76. }
  77. }
  78. public bool Rotate(Storyboard storyboard, int angle, bool absolute = true, int ms = 0, double accelerationRatio = 0, double decelerationRatio = 0, Action onComplete = null)
  79. {
  80. var oldAngle = 0d;
  81. if (RenderTransform is RotateTransform)
  82. {
  83. oldAngle = (RenderTransform as RotateTransform).Angle;
  84. }
  85. var newAngle = absolute ? angle : oldAngle + angle;
  86. if (newAngle == oldAngle)
  87. {
  88. return false;
  89. }
  90. var top = Height / 2;
  91. var leftPoint = new Point(AxisLeft, top);
  92. if (ms <= 0)
  93. {
  94. RenderTransform = new RotateTransform(newAngle, leftPoint.X, leftPoint.Y);
  95. return false;
  96. //InvalidateVisual();
  97. }
  98. else
  99. {
  100. var rotateTransform = new RotateTransform();
  101. rotateTransform.CenterX = leftPoint.X;
  102. rotateTransform.CenterY = leftPoint.Y;
  103. RenderTransform = rotateTransform;
  104. var animation = new DoubleAnimation(oldAngle, newAngle, TimeSpan.FromMilliseconds(ms));
  105. animation.AccelerationRatio = accelerationRatio;
  106. animation.DecelerationRatio = decelerationRatio;
  107. storyboard.Children.Add(animation);
  108. Storyboard.SetTarget(animation, this);
  109. Storyboard.SetTargetProperty(animation, new PropertyPath("RenderTransform.Angle"));
  110. return true;
  111. }
  112. }
  113. public void Stop()
  114. {
  115. if (currentStoryboard != null)
  116. {
  117. currentStoryboard.Stop(this);
  118. currentStoryboard = null;
  119. }
  120. }
  121. }
  122. }