■ DrawingContext 클래스의 DrawGlyphRun 메소드를 사용해 텍스트를 그리는 방법을 보여준다.
▶ 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 |
using System; using System.Windows; using System.Windows.Media; namespace TestProject { /// <summary> /// 커스텀 엘리먼트 /// </summary> public class CustomElement : FrameworkElement { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 렌더링시 처리하기 - OnRender(drawingContext) /// <summary> /// 렌더링시 처리하기 /// </summary> /// <param name="drawingContext">드로잉 컨텍스트</param> protected override void OnRender(DrawingContext drawingContext) { GlyphRun glyphRun = new GlyphRun ( new GlyphTypeface(new Uri(@"C:\Windows\Fonts\Arial.ttf")), 0, false, 96, new ushort[] { 58, 82, 90 }, // Wow new Point(20, 100), new double[] { 80.0, 45.0, 0.0 }, null, null, null, null, null, null ); drawingContext.DrawGlyphRun(Brushes.Black, glyphRun); base.OnRender(drawingContext); } #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 |
<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="DrawingContext 클래스 : DrawGlyphRun 메소드를 사용해 텍스트 그리기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Border HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1" BorderBrush="DarkGray"> <local:CustomElement Width="300" Height="300" /> </Border> </Grid> </Window> |