■ TextFormatter 클래스의 FormatLine 메소드를 사용해 문서 서식과 내용 표시를 위한 텍스트 라인을 만드는 방법을 보여준다.
▶ CustomTextRunProperties.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 |
using System.Globalization; using System.Windows; using System.Windows.Media; using System.Windows.Media.TextFormatting; namespace TestProject { /// <summary> /// 커스텀 텍스트 런 속성 /// </summary> public class CustomTextRunProperties : TextRunProperties { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문화 정보 - CultureInfo /// <summary> /// 문화 정보 /// </summary> public override CultureInfo CultureInfo { get { return CultureInfo.CurrentUICulture; } } #endregion #region 배경 브러시 - BackgroundBrush /// <summary> /// 배경 브러시 /// </summary> public override Brush BackgroundBrush { get { return Brushes.Transparent; } } #endregion #region 전경 브러시 - ForegroundBrush /// <summary> /// 전경 브러시 /// </summary> public override Brush ForegroundBrush { get { return Brushes.Teal; } } #endregion #region 타입 페이스 - Typeface /// <summary> /// 타입 페이스 /// </summary> public override Typeface Typeface { get { return new Typeface("Narkism"); } } #endregion #region 폰트 힌트 EM 크기 - FontHintingEmSize /// <summary> /// 폰트 힌트 EM 크기 /// </summary> public override double FontHintingEmSize { get { return 24.0; } } #endregion #region 폰트 렌더링 EM 크기 - FontRenderingEmSize /// <summary> /// 폰트 렌더링 EM 크기 /// </summary> public override double FontRenderingEmSize { get { return 24.0; } } #endregion #region 텍스트 데코레이션 컬렉션 - TextDecorations /// <summary> /// 텍스트 데코레이션 컬렉션 /// </summary> public override TextDecorationCollection TextDecorations { get { return null; } } #endregion #region 텍스트 효과 컬렉션 - TextEffects /// <summary> /// 텍스트 효과 컬렉션 /// </summary> public override TextEffectCollection TextEffects { get { return null; } } #endregion } } |
▶ CustomTextParagraphProperties.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 |
using System.Windows; using System.Windows.Media.TextFormatting; namespace TestProject { /// <summary> /// 커스텀 텍스트 문단 속성 /// </summary> public class CustomTextParagraphProperties : TextParagraphProperties { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 커스텀 텍스트 런 속성 /// </summary> private CustomTextRunProperties customTextRunProperties = new CustomTextRunProperties(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 디폴트 텍스트 런 속성 - DefaultTextRunProperties /// <summary> /// 디폴트 텍스트 런 속성 /// </summary> public override TextRunProperties DefaultTextRunProperties { get { return this.customTextRunProperties; } } #endregion #region 문단에서 첫번째 라인 - FirstLineInParagraph /// <summary> /// 문단에서 첫번째 라인 /// </summary> public override bool FirstLineInParagraph { get { return true; } } #endregion #region 플로우 방향 - FlowDirection /// <summary> /// 플로우 방향 /// </summary> public override FlowDirection FlowDirection { get { return FlowDirection.LeftToRight; } } #endregion #region 들여쓰기 - Indent /// <summary> /// 들여쓰기 /// </summary> public override double Indent { get { return 0; } } #endregion #region 라인 높이 - LineHeight /// <summary> /// 라인 높이 /// </summary> public override double LineHeight { get { return 24; } } #endregion #region 텍스트 정렬 - TextAlignment /// <summary> /// 텍스트 정렬 /// </summary> public override TextAlignment TextAlignment { get { return TextAlignment.Left; } } #endregion #region 텍스트 마커 속성 - TextMarkerProperties /// <summary> /// 텍스트 마커 속성 /// </summary> public override TextMarkerProperties TextMarkerProperties { get { return null; } } #endregion #region 텍스트 랩핑 - TextWrapping /// <summary> /// 텍스트 랩핑 /// </summary> public override TextWrapping TextWrapping { get { return TextWrapping.Wrap; } } #endregion } } |
▶ CustomTextSource.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 |
using System; using System.Windows.Media.TextFormatting; namespace TestProject { /// <summary> /// 커스텀 텍스트 소스 /// </summary> public class CustomTextSource : TextSource { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 텍스트 /// </summary> private string text; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 텍스트 - Text /// <summary> /// 텍스트 /// </summary> public string Text { get { return this.text; } set { this.text = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 텍스트 런 구하기 - GetTextRun(textSourceCharacterIndex) /// <summary> /// 텍스트 런 구하기 /// </summary> /// <param name="textSourceCharacterIndex">텍스트 소스 문자 인덱스</param> /// <returns>텍스트 런</returns> public override TextRun GetTextRun(int textSourceCharacterIndex) { if(textSourceCharacterIndex < 0) { throw new ArgumentOutOfRangeException ( "textSourceCharacterIndex", "Value must be greater than 0." ); } if(textSourceCharacterIndex >= this.text.Length) { return new TextEndOfParagraph(1); } if(textSourceCharacterIndex < this.text.Length) { TextCharacters textCharacters = new TextCharacters ( this.text, textSourceCharacterIndex, this.text.Length - textSourceCharacterIndex, new CustomTextRunProperties() ); return textCharacters; } return new TextEndOfParagraph(1); } #endregion #region 선행 텍스트 구하기 - GetPrecedingText(textSourceCharacterIndexLimit) /// <summary> /// 선행 텍스트 구하기 /// </summary> /// <param name="textSourceCharacterIndexLimit">텍스트 소스 문자 인덱스 제한</param> /// <returns>텍스트 범위</returns> public override TextSpan<CultureSpecificCharacterBufferRange> GetPrecedingText(int textSourceCharacterIndexLimit) { throw new Exception("The method or operation is not implemented."); } #endregion #region 텍스트 소스 문자 인덱스에서 텍스트 효과 문자 인덱스 구하기 - GetTextEffectCharacterIndexFromTextSourceCharacterIndex(textSourceCharacterIndex) /// <summary> /// 텍스트 소스 문자 인덱스에서 텍스트 효과 문자 인덱스 구하기 /// </summary> /// <param name="textSourceCharacterIndex">텍스트 소스 문자 인덱스</param> /// <returns>텍스트 소스 문자 인덱스에서 텍스트 효과 문자 인덱스</returns> public override int GetTextEffectCharacterIndexFromTextSourceCharacterIndex(int textSourceCharacterIndex) { throw new Exception("The method or operation is not implemented."); } #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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TextFormatter 클래스 : FormatLine 메소드를 사용해 문서 서식과 내용 표시를 위한 텍스트 라인 만들기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBox Name="sourceTextBox" Margin="10" Width="600" Height="50" AcceptsReturn="True" TextWrapping="WrapWithOverflow"> The quick red fox jumps over the lazy brown dog. </TextBox> <StackPanel HorizontalAlignment="Center" Margin="10" Orientation="Horizontal"> <Button Name="button1" Margin="10" Width="100" Height="30" Content="테스트 1" /> <Button Name="button2" Margin="10" Width="100" Height="30" Content="테스트 2" /> <Button Name="button3" Margin="10" Width="100" Height="30" Content="테스트 3" /> </StackPanel> <Border Name="border" Margin="10" Width="600" Height="300" BorderBrush="Black" BorderThickness="1"> <Rectangle Name="r"> <Rectangle.Fill> <DrawingBrush x:Name="drawingBrush" Stretch="None" AlignmentX="Left" AlignmentY="Top"> <DrawingBrush.Drawing> <DrawingGroup x:Name="drawingGroup" /> </DrawingBrush.Drawing> </DrawingBrush> </Rectangle.Fill> </Rectangle> </Border> </StackPanel> </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 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 |
using System; using System.Windows; using System.Windows.Media; using System.Windows.Media.TextFormatting; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 커스텀 텍스트 소스 /// </summary> private CustomTextSource textSource; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.button1.Click += button1_Click; this.button2.Click += button2_Click; this.button3.Click += button3_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 버튼 1 클릭시 처리하기 - button1_Click(sender, e) /// <summary> /// 버튼 1 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button1_Click(object sender, EventArgs e) { if(this.textSource == null) { this.textSource = new CustomTextSource(); } UpdateFormattedText1(); } #endregion #region 버튼 2 클릭시 처리하기 - button2_Click(sender, e) /// <summary> /// 버튼 2 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button2_Click(object sender, EventArgs e) { if(this.textSource == null) { this.textSource = new CustomTextSource(); } UpdateFormattedText2(); } #endregion #region 버튼 3 클릭시 처리하기 - button3_Click(sender, e) /// <summary> /// 버튼 3 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button3_Click(object sender, EventArgs e) { if(this.textSource == null) { this.textSource = new CustomTextSource(); } UpdateFormattedText3(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 포맷 텍스트 갱신하기 1 - UpdateFormattedText1() /// <summary> /// 포맷 텍스트 갱신하기 1 /// </summary> private void UpdateFormattedText1() { int firstCharacterIndex = 0; Point originPoint = new Point(0, 0); DrawingContext drawingContext = this.drawingGroup.Open(); this.textSource.Text = this.sourceTextBox.Text; TextFormatter formatter = TextFormatter.Create(); CustomTextParagraphProperties textParagraphProperties = new CustomTextParagraphProperties(); while(firstCharacterIndex < this.textSource.Text.Length) { using ( TextLine textLine = formatter.FormatLine ( this.textSource, firstCharacterIndex, this.border.ActualWidth, textParagraphProperties, null ) ) { textLine.Draw(drawingContext, originPoint, InvertAxes.None); firstCharacterIndex += textLine.Length; originPoint.Y += textLine.Height; } } drawingContext.Close(); this.drawingBrush.Drawing = this.drawingGroup; } #endregion #region 포맷 텍스트 갱신하기 2 - UpdateFormattedText2() /// <summary> /// 포맷 텍스트 갱신하기 2 /// </summary> private void UpdateFormattedText2() { DrawingContext drawingContext = this.drawingGroup.Open(); this.textSource.Text = this.sourceTextBox.Text; TextFormatter formatter = TextFormatter.Create(); CustomTextParagraphProperties textParagraphProperties = new CustomTextParagraphProperties(); using ( TextLine textLine = formatter.FormatLine ( this.textSource, 0, 600, textParagraphProperties, null ) ) { textLine.Draw(drawingContext, new Point(0, 0), InvertAxes.None); } drawingContext.Close(); } #endregion #region 포맷 텍스트 갱신하기 3 - UpdateFormattedText3() /// <summary> /// 포맷 텍스트 갱신하기 3 /// </summary> private void UpdateFormattedText3() { int firstCharacterIndex = 0; Point originPoint = new Point(0, 0); DrawingContext drawingContext = this.drawingGroup.Open(); this.textSource.Text = this.sourceTextBox.Text; TextFormatter formatter = TextFormatter.Create(); CustomTextParagraphProperties textParagraphProperties = new CustomTextParagraphProperties(); MinMaxParagraphWidth minMaxParaWidth = formatter.FormatMinMaxParagraphWidth ( this.textSource, 0, textParagraphProperties ); while(firstCharacterIndex < textSource.Text.Length) { using ( TextLine textLine1 = formatter.FormatLine ( this.textSource, firstCharacterIndex, minMaxParaWidth.MinWidth, textParagraphProperties, null ) ) { textLine1.Draw(drawingContext, originPoint, InvertAxes.None); firstCharacterIndex += textLine1.Length; originPoint.Y += textLine1.Height; } } TextLine textLine2 = formatter.FormatLine ( textSource, 0, minMaxParaWidth.MaxWidth, textParagraphProperties, null ); textLine2.Draw(drawingContext, originPoint, InvertAxes.None); drawingContext.Close(); this.drawingBrush.Drawing = this.drawingGroup; } #endregion } } |