■ EventManager 클래스의 RegisterRoutedEvent 정적 메소드를 사용해 라우팅 이벤트를 만드는 방법을 보여준다.
▶ KnockButton.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 |
using System; using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace TestProject { /// <summary> /// 노크 버튼 /// </summary> public class KnockButton : Control { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 노크 이벤트 - Knock /// <summary> /// 노크 이벤트 /// </summary> public event RoutedEventHandler Knock { add { AddHandler(KnockEvent, value); } remove { RemoveHandler(KnockEvent, value); } } #endregion #region 노크 사전 이벤트 - PreviewKnock /// <summary> /// 노크 사전 이벤트 /// </summary> public event RoutedEventHandler PreviewKnock { add { AddHandler(PreviewKnockEvent, value); } remove { RemoveHandler(PreviewKnockEvent, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 텍스트 속성 /// </summary> public static readonly DependencyProperty TextProperty; /// <summary> /// 노크 이벤트 /// </summary> public static readonly RoutedEvent KnockEvent; /// <summary> /// 노크 사전 이벤트 /// </summary> public static readonly RoutedEvent PreviewKnockEvent; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 포맷 텍스트 /// </summary> private FormattedText formattedText; /// <summary> /// 마우스 상위 여부 /// </summary> private bool isMouseReallyOver; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 텍스트 - Text /// <summary> /// 텍스트 /// </summary> public string Text { set { SetValue(TextProperty, value == null ? " " : value); } get { return (string)GetValue(TextProperty); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - KnockButton() /// <summary> /// 생성자 /// </summary> static KnockButton() { TextProperty = DependencyProperty.Register ( "Text", typeof(string), typeof(KnockButton), new FrameworkPropertyMetadata ( " ", FrameworkPropertyMetadataOptions.AffectsMeasure ) ); KnockEvent = EventManager.RegisterRoutedEvent ( "Knock", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(KnockButton) ); PreviewKnockEvent = EventManager.RegisterRoutedEvent ( "PreviewKnock", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(KnockButton) ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 노크시 처리하기 - OnKnock() /// <summary> /// 노크시 처리하기 /// </summary> protected virtual void OnKnock() { RoutedEventArgs e = new RoutedEventArgs(); e.RoutedEvent = KnockButton.KnockEvent; e.Source = this; RaiseEvent(e); } #endregion #region 프리뷰 노크시 처리하기 - OnPreviewKnock() /// <summary> /// 프리뷰 노크시 처리하기 /// </summary> protected virtual void OnPreviewKnock() { RoutedEventArgs e = new RoutedEventArgs(); e.RoutedEvent = KnockButton.PreviewKnockEvent; e.Source = this; RaiseEvent(e); } #endregion #region 마우스 진입시 처리하기 - OnMouseEnter(e) /// <summary> /// 마우스 진입시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseEnter(MouseEventArgs e) { base.OnMouseEnter(e); InvalidateVisual(); } #endregion #region 마우스 이탈시 처리하기 - OnMouseLeave(e) /// <summary> /// 마우스 이탈시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseLeave(MouseEventArgs e) { base.OnMouseLeave(e); InvalidateVisual(); } #endregion #region 마우스 이동시 처리하기 - OnMouseMove(e) /// <summary> /// 마우스 이동시 처리하기 /// </summary> /// <param name="e">이벤트 발생자</param> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); Point mousePoint = e.GetPosition(this); bool isReallyOverNow = ( mousePoint.X >= 0 && mousePoint.X < ActualWidth && mousePoint.Y >= 0 && mousePoint.Y < ActualHeight ); if(isReallyOverNow != this.isMouseReallyOver) { this.isMouseReallyOver = isReallyOverNow; InvalidateVisual(); } } #endregion #region 마우스 왼쪽 버튼 DOWN 처리하기 - OnMouseLeftButtonDown(e) /// <summary> /// 마우스 왼쪽 버튼 DOWN 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); CaptureMouse(); InvalidateVisual(); e.Handled = true; } #endregion #region 마우스 왼쪽 버튼 UP 처리하기 - OnMouseLeftButtonUp(e) /// <summary> /// 마우스 왼쪽 버튼 UP 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); if(IsMouseCaptured) { if(this.isMouseReallyOver) { OnPreviewKnock(); OnKnock(); } e.Handled = true; Mouse.Capture(null); } } #endregion #region 마우스 캡처 해제시 처리하기 - OnLostMouseCapture(e) /// <summary> /// 마우스 캡처 해제시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnLostMouseCapture(MouseEventArgs e) { base.OnLostMouseCapture(e); InvalidateVisual(); } #endregion #region 키 DOWN 처리하기 - OnKeyDown(e) /// <summary> /// 키 DOWN 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if(e.Key == Key.Space || e.Key == Key.Enter) { e.Handled = true; } } #endregion #region 키 UP 처리하기 - OnKeyUp(e) /// <summary> /// 키 UP 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnKeyUp(KeyEventArgs e) { base.OnKeyUp(e); if(e.Key == Key.Space || e.Key == Key.Enter) { OnPreviewKnock(); OnKnock(); e.Handled = true; } } #endregion #region 측정하기 (오버라이딩) - MeasureOverride(availableSize) /// <summary> /// 측정하기 (오버라이딩) /// </summary> /// <param name="availableSize">이용 가능한 크기</param> /// <returns>측정 크기</returns> protected override Size MeasureOverride(Size availableSize) { this.formattedText = new FormattedText ( Text, CultureInfo.CurrentCulture, FlowDirection, new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Foreground ); Size desiredSize = new Size(Math.Max(48, this.formattedText.Width) + 4, this.formattedText.Height + 4); desiredSize.Width += Padding.Left + Padding.Right; desiredSize.Height += Padding.Top + Padding.Bottom; desiredSize.Width = Math.Min(desiredSize.Width , availableSize.Width ); desiredSize.Height = Math.Min(desiredSize.Height, availableSize.Height); return desiredSize; } #endregion #region 렌더링 처리하기 - OnRender(drawingContext) /// <summary> /// 렌더링 처리하기 /// </summary> /// <param name="drawingContext">DrawingContext</param> protected override void OnRender(DrawingContext drawingContext) { Brush backgroundBrush = SystemColors.ControlBrush; if(this.isMouseReallyOver && IsMouseCaptured) { backgroundBrush = SystemColors.ControlDarkBrush; } Pen pen = new Pen(Foreground, IsMouseOver ? 2 : 1); drawingContext.DrawRoundedRectangle ( backgroundBrush, pen, new Rect(new Point(0, 0), RenderSize), 4, 4 ); this.formattedText.SetForegroundBrush ( IsEnabled ? Foreground : SystemColors.ControlDarkBrush ); Point textPoint = new Point(2, 2); switch(HorizontalContentAlignment) { case HorizontalAlignment.Left : textPoint.X += Padding.Left; break; case HorizontalAlignment.Right : textPoint.X += RenderSize.Width - this.formattedText.Width - Padding.Right; break; case HorizontalAlignment.Center : case HorizontalAlignment.Stretch : textPoint.X += (RenderSize.Width - this.formattedText.Width - Padding.Left - Padding.Right) / 2; break; } switch(VerticalContentAlignment) { case VerticalAlignment.Top : textPoint.Y += Padding.Top; break; case VerticalAlignment.Bottom : textPoint.Y += RenderSize.Height - this.formattedText.Height - Padding.Bottom; break; case VerticalAlignment.Center : case VerticalAlignment.Stretch : textPoint.Y += (RenderSize.Height - this.formattedText.Height - Padding.Top - Padding.Bottom) / 2; break; } drawingContext.DrawText(this.formattedText, textPoint); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<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="EventManager 클래스 : RegisterRoutedEvent 정적 메소드를 사용해 라우팅 이벤트 만들기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <local:KnockButton x:Name="button" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="30" Text="테스트" /> </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.button.PreviewKnock += button_PreviewKnock; this.button.Knock += button_Knock; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 버튼 프리뷰 노크시 처리하기 - button_PreviewKnock(sender, e) /// <summary> /// 버튼 프리뷰 노크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button_PreviewKnock(object sender, RoutedEventArgs e) { MessageBox.Show(this, "버튼 프리뷰 노크 이벤트"); } #endregion #region 버튼 노크시 처리하기 - button_Knock(sender, e) /// <summary> /// 버튼 노크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button_Knock(object sender, RoutedEventArgs e) { MessageBox.Show(this, "버튼 노크 이벤트"); } #endregion } } |