■ InkPresenter 클래스를 사용해 스트로크 회전 어도너를 만드는 방법을 보여준다.
▶ RotatingStrokesAdorner.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; namespace TestProject { /// <summary> /// 스트로크 회전 어도너 /// </summary> public class RotatingStrokesAdorner : Adorner { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 핸들 마진 /// </summary> private const int HANDLE_MARGIN = 10; /// <summary> /// 회전 핸들 썸 /// </summary> private Thumb rotateHandleThumb; /// <summary> /// 외곽선 경로 /// </summary> private Path outlinePath; /// <summary> /// 비주얼 컬렉션 /// </summary> private VisualCollection visualCollection; /// <summary> /// 중심 포인트 /// </summary> private Point centerPoint; /// <summary> /// 마지막 각도 /// </summary> private double lastAngle; /// <summary> /// 회전 변환 /// </summary> private RotateTransform rotateTransform; /// <summary> /// 스트로크 테두리 사각형 /// </summary> private Rect strokeBoundRectangle = Rect.Empty; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 장식 스트로크 컬렉션 - AdornedStrokeCollection /// <summary> /// 장식 스트로크 컬렉션 /// </summary> private StrokeCollection AdornedStrokeCollection { get { return ((InkPresenter)AdornedElement).Strokes; } } #endregion #region 비주얼 자식 카운트 - VisualChildrenCount /// <summary> /// 비주얼 자식 카운트 /// </summary> protected override int VisualChildrenCount { get { return this.visualCollection.Count; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - RotatingStrokesAdorner(adornedElement) /// <summary> /// 생성자 /// </summary> /// <param name="adornedElement">장식 엘리먼트</param> public RotatingStrokesAdorner(UIElement adornedElement) : base(adornedElement) { this.visualCollection = new VisualCollection(this); this.rotateHandleThumb = new Thumb(); this.rotateHandleThumb.Width = 20; this.rotateHandleThumb.Height = 20; this.rotateHandleThumb.Background = Brushes.Blue; this.rotateHandleThumb.Cursor = Cursors.SizeNWSE; this.rotateHandleThumb.DragDelta += rotateHandleThumb_DragDelta; this.rotateHandleThumb.DragCompleted += rotateHandleThumb_DragCompleted; this.outlinePath = new Path(); this.outlinePath.StrokeThickness = 1; this.outlinePath.Stroke = Brushes.Blue; this.visualCollection.Add(this.outlinePath ); this.visualCollection.Add(this.rotateHandleThumb); this.strokeBoundRectangle = AdornedStrokeCollection.GetBounds(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 비주얼 자식 구하기 - GetVisualChild(index) /// <summary> /// 비주얼 자식 구하기 /// </summary> /// <param name="index">인덱스</param> /// <returns>비주얼 자식</returns> protected override Visual GetVisualChild(int index) { return this.visualCollection[index]; } #endregion #region 배열하기 (오버라이드) - ArrangeOverride(finalSize) /// <summary> /// 배열하기 (오버라이드) /// </summary> /// <param name="finalSize">최종 크기</param> /// <returns>배열 크기</returns> protected override Size ArrangeOverride(Size finalSize) { if(this.strokeBoundRectangle.IsEmpty) { return finalSize; } this.centerPoint = new Point ( this.strokeBoundRectangle.X + this.strokeBoundRectangle.Width / 2, this.strokeBoundRectangle.Y + this.strokeBoundRectangle.Height / 2 ); Rect handleRectangle = new Rect ( this.strokeBoundRectangle.X, this.strokeBoundRectangle.Y - (this.strokeBoundRectangle.Height / 2 + HANDLE_MARGIN), this.strokeBoundRectangle.Width, this.strokeBoundRectangle.Height ); if(this.rotateTransform != null) { handleRectangle.Transform(this.rotateTransform.Value); } this.rotateHandleThumb.Arrange(handleRectangle); this.outlinePath.Data = new RectangleGeometry(this.strokeBoundRectangle); this.outlinePath.Arrange(new Rect(finalSize)); return finalSize; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 회전 핸들 썸 드래그 델타 처리하기 - rotateHandleThumb_DragDelta(sender, e) /// <summary> /// 회전 핸들 썸 드래그 델타 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void rotateHandleThumb_DragDelta(object sender, DragDeltaEventArgs e) { Point point = Mouse.GetPosition(this); double deltaX = point.X - this.centerPoint.X; double deltaY = point.Y - this.centerPoint.Y; if(deltaY.Equals(0)) { return; } double tangent = deltaX / deltaY; double angle = Math.Atan(tangent); angle = angle * 180 / Math.PI; if(deltaY > 0) { angle = 180 - Math.Abs(angle); } if(deltaX < 0) { angle = -Math.Abs(angle); } else { angle = Math.Abs(angle); } if(double.IsNaN(angle)) { return; } this.rotateTransform = new RotateTransform(angle, this.centerPoint.X, this.centerPoint.Y); this.outlinePath.RenderTransform = this.rotateTransform; } #endregion #region 회전 핸들 썸 드래그 완료시 처리하기 - rotateHandleThumb_DragCompleted(sender, e) /// <summary> /// 회전 핸들 썸 드래그 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void rotateHandleThumb_DragCompleted(object sender, DragCompletedEventArgs e) { if(this.rotateTransform == null) { return; } Matrix matrix = new Matrix(); matrix.RotateAt(this.rotateTransform.Angle - this.lastAngle, this.centerPoint.X, this.centerPoint.Y); AdornedStrokeCollection.Transform(matrix, true); this.lastAngle = this.rotateTransform.Angle; InvalidateArrange(); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <InkPresenter Name="inkPresenter"> <InkPresenter.Strokes> ALMDAwRIEEU1BQE4GSAyCQD0/wIB6SI6RTMJAPifAgFaIDpFOAgA/gMAAACAfxEAAIA/ HwkRAAAAAAAA8D8KlwE1h/CPd4SB4NA4OicCjcGjcClcDj8Lh8DgUSkUmmU6nUmoUuk 0ukUCQKVyehz+rzuly+bzORx+BReRQ+RTaRCH8JyXhPbgcPicPh8Pg8Oh0qk1SoVGrV Oo0mi0Xi8rm9Xr9Dqc/p87pc/k8XicHicOj1CoVKtVmv1GqUaiUHlYg8el4akXK7m7T cSJgQgghEyym5zx6+PACk4dhPwg/fhCbxY8dp4p2tqnqxyvbPO85z1X1aswhvCd94Tq 55DRUGi4+Tk6OLn4KLkoOejo6ig5KTioOPCD9LlHmrzNxMRCCc3ec8+fe4AKQBmE/Cw 9+FkPNvlOdkrYsWa+acp3Z8erOIT8JaX4S6+FbFilbHNvvPXNJbFqluxghKc5DkwrVF GEEIJ1w5eLKYAKShuF+Dnr4Oa8HVHXNPFFFFho8VFkqsMRYuuvJxiF+F9r4Xx8HFiqs FNcirnweDw9+LvvvixdV0+GhONmlj3wjNOcSCEYTnfLy4oA </InkPresenter.Strokes> </InkPresenter> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
using System.Windows; using System.Windows.Documents; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 어도너 레이어 /// </summary> private AdornerLayer adornerLayer; /// <summary> /// 어도너 /// </summary> private Adorner adorner; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { this.adornerLayer = AdornerLayer.GetAdornerLayer(this.inkPresenter); this.adorner = new RotatingStrokesAdorner(this.inkPresenter); this.adornerLayer.Add(this.adorner); } #endregion } } |