■ InkCanvas 클래스에서 커스텀 렌더링 잉크를 사용하는 방법을 보여준다.
▶ CustomStroke.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 |
using System.Windows; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; namespace TestProject { /// <summary> /// 커스텀 스트로크 /// </summary> public class CustomStroke : Stroke { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 브러시 /// </summary> private Brush brush; /// <summary> /// 펜 /// </summary> private Pen pen; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - CustomStroke(stylusPointCollection) /// <summary> /// 생성자 /// </summary> /// <param name="stylusPointCollection">스타일러스 포인트 컬렉션</param> public CustomStroke(StylusPointCollection stylusPointCollection) : base(stylusPointCollection) { this.brush = new LinearGradientBrush(Colors.Red, Colors.Blue, 20d); this.pen = new Pen(this.brush, 2d); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 그리기 (코어) - DrawCore(drawingContext, drawingAttributes) /// <summary> /// 그리기 (코어) /// </summary> /// <param name="drawingContext">드로잉 컨텍스트</param> /// <param name="drawingAttributes">드로잉 어트리뷰트</param> protected override void DrawCore(DrawingContext drawingContext, DrawingAttributes drawingAttributes) { Point previousPoint = new Point(double.NegativeInfinity, double.NegativeInfinity); for(int i = 0; i < StylusPoints.Count; i++) { Point point = (Point)StylusPoints[i]; Vector vector = Point.Subtract(previousPoint, point); if(vector.Length > 4) { double radius = StylusPoints[i].PressureFactor * 10d; drawingContext.DrawEllipse(this.brush, this.pen, point, radius, radius); previousPoint = point; } } } #endregion } } |
▶ CustomDynamicRenderer.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 |
using System; using System.Windows; using System.Windows.Input; using System.Windows.Input.StylusPlugIns; using System.Windows.Media; namespace TestProject { /// <summary> /// 커스텀 동적 렌더러 /// </summary> public class CustomDynamicRenderer : DynamicRenderer { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 브러시 /// </summary> [ThreadStatic] private static Brush _brush = null; /// <summary> /// 펜 /// </summary> [ThreadStatic] private static Pen _pen = null; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이전 포인트 /// </summary> private Point previousPoint; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Method //////////////////////////////////////////////////////////////////////////////// Protected #region 스타일러스 DOWN 처리하기 - OnStylusDown(rawStylusInput) /// <summary> /// 스타일러스 DOWN 처리하기 /// </summary> /// <param name="rawStylusInput">원시 스타일러스 입력</param> protected override void OnStylusDown(RawStylusInput rawStylusInput) { this.previousPoint = new Point(double.NegativeInfinity, double.NegativeInfinity); base.OnStylusDown(rawStylusInput); } #endregion #region 그리기 처리하기 - OnDraw(drawingContext, stylusPointCollection, geometry, fillBrush) /// <summary> /// 그리기 처리하기 /// </summary> /// <param name="drawingContext">드로잉 컨텍스트</param> /// <param name="stylusPointCollection">스타일러스 포인트 컬렉션</param> /// <param name="geometry">지오메트리</param> /// <param name="fillBrush">채우기 브러시</param> protected override void OnDraw(DrawingContext drawingContext, StylusPointCollection stylusPointCollection, Geometry geometry, Brush fillBrush) { _brush = _brush ?? new LinearGradientBrush(Colors.Red, Colors.Blue, 20d); _pen = _pen ?? new Pen(_brush, 2d); for(int i = 0; i < stylusPointCollection.Count; i++) { Point point = (Point)stylusPointCollection[i]; Vector vector = Point.Subtract(this.previousPoint, point); if(vector.Length > 4) { double radius = stylusPointCollection[i].PressureFactor * 10d; drawingContext.DrawEllipse(_brush, _pen, point, radius, radius); this.previousPoint = point; } } } #endregion } } |
▶ CustomInkCanvas.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 |
using System.Windows.Controls; namespace TestProject { /// <summary> /// 커스텀 잉크 캔버스 /// </summary> public class CustomInkCanvas : InkCanvas { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 커스텀 동적 렌더러 /// </summary> private CustomDynamicRenderer renderer = new CustomDynamicRenderer(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - CustomInkCanvas() /// <summary> /// 생성자 /// </summary> public CustomInkCanvas() : base() { DynamicRenderer = this.renderer; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 스트로크 수집시 처리하기 - OnStrokeCollected(e) /// <summary> /// 스트로크 수집시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnStrokeCollected(InkCanvasStrokeCollectedEventArgs e) { Strokes.Remove(e.Stroke); CustomStroke stroke = new CustomStroke(e.Stroke.StylusPoints); Strokes.Add(stroke); InkCanvasStrokeCollectedEventArgs inkCanvasStrokeCollectedEventArgs = new InkCanvasStrokeCollectedEventArgs(stroke); base.OnStrokeCollected(inkCanvasStrokeCollectedEventArgs); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<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"> <Border Margin="10" BorderThickness="2" BorderBrush="Black"> <local:CustomInkCanvas /> </Border> </Window> |