■ UIElement 클래스를 사용해 잉크 입력 컨트롤에서 잉크를 선택하는 방법을 보여준다.
▶ InkMode.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
namespace TestProject { /// <summary> /// 잉크 모드 /// </summary> public enum InkMode { /// <summary> /// 잉크 /// </summary> Ink, /// <summary> /// 선택 /// </summary> Select } } |
▶ InkControl.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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Input.StylusPlugIns; using System.Windows.Media; namespace TestProject { /// <summary> /// 잉크 컨트롤 /// </summary> public class InkControl : Label { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 잉크 모드 /// </summary> private InkMode mode; /// <summary> /// 잉크 드로잉 어트리뷰트 /// </summary> private DrawingAttributes inkDrawingAttributes; /// <summary> /// 선택 드로잉 어트리뷰트 /// </summary> private DrawingAttributes selectionDrawingAttributes; /// <summary> /// 잉크 프리젠터 /// </summary> private InkPresenter presenter; /// <summary> /// 선택 증분 올가미 히트 테스터 /// </summary> private IncrementalLassoHitTester selectionIncrementalLassoHitTester; /// <summary> /// 선택 스트로크 컬렉션 /// </summary> private StrokeCollection selectedStrokeCollection = new StrokeCollection(); /// <summary> /// 스타일러스 포인트 컬렉션 /// </summary> private StylusPointCollection stylusPointCollection; /// <summary> /// 올가미 경로 스트로크 /// </summary> private Stroke lassoPathStroke; /// <summary> /// 동적 렌더러 /// </summary> private DynamicRenderer renderer; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 모드 - Mode /// <summary> /// 모드 /// </summary> public InkMode Mode { get { return this.mode; } set { this.mode = value; if(this.mode == InkMode.Ink) { this.renderer.DrawingAttributes = this.inkDrawingAttributes; } else { this.renderer.DrawingAttributes = this.selectionDrawingAttributes; } this.presenter.DetachVisuals(this.renderer.RootVisual); this.presenter.AttachVisuals(this.renderer.RootVisual, this.renderer.DrawingAttributes); } } #endregion #region 잉크 드로잉 어트리뷰트 - InkDrawingAttributes /// <summary> /// 잉크 드로잉 어트리뷰트 /// </summary> public DrawingAttributes InkDrawingAttributes { get { return this.inkDrawingAttributes; } } #endregion #region 선택 드로잉 어트리뷰트 - SelectionDrawingAttributes /// <summary> /// 선택 드로잉 어트리뷰트 /// </summary> public DrawingAttributes SelectionDrawingAttributes { get { return this.selectionDrawingAttributes; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - InkControl() /// <summary> /// 생성자 /// </summary> static InkControl() { Type ownerType = typeof(InkControl); ClipToBoundsProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(true)); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - InkControl() /// <summary> /// 생성자 /// </summary> public InkControl() { this.mode = InkMode.Ink; this.presenter = new InkPresenter(); Content = this.presenter; this.inkDrawingAttributes = new DrawingAttributes(); this.inkDrawingAttributes.Width = 5; this.inkDrawingAttributes.Height = 5; this.selectionDrawingAttributes = new DrawingAttributes(); this.selectionDrawingAttributes.Color = Colors.DarkGray; this.selectionDrawingAttributes.Width = 3; this.selectionDrawingAttributes.Height = 3; this.inkDrawingAttributes.AttributeChanged += new PropertyDataChangedEventHandler(drawingAttributes_AttributeChanged); this.selectionDrawingAttributes.AttributeChanged += new PropertyDataChangedEventHandler(drawingAttributes_AttributeChanged); this.renderer = new DynamicRenderer(); this.renderer.DrawingAttributes = this.inkDrawingAttributes; StylusPlugIns.Add(this.renderer); this.presenter.AttachVisuals(this.renderer.RootVisual, this.renderer.DrawingAttributes); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 스타일러스 DOWN 처리하기 - OnStylusDown(e) /// <summary> /// 스타일러스 DOWN 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnStylusDown(StylusDownEventArgs e) { base.OnStylusDown(e); Stylus.Capture(this); this.stylusPointCollection = new StylusPointCollection(); StylusPointCollection pointCollection = e.GetStylusPoints(this, this.stylusPointCollection.Description); this.stylusPointCollection.Add(pointCollection); InitializeHitTester(pointCollection); } #endregion #region 스타일러스 이동시 처리하기 - OnStylusMove(e) /// <summary> /// 스타일러스 이동시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnStylusMove(StylusEventArgs e) { if(this.stylusPointCollection == null) { return; } StylusPointCollection pointCollection = e.GetStylusPoints(this, this.stylusPointCollection.Description); this.stylusPointCollection.Add(pointCollection); AddPointsToHitTester(pointCollection); } #endregion #region 스타일러스 UP 처리하기 - OnStylusUp(e) /// <summary> /// 스타일러스 UP 처리하기 /// </summary> /// <param name="e">이벤트 발생자</param> protected override void OnStylusUp(StylusEventArgs e) { this.stylusPointCollection = this.stylusPointCollection ?? new StylusPointCollection(); StylusPointCollection pointCollection = e.GetStylusPoints(this, this.stylusPointCollection.Description); this.stylusPointCollection.Add(pointCollection); AddPointsToHitTester(pointCollection); AddStrokeToPresenter(Mode == InkMode.Select); this.stylusPointCollection = null; Stylus.Capture(null); } #endregion #region 마우스 왼쪽 버튼 DOWN 처리하기 - OnMouseLeftButtonDown(e) /// <summary> /// 마우스 왼쪽 버튼 DOWN 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); Mouse.Capture(this); if(e.StylusDevice != null) { return; } Point point = e.GetPosition(this); StylusPointCollection pointCollection = new StylusPointCollection(new Point[] { point }); this.stylusPointCollection = new StylusPointCollection(); this.stylusPointCollection.Add(pointCollection); InitializeHitTester(pointCollection); } #endregion #region 마우스 이동시 처리하기 - OnMouseMove(e) /// <summary> /// 마우스 이동시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if(e.StylusDevice != null) { return; } if(e.LeftButton == MouseButtonState.Released) { return; } this.stylusPointCollection = this.stylusPointCollection ?? new StylusPointCollection(); Point point = e.GetPosition(this); StylusPointCollection pointCollection = new StylusPointCollection(new Point[] { point }); this.stylusPointCollection.Add(pointCollection); AddPointsToHitTester(pointCollection); } #endregion #region 마우스 왼쪽 버튼 UP 처리하기 - OnMouseLeftButtonUp(e) /// <summary> /// 마우스 왼쪽 버튼 UP 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); if(e.StylusDevice != null) { return; } if(this.stylusPointCollection == null) { this.stylusPointCollection = new StylusPointCollection(); } Point point = e.GetPosition(this); StylusPointCollection pointCollection = new StylusPointCollection(new Point[] { point }); this.stylusPointCollection.Add(pointCollection); AddPointsToHitTester(pointCollection); AddStrokeToPresenter(Mode == InkMode.Select); this.stylusPointCollection = null; Mouse.Capture(null); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 드로잉 어트리뷰트 어트리뷰트 변경시 처리하기 - drawingAttributes_AttributeChanged(sender, e) /// <summary> /// 드로잉 어트리뷰트 어트리뷰트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void drawingAttributes_AttributeChanged(object sender, PropertyDataChangedEventArgs e) { this.presenter.DetachVisuals(this.renderer.RootVisual); this.presenter.AttachVisuals(this.renderer.RootVisual, this.renderer.DrawingAttributes); } #endregion #region 선택 증분 올가미 히트 테스터 선택 변경시 처리하기 - selectionIncrementalLassoHitTester_SelectionChanged(sender, e) /// <summary> /// 선택 증분 올가미 히트 테스터 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void selectionIncrementalLassoHitTester_SelectionChanged(object sender, LassoSelectionChangedEventArgs e) { foreach(Stroke selectedStroke in e.SelectedStrokes) { selectedStroke.DrawingAttributes.Color = Colors.Red; this.selectedStrokeCollection.Add(selectedStroke); } foreach(Stroke unselectedStroke in e.DeselectedStrokes) { unselectedStroke.DrawingAttributes.Color = this.inkDrawingAttributes.Color; this.selectedStrokeCollection.Remove(unselectedStroke); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 히트 테스터 초기화하기 - InitializeHitTester(pointCollection) /// <summary> /// 히트 테스터 초기화하기 /// </summary> /// <param name="pointCollection">포인트 컬렉션</param> private void InitializeHitTester(StylusPointCollection pointCollection) { foreach(Stroke selectedStroke in this.selectedStrokeCollection) { selectedStroke.DrawingAttributes.Color = this.inkDrawingAttributes.Color; } this.selectedStrokeCollection.Clear(); if(this.mode == InkMode.Select) { if(this.lassoPathStroke != null) { this.presenter.Strokes.Remove(this.lassoPathStroke); this.lassoPathStroke = null; } this.selectionIncrementalLassoHitTester = this.presenter.Strokes.GetIncrementalLassoHitTester(80); this.selectionIncrementalLassoHitTester.SelectionChanged += selectionIncrementalLassoHitTester_SelectionChanged; this.selectionIncrementalLassoHitTester.AddPoints(pointCollection); } } #endregion #region 히트 테스트에 포인트 추가하기 - AddPointsToHitTester(pointCollection) /// <summary> /// 히트 테스트에 포인트 추가하기 /// </summary> /// <param name="pointCollection">포인트 컬렉션</param> private void AddPointsToHitTester(StylusPointCollection pointCollection) { if(this.mode == InkMode.Select && this.selectionIncrementalLassoHitTester != null && this.selectionIncrementalLassoHitTester.IsValid) { this.selectionIncrementalLassoHitTester.AddPoints(pointCollection); } } #endregion #region 프리젠터에 스트로크 추가하기 - AddStrokeToPresenter() /// <summary> /// 프리젠터에 스트로크 추가하기 /// </summary> private void AddStrokeToPresenter(bool up = false) { Stroke newStroke = new Stroke(this.stylusPointCollection); if(this.mode == InkMode.Ink) { newStroke.DrawingAttributes = this.inkDrawingAttributes.Clone(); this.presenter.Strokes.Add(newStroke); } if(this.mode == InkMode.Select && this.lassoPathStroke == null) { if(up) { this.presenter.Strokes.Remove(this.lassoPathStroke); } else { this.lassoPathStroke = newStroke; this.lassoPathStroke.DrawingAttributes = this.selectionDrawingAttributes.Clone(); this.presenter.Strokes.Add(this.lassoPathStroke); this.selectionIncrementalLassoHitTester.SelectionChanged -= selectionIncrementalLassoHitTester_SelectionChanged; this.selectionIncrementalLassoHitTester.EndHitTesting(); } } } #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 25 26 27 28 29 30 31 32 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <local:InkControl x:Name="inkControl" /> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10" Orientation="Vertical"> <Button Name="inkButton" HorizontalAlignment="Left" Width="100" Height="30" Content="잉크" /> <Button Name="selectButton" HorizontalAlignment="Left" Margin="0 10 0 0" Width="100" Height="30" Content="선택하기" /> </StackPanel> </Grid> </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 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.inkButton.Click += inkButton_Click; this.selectButton.Click += selectButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 잉크 버튼 클릭시 처리하기 - inkButton_Click(sender, e) /// <summary> /// 잉크 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void inkButton_Click(object sender, RoutedEventArgs e) { this.inkControl.Mode = InkMode.Ink; } #endregion #region 선택하기 버튼 클릭시 처리하기 - selectButton_Click(sender, e) /// <summary> /// 선택하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void selectButton_Click(object sender, RoutedEventArgs e) { this.inkControl.Mode = InkMode.Select; } #endregion } } |