■ FrameworkElement 클래스에서 GDI+를 사용해 텍스트 블럭을 만드는 방법을 보여준다.
▶ BitmapElement.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 |
using System; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 비트맵 엘리먼트 /// </summary> public class BitmapElement : UIElement { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 비트맵 실패시 - BitmapFailed /// <summary> /// 비트맵 실패시 /// </summary> public event EventHandler<ExceptionEventArgs> BitmapFailed; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 소스 속성 - SourceProperty /// <summary> /// 소스 속성 /// </summary> public static readonly DependencyProperty SourceProperty = DependencyProperty.Register ( "Source", typeof(BitmapSource), typeof(BitmapElement), new FrameworkPropertyMetadata ( null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(SourcePropertyChangedCallback) ) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 비트맵 소스 다운로드 완료시 이벤트 핸들러 /// </summary> private EventHandler bitmapSourceDownloadCompletedEventHandler; /// <summary> /// 비트맵 소스 다운로드 실패시 이벤트 핸들러 /// </summary> private EventHandler<ExceptionEventArgs> bitmapSourceDownloadFailedEventHandler; /// <summary> /// 픽셀 오프셋 /// </summary> private Point pixelOffset; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 소스 - Source /// <summary> /// 소스 /// </summary> public BitmapSource Source { get { return (BitmapSource)GetValue(SourceProperty); } set { SetValue(SourceProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - BitmapElement() /// <summary> /// 생성자 /// </summary> public BitmapElement() { this.bitmapSourceDownloadCompletedEventHandler = new EventHandler(bitmapSource_DownloadCompleted); this.bitmapSourceDownloadFailedEventHandler = new EventHandler<ExceptionEventArgs>(bitmapSource_DownloadFailed); LayoutUpdated += UIElement_LayoutUpdated; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 소스 속성 변경시 콜백 처리하기 - SourcePropertyChangedCallback(d, e) /// <summary> /// 소스 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void SourcePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { BitmapElement bitmapElement = (BitmapElement)d; BitmapSource oldSource = (BitmapSource)e.OldValue; BitmapSource newSource = (BitmapSource)e.NewValue; if((oldSource != null) && (bitmapElement.bitmapSourceDownloadCompletedEventHandler != null) && !oldSource.IsFrozen && (oldSource is BitmapSource)) { oldSource.DownloadCompleted -= bitmapElement.bitmapSourceDownloadCompletedEventHandler; oldSource.DownloadFailed -= bitmapElement.bitmapSourceDownloadFailedEventHandler; } if((newSource != null) && (newSource is BitmapSource) && !newSource.IsFrozen) { newSource.DownloadCompleted += bitmapElement.bitmapSourceDownloadCompletedEventHandler; newSource.DownloadFailed += bitmapElement.bitmapSourceDownloadFailedEventHandler; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Protected #region 측정하기 (코어) - MeasureCore(availableSize) /// <summary> /// 측정하기 (코어) /// </summary> /// <param name="availableSize">이용 가능한 크기</param> /// <returns>측정 크기</returns> protected override Size MeasureCore(Size availableSize) { Size measureSize = new Size(); BitmapSource bitmapSource = Source; if(bitmapSource != null) { PresentationSource presentationSource = PresentationSource.FromVisual(this); if(presentationSource != null) { Matrix fromDeviceMartix = presentationSource.CompositionTarget.TransformFromDevice; Vector pixelSizeVector = new Vector(bitmapSource.PixelWidth, bitmapSource.PixelHeight); Vector measureSizeVector = fromDeviceMartix.Transform(pixelSizeVector); measureSize = new Size(measureSizeVector.X, measureSizeVector.Y); } } return measureSize; } #endregion #region 렌더링시 처리하기 - OnRender(context) /// <summary> /// 렌더링시 처리하기 /// </summary> /// <param name="context">드로잉 컨텍스트</param> protected override void OnRender(DrawingContext context) { this.pixelOffset = GetPixelOffset(); BitmapSource bitmapSource = Source; if(bitmapSource != null) { context.DrawImage(bitmapSource, new Rect(this.pixelOffset, DesiredSize)); } } #endregion //////////////////////////////////////////////////////////////////////////////// Private ////////////////////////////////////////////////////////////////////// Event #region UI 엘리먼트 레이아웃 업데이트시 처리하기 - UIElement_LayoutUpdated(sender, e) /// <summary> /// UI 엘리먼트 레이아웃 업데이트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UIElement_LayoutUpdated(object sender, EventArgs e) { Point pixelOffset = GetPixelOffset(); if(!IsAreClosed(pixelOffset, this.pixelOffset)) { InvalidateVisual(); } } #endregion #region 비트맵 소스 다운로드 완료시 처리하기 - bitmapSource_DownloadCompleted(sender, e) /// <summary> /// 비트맵 소스 다운로드 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bitmapSource_DownloadCompleted(object sender, EventArgs e) { InvalidateMeasure(); InvalidateVisual(); } #endregion #region 비트맵 소스 다운로드 실패시 처리하기 - bitmapSource_DownloadFailed(sender, e) /// <summary> /// 비트맵 소스 다운로드 실패시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bitmapSource_DownloadFailed(object sender, ExceptionEventArgs e) { Source = null; BitmapFailed(this, e); } #endregion ////////////////////////////////////////////////////////////////////// Function #region 비주얼 변환 매트릭스 구하기 - GetVisualTransformMatrix(visual) /// <summary> /// 비주얼 변환 매트릭스 구하기 /// </summary> /// <param name="visual">비주얼</param> /// <returns>비주얼 변환 매트릭스</returns> private Matrix GetVisualTransformMatrix(Visual visual) { if(visual != null) { Matrix matrix = Matrix.Identity; Transform transform = VisualTreeHelper.GetTransform(visual); if(transform != null) { Matrix transformMatrix = transform.Value; matrix = Matrix.Multiply(matrix, transformMatrix); } Vector offsetVector = VisualTreeHelper.GetOffset(visual); matrix.Translate(offsetVector.X, offsetVector.Y); return matrix; } return Matrix.Identity; } #endregion #region 비주얼 변환 적용하기 - ApplyVisualTransform(point, visual, inverse, throwException, success) /// <summary> /// 비주얼 변환 적용하기 /// </summary> /// <param name="point">포인트</param> /// <param name="visual">비주얼</param> /// <param name="inverse">반전 여부</param> /// <param name="throwException">예외 발생 여부</param> /// <param name="success">성공 여부</param> /// <returns>비주얼 변환 포인트</returns> private Point ApplyVisualTransform(Point point, Visual visual, bool inverse, bool throwException, out bool success) { success = true; if(visual != null) { Matrix matrix = GetVisualTransformMatrix(visual); if(inverse) { if(!throwException && !matrix.HasInverse) { success = false; return new Point(0, 0); } matrix.Invert(); } point = matrix.Transform(point); } return point; } #endregion #region 비주얼 변환 적용하기 - ApplyVisualTransform(point, visual, inverse) /// <summary> /// 비주얼 변환 적용하기 /// </summary> /// <param name="point">포인트</param> /// <param name="visual">비주얼</param> /// <param name="inverse">반전 여부</param> /// <returns>비주얼 변환 포인트</returns> private Point ApplyVisualTransform(Point point, Visual visual, bool inverse) { return ApplyVisualTransform(point, visual, inverse, true, out _); } #endregion #region 픽셀 오프셋 구하기 - GetPixelOffset() /// <summary> /// 픽셀 오프셋 구하기 /// </summary> /// <returns>픽셀 오프셋</returns> private Point GetPixelOffset() { Point pixelOffset = new Point(); PresentationSource presentationSource = PresentationSource.FromVisual(this); if(presentationSource != null) { Visual rootVisual = presentationSource.RootVisual; pixelOffset = TransformToAncestor(rootVisual).Transform(pixelOffset); pixelOffset = ApplyVisualTransform(pixelOffset, rootVisual, false); pixelOffset = presentationSource.CompositionTarget.TransformToDevice.Transform(pixelOffset); pixelOffset.X = Math.Round(pixelOffset.X); pixelOffset.Y = Math.Round(pixelOffset.Y); pixelOffset = presentationSource.CompositionTarget.TransformFromDevice.Transform(pixelOffset); pixelOffset = ApplyVisualTransform(pixelOffset, rootVisual, true); pixelOffset = rootVisual.TransformToDescendant(this).Transform(pixelOffset); } return pixelOffset; } #endregion #region 호 폐쇄 여부 구하기 - IsAreClosed(value1, value2) /// <summary> /// 호 폐쇄 여부 구하기 /// </summary> /// <param name="value1">값 1</param> /// <param name="value2">값 2</param> /// <returns>호 폐쇄 여부</returns> private bool IsAreClosed(double value1, double value2) { if(value1 == value2) { return true; } double delta = value1 - value2; return (delta < 1.53E-06) && (delta > -1.53E-06); } #endregion #region 호 폐쇄 여부 구하기 - IsAreClosed(point1, point2) /// <summary> /// 호 폐쇄 여부 구하기 /// </summary> /// <param name="point1">포인트 1</param> /// <param name="point2">포인트 2</param> /// <returns>호 폐쇄 여부</returns> private bool IsAreClosed(Point point1, Point point2) { return IsAreClosed(point1.X, point2.X) && IsAreClosed(point1.Y, point2.Y); } #endregion } } |
▶ GDITextQuality.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 |
namespace TestProject { /// <summary> /// GDI 텍스트 품질 /// </summary> public enum GDITextQuality { /// <summary> /// SystemDefault /// </summary> /// <remarks> /// 각 문자는 시스템 기본 렌더링 힌트와 함께 해당 글리프 비트맵을 사용하여 그려집니다. /// 텍스트는 사용자가 시스템에 대해 선택한 글꼴 다듬기 설정을 사용하여 그려집니다. /// </remarks> SystemDefault, /// <summary> /// SingleBitPerPixelGridFit /// </summary> /// <remarks> /// 각 문자는 해당 글리프 비트맵을 사용하여 그려집니다. /// 힌팅은 줄기와 곡률의 문자 모양을 개선하는 데 사용됩니다. /// </remarks> SingleBitPerPixelGridFit, /// <summary> /// SingleBitPerPixel /// </summary> /// <remarks> /// 각 문자는 해당 글리프 비트맵을 사용하여 그려집니다. /// 힌트는 사용되지 않습니다. /// </remarks> SingleBitPerPixel, /// <summary> /// AntiAliasGridFit /// </summary> /// <remarks> /// 각 문자는 힌트가 있는 앤티앨리어싱된 글리프 비트맵을 사용하여 그려집니다. /// 앤티앨리어싱으로 인해 훨씬 더 나은 품질을 제공하지만 더 높은 성능 비용이 듭니다. /// </remarks> AntiAliasGridFit, /// <summary> /// AntiAlias /// </summary> /// <remarks> /// 각 문자는 힌트 없이 앤티앨리어싱된 글리프 비트맵을 사용하여 그려집니다. /// 앤티 앨리어싱으로 인해 더 나은 품질. /// 힌팅이 꺼져 있기 때문에 줄기 너비 차이가 눈에 띌 수 있습니다. /// </remarks> AntiAlias, /// <summary> /// ClearTypeGridFit /// </summary> /// <remarks> /// 각 문자는 힌트와 함께 해당 글리프 ClearType 비트맵을 사용하여 그려집니다. /// 최고 품질의 설정. ClearType 글꼴 기능을 활용하는 데 사용됩니다. /// </remarks> ClearTypeGridFit } } |
▶ GDITextBlock.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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 |
using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Media; namespace TestProject { /// <summary> /// GDI 텍스트 블럭 /// </summary> public class GDITextBlock : FrameworkElement { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 텍스트 속성 - TextProperty /// <summary> /// 텍스트 속성 /// </summary> public static readonly DependencyProperty TextProperty = TextBlock.TextProperty.AddOwner ( typeof(GDITextBlock), new FrameworkPropertyMetadata { AffectsMeasure = true } ); #endregion #region 텍스트 품질 속성 - TextQualityProperty /// <summary> /// 텍스트 품질 속성 /// </summary> public static readonly DependencyProperty TextQualityProperty = DependencyProperty.Register ( "TextQuality", typeof(GDITextQuality), typeof(GDITextBlock), new FrameworkPropertyMetadata { DefaultValue = GDITextQuality.SingleBitPerPixelGridFit, AffectsMeasure = true, CoerceValueCallback = TextQualityProperty_CoerceValueCallback } ); #endregion #region 텍스트 정렬 속성 - TextAlignmentProperty /// <summary> /// 텍스트 정렬 속성 /// </summary> public static readonly DependencyProperty TextAlignmentProperty = Block.TextAlignmentProperty.AddOwner ( typeof(GDITextBlock), new FrameworkPropertyMetadata { AffectsMeasure = true, CoerceValueCallback = TextAlignmentProperty_CoerceValueCallback } ); #endregion #region 텍스트 래핑 속성 - TextWrappingProperty /// <summary> /// 텍스트 래핑 속성 /// </summary> public static readonly DependencyProperty TextWrappingProperty = TextBlock.TextWrappingProperty.AddOwner ( typeof(GDITextBlock), new FrameworkPropertyMetadata { AffectsMeasure = true, CoerceValueCallback = TextWrappingProperty_CoerceValueCallback } ); #endregion #region 텍스트 트리밍 속성 - TextTrimmingProperty /// <summary> /// 텍스트 트리밍 속성 /// </summary> public static readonly DependencyProperty TextTrimmingProperty = TextBlock.TextTrimmingProperty.AddOwner ( typeof(GDITextBlock), new FrameworkPropertyMetadata { AffectsMeasure = true } ); #endregion #region 폰트 패밀리 속성 - FontFamilyProperty /// <summary> /// 폰트 패밀리 속성 /// </summary> public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner ( typeof(GDITextBlock), new FrameworkPropertyMetadata { AffectsMeasure = true } ); #endregion #region 폰트 스타일 속성 - FontStyleProperty /// <summary> /// 폰트 스타일 속성 /// </summary> public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner ( typeof(GDITextBlock), new FrameworkPropertyMetadata { AffectsMeasure = true, CoerceValueCallback = FontStyleProperty_CoerceValueCallback } ); #endregion #region 폰트 가중치 속성 - FontWeightProperty /// <summary> /// 폰트 가중치 속성 /// </summary> public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner ( typeof(GDITextBlock), new FrameworkPropertyMetadata { AffectsMeasure = true, CoerceValueCallback = FontWeightProperty_CoerceValueCallback } ); #endregion #region 폰트 크기 속성 - FontSizeProperty /// <summary> /// 폰트 크기 속성 /// </summary> public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner ( typeof(GDITextBlock), new FrameworkPropertyMetadata { AffectsMeasure = true } ); #endregion #region 전경색 속성 - ForegroundProperty /// <summary> /// 전경색 속성 /// </summary> public static readonly DependencyProperty ForegroundProperty = TextElement.ForegroundProperty.AddOwner ( typeof(GDITextBlock), new FrameworkPropertyMetadata { AffectsMeasure = true, CoerceValueCallback = ForegroundProperty_CoerceValueCallback } ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 비트맵 엘리먼트 /// </summary> private BitmapElement bitmapElement; /// <summary> /// 그리기 브러시 /// </summary> private System.Drawing.Brush drawingBrush; /// <summary> /// 그리기 폰트 /// </summary> private System.Drawing.Font drawingFont; /// <summary> /// 그리기 문자열 포맷 /// </summary> private System.Drawing.StringFormat drawingStringFormat; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 텍스트 - Text /// <summary> /// 텍스트 /// </summary> public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } #endregion #region 텍스트 품질 - TextQuality /// <summary> /// 텍스트 품질 /// </summary> public GDITextQuality TextQuality { get { return (GDITextQuality)GetValue(TextQualityProperty); } set { SetValue(TextQualityProperty, value); } } #endregion #region 텍스트 정렬 - TextAlignment /// <summary> /// 텍스트 정렬 /// </summary> public TextAlignment TextAlignment { get { return (TextAlignment)GetValue(TextAlignmentProperty); } set { SetValue(TextAlignmentProperty, value); } } #endregion #region 텍스트 래핑 - TextWrapping /// <summary> /// 텍스트 래핑 /// </summary> public TextWrapping TextWrapping { get { return (TextWrapping)GetValue(TextWrappingProperty); } set { SetValue(TextWrappingProperty, value); } } #endregion #region 텍스트 트리밍 - TextTrimming /// <summary> /// 텍스트 트리밍 /// </summary> public TextTrimming TextTrimming { get { return (TextTrimming)GetValue(TextTrimmingProperty); } set { SetValue(TextTrimmingProperty, value); } } #endregion #region 폰트 패밀리 - FontFamily /// <summary> /// 폰트 패밀리 /// </summary> public FontFamily FontFamily { get { return (FontFamily)GetValue(FontFamilyProperty); } set { SetValue(FontFamilyProperty, value); } } #endregion #region 폰트 스타일 - FontStyle /// <summary> /// 폰트 스타일 /// </summary> public FontStyle FontStyle { get { return (FontStyle)GetValue(FontStyleProperty); } set { SetValue(FontStyleProperty, value); } } #endregion #region 폰트 가중치 - FontWeight /// <summary> /// 폰트 가중치 /// </summary> public FontWeight FontWeight { get { return (FontWeight)GetValue(FontWeightProperty); } set { SetValue(FontWeightProperty, value); } } #endregion #region 폰트 크기 - FontSize /// <summary> /// 폰트 크기 /// </summary> [TypeConverter(typeof(FontSizeConverter))] public double FontSize { get { return (double)GetValue(FontSizeProperty); } set { SetValue(FontSizeProperty, value); } } #endregion #region 전경색 - Foreground /// <summary> /// 전경색 /// </summary> public SolidColorBrush Foreground { get { return (SolidColorBrush)GetValue(ForegroundProperty); } set { SetValue(ForegroundProperty, value); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Proptected #region 비주얼 자식 수 - VisualChildrenCount /// <summary> /// 비주얼 자식 수 /// </summary> protected override int VisualChildrenCount { get { return 1; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 텍스트 품질 속성 값 강제 콜백 처리하기 - TextQualityProperty_CoerceValueCallback(d, baseValue) /// <summary> /// 텍스트 품질 속성 값 강제 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="baseValue">기본 값</param> /// <returns>처리 결과</returns> private static object TextQualityProperty_CoerceValueCallback(DependencyObject d, object baseValue) { if(!Enum.IsDefined(typeof(GDITextQuality), baseValue)) { baseValue = TextQualityProperty.DefaultMetadata.DefaultValue; } return baseValue; } #endregion #region 텍스트 정렬 속성 값 강제 콜백 처리하기 - TextAlignmentProperty_CoerceValueCallback(d, baseValue) /// <summary> /// 텍스트 정렬 속성 값 강제 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="baseValue">기본 값</param> /// <returns>처리 결과</returns> private static object TextAlignmentProperty_CoerceValueCallback(DependencyObject d, object baseValue) { if(baseValue.Equals(TextAlignment.Justify)) { baseValue = TextAlignmentProperty.DefaultMetadata.DefaultValue; } return baseValue; } #endregion #region 텍스트 래핑 속성 값 강제 콜백 처리하기 - TextWrappingProperty_CoerceValueCallback(d, baseValue) /// <summary> /// 텍스트 래핑 속성 값 강제 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="baseValue">기본 값</param> /// <returns>처리 결과</returns> private static object TextWrappingProperty_CoerceValueCallback(DependencyObject d, object baseValue) { if(baseValue.Equals(TextWrapping.WrapWithOverflow)) { baseValue = TextWrapping.Wrap; } return baseValue; } #endregion #region 폰트 스타일 속성 값 강제 콜백 처리하기 - FontStyleProperty_CoerceValueCallback(d, baseValue) /// <summary> /// 폰트 스타일 속성 값 강제 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="baseValue">기본 값</param> /// <returns>처리 결과</returns> private static object FontStyleProperty_CoerceValueCallback(DependencyObject d, object baseValue) { if(baseValue.Equals(FontStyles.Oblique)) { baseValue = TextAlignmentProperty.DefaultMetadata.DefaultValue; } return baseValue; } #endregion #region 폰트 가중치 속성 값 강제 콜백 처리하기 - FontWeightProperty_CoerceValueCallback(d, baseValue) /// <summary> /// 폰트 가중치 속성 값 강제 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="baseValue">기본 값</param> /// <returns>처리 결과</returns> private static object FontWeightProperty_CoerceValueCallback(DependencyObject d, object baseValue) { if(!baseValue.Equals(FontWeights.Bold) && !baseValue.Equals(FontWeights.Regular) && !baseValue.Equals(FontWeights.Normal)) { baseValue = TextAlignmentProperty.DefaultMetadata.DefaultValue; } return baseValue; } #endregion #region 전경색 속성 값 강제 콜백 처리하기 - ForegroundProperty_CoerceValueCallback(d, baseValue) /// <summary> /// 전경색 속성 값 강제 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="baseValue">기본 값</param> /// <returns>처리 결과</returns> private static object ForegroundProperty_CoerceValueCallback(DependencyObject d, object baseValue) { if(!(baseValue is SolidColorBrush)) { baseValue = ForegroundProperty.DefaultMetadata.DefaultValue; } return baseValue; } #endregion #region 비트맵 소스 구하기 - GetBitmapSource(bitmap) /// <summary> /// 비트맵 소스 구하기 /// </summary> /// <param name="bitmap">비트맵</param> /// <returns>비트맵 소스</returns> private static System.Windows.Media.Imaging.BitmapSource GetBitmapSource(System.Drawing.Bitmap bitmap) { System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height); System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits ( rectangle, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat ); System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Media.Imaging.BitmapSource.Create ( bitmap.Width, bitmap.Height, bitmap.HorizontalResolution, bitmap.VerticalResolution, PixelFormats.Pbgra32, null, bitmapData.Scan0, bitmapData.Stride * bitmap.Height, bitmapData.Stride ); bitmap.UnlockBits(bitmapData); return bitmapSource; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Protected #region 초기화시 처리하기 - OnInitialized(e) /// <summary> /// 초기화시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnInitialized(EventArgs e) { this.bitmapElement = new BitmapElement(); AddVisualChild(this.bitmapElement); base.OnInitialized(e); } #endregion #region 비주얼 자식 구하기 - GetVisualChild(index) /// <summary> /// 비주얼 자식 구하기 /// </summary> /// <param name="index"></param> /// <returns>비주얼 자식</returns> protected override Visual GetVisualChild(int index) { return this.bitmapElement; } #endregion #region 측정하기 (오버라이드) - MeasureOverride(availableSize) /// <summary> /// 측정하기 (오버라이드) /// </summary> /// <param name="availableSize">이용 가능한 크기</param> /// <returns>측정 크기</returns> protected override Size MeasureOverride(Size availableSize) { Matrix dpiMatrix = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice; float dpiX = (float)dpiMatrix.M11; float dpiY = (float)dpiMatrix.M22; CreateDrawingObjects(dpiX); System.Drawing.Size size = new System.Drawing.Size(); using(System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(1, 1)) { using(System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap)) { graphics.TextRenderingHint = (System.Drawing.Text.TextRenderingHint)TextQuality; System.Drawing.SizeF boundarySize = new System.Drawing.SizeF ( (float)availableSize.Width * dpiX, (float)availableSize.Height * dpiY ); boundarySize = graphics.MeasureString(Text, this.drawingFont, boundarySize, this.drawingStringFormat); size = new System.Drawing.Size ( (int)Math.Ceiling(boundarySize.Width ), (int)Math.Ceiling(boundarySize.Height) ); } } using(System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(size.Width, size.Height)) { using(System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap)) { graphics.TextRenderingHint = (System.Drawing.Text.TextRenderingHint)TextQuality; Color color = Foreground.Color; graphics.DrawString ( Text, this.drawingFont, this.drawingBrush, new System.Drawing.RectangleF(0, 0, size.Width, size.Height), this.drawingStringFormat ); this.bitmapElement.Source = GetBitmapSource(bitmap); this.bitmapElement.InvalidateMeasure(); this.bitmapElement.InvalidateVisual(); } } this.bitmapElement.Measure(availableSize); return this.bitmapElement.DesiredSize; } #endregion #region 배열하기 (오버라이드) - ArrangeOverride(finalSize) /// <summary> /// 배열하기 (오버라이드) /// </summary> /// <param name="finalSize">최종 크기</param> /// <returns>배열 크기</returns> protected override Size ArrangeOverride(Size finalSize) { this.bitmapElement.Arrange(new Rect(finalSize)); return finalSize; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 그리기 객체 리소스 해제하기 - DisposeDrawingObjects() /// <summary> /// 그리기 객체 리소스 해제하기 /// </summary> private void DisposeDrawingObjects() { if(this.drawingBrush != null) { this.drawingBrush.Dispose(); } if(this.drawingFont != null) { this.drawingFont.Dispose(); } if(this.drawingStringFormat != null) { this.drawingStringFormat.Dispose(); } } #endregion #region 그리기 객체 생성하기 - CreateDrawingObjects(dpi) /// <summary> /// 그리기 객체 생성하기 /// </summary> /// <param name="dpi">DPI</param> private void CreateDrawingObjects(float dpi) { DisposeDrawingObjects(); Color color = Foreground.Color; this.drawingBrush = new System.Drawing.SolidBrush ( System.Drawing.Color.FromArgb ( (int)color.A, (int)color.R, (int)color.G, (int)color.B ) ); System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular; if(FontWeight == FontWeights.Bold) { style |= System.Drawing.FontStyle.Bold; } if(FontStyle == FontStyles.Italic) { style |= System.Drawing.FontStyle.Italic; } this.drawingFont = new System.Drawing.Font ( FontFamily.Source, (float)(FontSize * dpi), style, System.Drawing.GraphicsUnit.Pixel ); this.drawingStringFormat = new System.Drawing.StringFormat(); switch(TextTrimming) { case TextTrimming.None : this.drawingStringFormat.Trimming = System.Drawing.StringTrimming.None; break; case TextTrimming.CharacterEllipsis : this.drawingStringFormat.Trimming = System.Drawing.StringTrimming.EllipsisCharacter; break; case TextTrimming.WordEllipsis : this.drawingStringFormat.Trimming = System.Drawing.StringTrimming.EllipsisWord; break; } this.drawingStringFormat.FormatFlags = System.Drawing.StringFormatFlags.LineLimit; if(TextWrapping == TextWrapping.NoWrap) { this.drawingStringFormat.FormatFlags |= System.Drawing.StringFormatFlags.NoWrap; } if(FlowDirection == FlowDirection.RightToLeft) { this.drawingStringFormat.FormatFlags |= System.Drawing.StringFormatFlags.DirectionRightToLeft; } switch(TextAlignment) { case TextAlignment.Left : this.drawingStringFormat.Alignment = System.Drawing.StringAlignment.Near; break; case TextAlignment.Center : this.drawingStringFormat.Alignment = System.Drawing.StringAlignment.Center; break; case TextAlignment.Right : this.drawingStringFormat.Alignment = System.Drawing.StringAlignment.Far; break; } } #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 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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="FrameworkElement 클래스 : GDI+를 사용해 텍스트 블럭 만들기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <sys:String x:Key="TextStringKey"> Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit, amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi sint, obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio, cumque nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. </sys:String> <Style x:Key="ColorComboBoxItemKey" TargetType="ComboBoxItem"> <Setter Property="Background" Value="{Binding Value}" /> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="FontSize" Value="16" /> <Setter Property="FontWeight" Value="Bold" /> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" Text="텍스트 크기" /> <TextBlock VerticalAlignment="Center" Margin="10 0 0 0" Text="{Binding ElementName=textSizeSlider, Path=Value}" /> <Slider Name="textSizeSlider" VerticalAlignment="Center" Margin="10 0 0 0" Width="100" Minimum="10" Maximum="30" SmallChange="1" LargeChange="2" Value="20" IsSnapToTickEnabled="True" /> <TextBlock VerticalAlignment="Center" Margin="10 0 0 0" Text="배경색" /> <ComboBox Name="backgroundColorComboBox" VerticalAlignment="Center" Margin="10 0 0 0" Width="100" SelectedValuePath="Value" DisplayMemberPath="Key" ItemContainerStyle="{StaticResource ColorComboBoxItemKey}" /> <TextBlock VerticalAlignment="Center" Margin="10 0 0 0" Text="전경색" /> <ComboBox Name="foregroundColorComboBox" Margin="10 0 0 0" Width="100" SelectedValuePath="Value" DisplayMemberPath="Key" ItemContainerStyle="{StaticResource ColorComboBoxItemKey}" /> </StackPanel> <Grid Grid.Row="1" Margin="0 10 0 0" Background="{Binding ElementName=backgroundColorComboBox, Path=SelectedValue}" TextBlock.Foreground="{Binding ElementName=foregroundColorComboBox, Path=SelectedValue}" TextBlock.FontFamily="Times New Roman" TextBlock.FontSize="{Binding ElementName=textSizeSlider, Path=Value}"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Content="GDI+" Grid.Column="0" /> <ScrollViewer Name="gdiScrollViewer" Grid.Row="1" Grid.Column="0" ScrollChanged="ScrollViewer_ScrollChanged"> <local:GDITextBlock TextWrapping="Wrap" TextTrimming="WordEllipsis" Text="{StaticResource TextStringKey}" /> </ScrollViewer> <Label Content="WPF" Grid.Column="2" /> <ScrollViewer Name="wpfScrollViewer" Grid.Row="1" Grid.Column="2" ScrollChanged="ScrollViewer_ScrollChanged"> <TextBlock TextWrapping="Wrap" TextTrimming="WordEllipsis" Text="{StaticResource TextStringKey}" /> </ScrollViewer> </Grid> </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 61 |
using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); KeyValuePair<string, SolidColorBrush>[] brushKeyValuePairArray = typeof(Brushes).GetProperties() .Where(pi => pi.PropertyType == typeof(SolidColorBrush)) .Select(pi => new KeyValuePair<string, SolidColorBrush>(pi.Name, (SolidColorBrush)pi.GetValue(null, null))) .ToArray(); this.backgroundColorComboBox.ItemsSource = brushKeyValuePairArray; this.backgroundColorComboBox.SelectedItem = brushKeyValuePairArray.First(c => c.Key == "Transparent"); this.foregroundColorComboBox.ItemsSource = brushKeyValuePairArray; this.foregroundColorComboBox.SelectedItem = brushKeyValuePairArray.First(c => c.Key == "Black"); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 스크롤 뷰어 스크롤 변경시 처리하기 - ScrollViewer_ScrollChanged(sender, e) /// <summary> /// 스크롤 뷰어 스크롤 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e) { ScrollViewer scrollViewer = (sender == this.wpfScrollViewer) ? this.gdiScrollViewer : this.wpfScrollViewer; scrollViewer.ScrollToVerticalOffset(e.VerticalOffset); } #endregion } } |