■ UIElement 클래스에서 잉크 입력 컨트롤을 만드는 방법을 보여준다.
▶ 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 |
using System; using System.Windows; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Input.StylusPlugIns; using System.Windows.Controls; namespace TestProject { /// <summary> /// 잉크 컨트롤 /// </summary> public class InkControl : Label { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 잉크 프리젠터 /// </summary> private InkPresenter presenter; /// <summary> /// 동적 렌더러 /// </summary> private DynamicRenderer renderer; /// <summary> /// 스타일러스 포인트 컬렉션 /// </summary> private StylusPointCollection pointCollection = null; #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.presenter = new InkPresenter(); Content = this.presenter; this.renderer = new DynamicRenderer(); this.presenter.AttachVisuals(this.renderer.RootVisual, this.renderer.DrawingAttributes); StylusPlugIns.Add(this.renderer); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 스타일러스 DOWN 처리하기 - OnStylusDown(e) /// <summary> /// 스타일러스 DOWN 처리하기 /// </summary> /// <param name="e">이벤트 발생자</param> protected override void OnStylusDown(StylusDownEventArgs e) { Stylus.Capture(this); this.pointCollection = new StylusPointCollection(); StylusPointCollection pointCollection = e.GetStylusPoints(this, this.pointCollection.Description); this.pointCollection.Add(pointCollection); } #endregion #region 스타일러스 이동시 처리하기 - OnStylusMove(e) /// <summary> /// 스타일러스 이동시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnStylusMove(StylusEventArgs e) { if(this.pointCollection == null) { return; } StylusPointCollection pointCollection = e.GetStylusPoints(this, this.pointCollection.Description); this.pointCollection.Add(pointCollection); } #endregion #region 스타일러스 UP 처리하기 - OnStylusUp(e) /// <summary> /// 스타일러스 UP 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnStylusUp(StylusEventArgs e) { if(this.pointCollection == null) { return; } StylusPointCollection pointCollection = e.GetStylusPoints(this, this.pointCollection.Description); this.pointCollection.Add(pointCollection); Stroke stroke = new Stroke(this.pointCollection); this.presenter.Strokes.Add(stroke); this.pointCollection = 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); if(e.StylusDevice != null) { return; } this.pointCollection = new StylusPointCollection(); Point point = e.GetPosition(this); this.pointCollection.Add(new StylusPoint(point.X, point.Y)); } #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 || this.pointCollection == null) { return; } Point point = e.GetPosition(this); this.pointCollection.Add(new StylusPoint(point.X, point.Y)); } #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.pointCollection == null) { return; } Point point = e.GetPosition(this); this.pointCollection.Add(new StylusPoint(point.X, point.Y)); Stroke stroke = new Stroke(this.pointCollection); stroke.DrawingAttributes = this.renderer.DrawingAttributes; this.presenter.Strokes.Add(stroke); this.pointCollection = null; } #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:InkControl /> </Border> </Window> |