1
- using System . Windows ;
1
+ using System ;
2
+ using System . ComponentModel ;
3
+ using System . Windows ;
2
4
using System . Windows . Controls ;
3
5
using System . Windows . Controls . Primitives ;
4
6
using System . Windows . Documents ;
5
7
using System . Windows . Input ;
6
8
using System . Windows . Media ;
9
+ using System . Windows . Media . Media3D ;
7
10
using System . Windows . Shapes ;
11
+ using WPFDevelopers . Helpers ;
8
12
9
13
namespace WPFDevelopers . Samples . Controls
10
14
{
11
15
public class ElementAdorner : Adorner
12
16
{
13
17
private const double ThumbSize = 16 , ElementMiniSize = 20 ;
18
+ private const double RotateThumbSize = 20 ;
14
19
private readonly Thumb tLeft ;
15
20
private readonly Thumb tRight ;
16
21
private readonly Thumb bLeftBottom ;
17
22
private readonly Thumb bRightBottom ;
18
23
private readonly Thumb tMove ;
19
24
private readonly VisualCollection visualCollection ;
25
+ private readonly Thumb tRotate ;
20
26
27
+ private Canvas canvas ;
28
+ private Point centerPoint ;
29
+ private FrameworkElement designerItem ;
30
+ private double initialAngle ;
31
+ private RotateTransform rotateTransform ;
32
+ private Vector startVector ;
21
33
public ElementAdorner ( UIElement adornedElement ) : base ( adornedElement )
22
34
{
23
35
visualCollection = new VisualCollection ( this ) ;
@@ -30,6 +42,7 @@ public ElementAdorner(UIElement adornedElement) : base(adornedElement)
30
42
CreateThumb ( Cursors . SizeNESW , HorizontalAlignment . Left , VerticalAlignment . Bottom ) ) ;
31
43
visualCollection . Add ( bRightBottom =
32
44
CreateThumb ( Cursors . SizeNWSE , HorizontalAlignment . Right , VerticalAlignment . Bottom ) ) ;
45
+ visualCollection . Add ( tRotate = CreateRotateThumb ( ) ) ;
33
46
}
34
47
35
48
protected override int VisualChildrenCount => visualCollection . Count ;
@@ -38,7 +51,7 @@ protected override void OnRender(DrawingContext drawingContext)
38
51
{
39
52
var offset = ThumbSize / 2 ;
40
53
var sz = new Size ( ThumbSize , ThumbSize ) ;
41
- var renderPen = new Pen ( new SolidColorBrush ( Colors . White ) , 2.0 ) ;
54
+ var renderPen = new Pen ( ControlsHelper . Brush , 2.0 ) ;
42
55
var startPoint = new Point ( AdornedElement . RenderSize . Width / 2 ,
43
56
AdornedElement . RenderSize . Height - AdornedElement . RenderSize . Height ) ;
44
57
var endPoint = new Point ( AdornedElement . RenderSize . Width / 2 ,
@@ -50,6 +63,11 @@ protected override void OnRender(DrawingContext drawingContext)
50
63
bLeftBottom . Arrange ( new Rect ( new Point ( - offset , AdornedElement . RenderSize . Height - offset ) , sz ) ) ;
51
64
bRightBottom . Arrange ( new Rect (
52
65
new Point ( AdornedElement . RenderSize . Width - offset , AdornedElement . RenderSize . Height - offset ) , sz ) ) ;
66
+ tRotate . Arrange ( new Rect ( new Point ( AdornedElement . RenderSize . Width / 2 - 10 , - ( RotateThumbSize - 5 ) ) , new Size ( 20 , 20 ) ) ) ;
67
+
68
+ var adornedElementRect = new Rect ( AdornedElement . RenderSize ) ;
69
+ var pen = new Pen ( ControlsHelper . Brush , 2 ) ;
70
+ drawingContext . DrawRectangle ( null , pen , adornedElementRect ) ;
53
71
}
54
72
55
73
private Thumb CreateMoveThumb ( )
@@ -112,7 +130,7 @@ private Thumb CreateThumb(Cursor cursor, HorizontalAlignment horizontal, Vertica
112
130
VerticalAlignment = vertical ,
113
131
Template = new ControlTemplate ( typeof ( Thumb ) )
114
132
{
115
- VisualTree = GetFactory ( new SolidColorBrush ( Colors . White ) )
133
+ VisualTree = GetFactory ( ControlsHelper . Brush )
116
134
}
117
135
} ;
118
136
@@ -203,5 +221,82 @@ private void SetPositon(FrameworkElement element,double left, double top)
203
221
Canvas . SetLeft ( element , left ) ;
204
222
Canvas . SetTop ( element , top ) ;
205
223
}
224
+ /// <summary>
225
+ /// 旋转
226
+ /// </summary>
227
+ /// <returns></returns>
228
+ private Thumb CreateRotateThumb ( )
229
+ {
230
+ Thumb thumb = new Thumb ( )
231
+ {
232
+ Cursor = Cursors . Hand ,
233
+ Width = RotateThumbSize ,
234
+ Height = RotateThumbSize ,
235
+ VerticalAlignment = VerticalAlignment . Top ,
236
+ Margin = new Thickness ( 0 , - RotateThumbSize , 0 , 0 ) ,
237
+ Template = new ControlTemplate ( typeof ( Thumb ) )
238
+ {
239
+ VisualTree = GetFactory ( GetFactoryRotate ( ) ) ,
240
+ } ,
241
+ } ;
242
+ thumb . DragDelta += Thumb_DragDelta ;
243
+ thumb . DragStarted += Thumb_DragStarted ;
244
+ return thumb ;
245
+ }
246
+
247
+
248
+ private Brush GetFactoryRotate ( )
249
+ {
250
+ var lan =
251
+ "M242 842l60-60c48 36 106 60 168 68v86c-86-8-164-42-228-94zM554 850c62-8 120-32 166-68l62 60c-64 52-142 86-228 94v-86zM782 722c36-48 60-104 68-166h86c-8 86-42 162-94 226zM640 512c0 70-58 128-128 128s-128-58-128-128 58-128 128-128 128 58 128 128zM174 554c8 62 32 120 68 166l-60 62c-52-64-86-142-94-228h86zM242 302c-36 48-60 106-68 168h-86c8-86 42-164 94-228zM850 470c-8-62-32-120-68-168l60-60c52 64 86 142 94 228h-86zM782 182l-60 60c-48-36-106-60-168-68v-86c86 8 164 42 228 94zM470 174c-62 8-120 32-168 68l-60-60c64-52 142-86 228-94v86z" ;
252
+ var converter = TypeDescriptor . GetConverter ( typeof ( Geometry ) ) ;
253
+ var geometry = ( Geometry ) converter . ConvertFrom ( lan ) ;
254
+ TileBrush bsh =
255
+ new DrawingBrush ( new GeometryDrawing ( ControlsHelper . Brush , new Pen ( Brushes . Transparent , 2 ) , geometry ) ) ;
256
+ bsh . Stretch = Stretch . Fill ;
257
+ return bsh ;
258
+ }
259
+ private void Thumb_DragDelta ( object sender , DragDeltaEventArgs e )
260
+ {
261
+ if ( designerItem != null && canvas != null )
262
+ {
263
+ var currentPoint = Mouse . GetPosition ( canvas ) ;
264
+ var deltaVector = Point . Subtract ( currentPoint , centerPoint ) ;
265
+
266
+ var angle = Vector . AngleBetween ( startVector , deltaVector ) ;
267
+
268
+ var rotateTransform = designerItem . RenderTransform as RotateTransform ;
269
+ rotateTransform . Angle = initialAngle + Math . Round ( angle , 0 ) ;
270
+ designerItem . InvalidateMeasure ( ) ;
271
+ }
272
+ }
273
+
274
+ private void Thumb_DragStarted ( object sender , DragStartedEventArgs e )
275
+ {
276
+ var thumb = sender as Thumb ;
277
+ designerItem = AdornedElement as FrameworkElement ;
278
+ canvas = VisualTreeHelper . GetParent ( designerItem ) as Canvas ;
279
+ if ( canvas != null )
280
+ {
281
+ centerPoint = designerItem . TranslatePoint (
282
+ new Point ( designerItem . Width * designerItem . RenderTransformOrigin . X ,
283
+ designerItem . Height * designerItem . RenderTransformOrigin . Y ) ,
284
+ canvas ) ;
285
+
286
+ var startPoint = Mouse . GetPosition ( canvas ) ;
287
+ startVector = Point . Subtract ( startPoint , centerPoint ) ;
288
+
289
+ rotateTransform = designerItem . RenderTransform as RotateTransform ;
290
+ if ( rotateTransform == null )
291
+ {
292
+ designerItem . RenderTransform = new RotateTransform ( 0 ) ;
293
+ initialAngle = 0 ;
294
+ }
295
+ else
296
+ {
297
+ initialAngle = rotateTransform . Angle ;
298
+ }
299
+ }
300
+ }
206
301
}
207
302
}
0 commit comments