如何在winform上以丰富的格式绘制字符串?

时间:2011-03-22 11:25:41

标签: c# .net winforms drawstring richtext

我需要在Windows窗体上以丰富的格式编写一行文本。我怎么能做到这一点?

5 个答案:

答案 0 :(得分:5)

如果不是富文本框,则需要使用GDI +绘制文本。检查it是否有帮助。

答案 1 :(得分: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,这个链接可能会很有用:

答案 2 :(得分:1)

RichTextBox控件是否设置为只执行您需要的操作?

答案 3 :(得分:1)

除了TcKs Code之外,以下段将解决自动换行功能:

        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;

                }
            }
        }
    }

答案 4 :(得分:0)

事实证明,没有任何标准方法可以在Graphics上下文中绘制RTF字符串。所以我有两种方法可以采取行动:

  1. 手动解析rtf字符串。如TcKs的回答所示,将其分成块并绘制到图形上下文中
  2. 第二种方式是某种解决方法,但它完全可以使用。查看this linkthis
  3. 谢谢大家的帮助!