2011-03-22 63 views
2

我需要在Windows窗體上以豐富格式編寫一行文本。我怎樣才能做到這一點?如何在Winform上以豐富格式繪製字符串?

+0

你是什麼意思的呈現?有什麼東西阻止你使用標籤? – 2011-03-22 11:28:10

+0

好的,我編輯了問題。我需要簡單地在窗體上編寫富文本 – 2011-03-22 11:31:16

回答

5

如果沒有豐富的文本框,那麼你就需要使用油漆GDI +的文本。檢查it是否有幫助。

1

將RichTextBox控件設置爲只讀是否會執行您所需的操作?

+0

RichTextBox在我的情況下是不允許的,我需要正確繪製字符串 – 2011-03-22 11:39:06

+1

然後,您必須使用GDI +並手動繪製字符串。 http://msdn.microsoft.com/en-us/library/aa984365(VS.71).aspx – 2011-03-22 11:51:16

2

您必須將字符串拆分爲具有相同格式的「塊」並繪製每個「塊」。

使用Graphics.MeasureString計算位置,並使用Graphics.DrawString進行最終繪製。

簡單的示例代碼(沒有解決字包木窗,圖片等):

public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 

     this.Paint += new PaintEventHandler(Form1_Paint); 
     this.SizeChanged += new EventHandler(Form1_SizeChanged); 
    } 

    void Form1_SizeChanged(object sender, EventArgs e) { 
     this.Invalidate(); 
    } 

    public class RichText { 
     public Font Font { get; set; } 
     public Color? TextColor { get; set; } 
     public string Text { get; set; } 

     public RichText() { } 
     public RichText(string text) { 
      this.Text = text; 
     } 
     public RichText(string text, Font font) : this(text) { 
      this.Font = font; 
     } 
     public RichText(string text, Color textColor) : this(text) { 
      this.TextColor = textColor; 
     } 
     public RichText(string text, Color textColor, Font font) : this(text, textColor) { 
      this.Font = font; 
     } 
    } 
    void Form1_Paint(object sender, PaintEventArgs e) { 
     var arial_8 = new Font("Arial", 8); 
     var webdings_10 = new Font("Webdings", 10); 

     var richTexts = new RichText[]{ 
      new RichText("Default text.") 
      , new RichText("Default green text.", Color.Green) 
      , new RichText("Regular arial 8.", arial_8) 
      , new RichText("Bold arial 8.", new Font(arial_8, FontStyle.Bold)) 
      , new RichText("Regular webdings 10.", webdings_10) 
      , new RichText("Regular blue webdings 10.", Color.Blue, webdings_10) 
     }; 

     var g = e.Graphics; 

     Point textPosition = new Point(0, 0); 
     int maxWidth = this.ClientSize.Width; 
     foreach (var richText in richTexts) { 
      var text = richText.Text; 
      if (string.IsNullOrEmpty(text)) { continue; } 

      var font = richText.Font ?? Control.DefaultFont; 
      var textColor = richText.TextColor ?? Control.DefaultForeColor; 

      using (var brush = new SolidBrush(textColor)) { 
       var rslt_Measure = g.MeasureString(text, font); 
       if (rslt_Measure.Width + textPosition.X > maxWidth) { 
        // this code does not solve word-wraping 
        var rslt_Line = g.MeasureString("\r\n", font); 
        Point tmpTextPosition = new Point(0, textPosition.Y + (int)rslt_Line.Height); 

        g.DrawString(text, font, brush, tmpTextPosition); 
        var newPosition = tmpTextPosition; 
        newPosition.X += (int)rslt_Measure.Width; 
        textPosition = newPosition; 
       } 
       else { 
        g.DrawString(text, font, brush, textPosition); 
        var newPosition = textPosition; 
        newPosition.X += (int)rslt_Measure.Width; 
        textPosition = newPosition; 
       } 
      } 
     } 
    } 
} 

編輯:

如果你想處理RTF也許這個鏈接將是有益的:

+0

謝謝!這似乎很有幫助,我會嘗試使用您的代碼。但問題是,最初我有一個純RTF格式的字符串 – 2011-03-22 13:30:33

+0

@Anton Semenov:我添加了一些用於RTF處理的鏈接。 – TcKs 2011-03-22 14:13:46

+0

謝謝!鏈接非常有趣 – 2011-03-22 15:51:07

0

事實證明,在Graphics上下文中沒有任何標準的繪製RTF字符串的方法。所以我有兩種行動方式:

  1. 手動解析rtf字符串。按照TcKs的回答,將其設計爲塊並繪製到圖形上下文中。
  2. 第二種方法是某種解決方法,但完全可以使用。看看this linkthis

謝謝大家的幫助!

1

除了第三文化孩子代碼,以下段將解決自動換行功能:

 private void pictureBox2_Paint(object sender, PaintEventArgs e) 
    { 
     var arial_8 = new Font("Arial", 8); 
     var webdings_10 = new Font("Webdings", 10); 

     var richTexts = new RichText[] { 
     new RichText("Default text and this is the lengthy string to check the display. One more line to verify again") 
     , new RichText("Default green text. this is the lengthy string to check the display. One more line to verify again", Color.Green) 
     , new RichText("Regular arial 8.", arial_8) 
     , new RichText("Bold arial 8.", new Font(arial_8, FontStyle.Bold)) 
     }; 

     var g = e.Graphics; 

     Point textPosition = new Point(0, 0); 
     int maxWidth = this.pictureBox2.ClientSize.Width; 
     foreach (var richText in richTexts) 
     { 
      var text = richText.Text; 
      if (string.IsNullOrEmpty(text)) { continue; } 

      var font = richText.Font ?? Control.DefaultFont; 
      var textColor = richText.TextColor ?? Control.DefaultForeColor; 

      using (var brush = new SolidBrush(textColor)) 
      { 
       string[] strs = text.Split(new string[] { " " }, StringSplitOptions.None); 
       int fontheight = (int)g.MeasureString("\r\n", font).Height; 
       foreach (string val in strs) 
       { 
        var measure = g.MeasureString(val, font); 
        if (measure.Width + textPosition.X > maxWidth) 
        { 
         var newPosition = textPosition; 
         newPosition.X = 0; 
         newPosition.Y = textPosition.Y + fontheight; 
         textPosition = newPosition; 
        } 

        g.DrawString(val, font, brush, textPosition); 

        var nextposition = textPosition; 
        nextposition.X = textPosition.X + (int)measure.Width; 
        textPosition = nextposition; 

       } 
      } 
     } 
    }