2010-09-01 59 views
1

我正在使用TextFormatter製作WPF文本編輯器。我需要縮進一些段落,所以我使用TextParagraphProperties類中的Indent屬性。WPF TextFormatter中的縮進

這在這種情況下的偉大工程:

沒有任何
縮進常規文本。
       本款具有均勻
       縮進所以一切都是
       確定。

但我也需要這樣的:
       約翰:這一段有
                        diferent壓痕
                       在第一行。
       喬:所以我不知道該怎麼
                       做到這一點。

我找到了ParagraphIndent和FirstLineInParagraph屬性,但我不知道它們是如何工作的,或者如果那樣會很有用。

在此先感謝! 何塞

+0

運氣好嗎?我試圖解決同樣的問題。如果你已經取得了一些成功,如果你回答你自己的問題會很好! – 2010-10-13 08:18:20

回答

1

何塞 -

希望這個代碼框架將幫助......我想你應該從MSDN中,高級文本格式項目當中,遺憾的是不是很先進的開始。下面是一個代碼片段,您可以介紹給MainWindow.Xaml.cs:

 TextParagraphProperties textParagraphProperties = new GenericTextParagraphProperties(TextAlignment.Left, 
                          false); 
     TextRunCache textRunCache = new TextRunCache(); 

     // By default, this is the first line of a paragrph 
     bool firstLine = true; 
     while (textStorePosition < _textStore.Length) 
     { 
      // Create a textline from the text store using the TextFormatter object. 
      TextLine myTextLine = formatter.FormatLine(
       _textStore, 
       textStorePosition, 
       96 * 6, 

       // ProxyTextParagraphProperties will return a different value for the Indent property, 
       // depending on whether firstLine is true or not. 
       new ProxyTextParagraphProperties(textParagraphProperties, firstLine), 
       lastLineBreak, 
       textRunCache); 

      // Draw the formatted text into the drawing context. 
      Debug.WriteLine("linePosition:" + linePosition); 
      myTextLine.Draw(dc, linePosition, InvertAxes.None); 

      // Update the index position in the text store. 
      textStorePosition += myTextLine.Length; 

      // Update the line position coordinate for the displayed line. 
      linePosition.Y += myTextLine.Height; 

      // Figure out if the next line is the first line of a paragraph 
      var textRunSpans = myTextLine.GetTextRunSpans(); 
      firstLine = (textRunSpans.Count > 0) && (textRunSpans[textRunSpans.Count - 1].Value is TextEndOfParagraph); 

      lastLineBreak = myTextLine.GetTextLineBreak(); 
     } 

你需要創建類ProxyTextParagraphProperties,從TextParagraphProperties下降。這是我做了什麼測試這個片段:

class ProxyTextParagraphProperties : TextParagraphProperties 
{ 
    private readonly TextParagraphProperties _paragraphProperties; 
    private readonly bool _firstLineInParagraph; 

    public ProxyTextParagraphProperties(TextParagraphProperties textParagraphProperties, bool firstLineInParagraph) 
    { 
     _paragraphProperties = textParagraphProperties; 
     _firstLineInParagraph = firstLineInParagraph; 
    } 

    public override FlowDirection FlowDirection 
    { 
     get { return _paragraphProperties.FlowDirection; } 
    } 

    // implement the same as above for all the other properties... 

    // But we need to handle this one specially... 
    public override double Indent 
    { 
     get 
     { 
      return _firstLineInParagraph ? _paragraphProperties.Indent : 0; 
     } 
    } 
}