■ FormattedText 클래스를 사용하는 방법을 보여준다.
▶ CustomElement.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 |
using System.Globalization; using System.Windows; using System.Windows.Media; namespace TestProject { /// <summary> /// 커스텀 엘리먼트 /// </summary> public class CustomElement : FrameworkElement { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 텍스트 /// </summary> private readonly string text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 렌더링시 처리하기 - OnRender(drawingContext) /// <summary> /// 렌더링시 처리하기 /// </summary> /// <param name="drawingContext">드로잉 컨텍스트</param> protected override void OnRender(DrawingContext drawingContext) { FormattedText formattedText = new FormattedText ( this.text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 32, Brushes.Black ); formattedText.MaxTextWidth = ActualWidth - 10; formattedText.MaxTextHeight = ActualHeight - 10; formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 5); formattedText.SetFontWeight(FontWeights.Bold, 6, 11); formattedText.SetForegroundBrush ( new LinearGradientBrush ( Colors.Orange, Colors.Teal, 90.0 ), 6, 11 ); formattedText.SetFontStyle(FontStyles.Italic, 28, 28); drawingContext.DrawText(formattedText, new Point(10, 10)); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<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 HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1" BorderBrush="Black" Width="400" Height="400"> <local:CustomElement /> </Border> </Window> |