C# RichTextBox显示不同格式的字体

时间:2021-02-16 16:02:02

C# RichTextBox显示不同格式的字体

            txtBox.AppendText(titile);
            txtBox.Select(0, titile.Length);
            txtBox.SelectionColor = System.Drawing.Color.Blue;

或者:

       txtBox.SelectionStart = 0;

      txtBox.SelectionLength = 5;

txtBox.SelectionColor = System.Drawing.Color.Blue;

转自:http://blog.sina.com.cn/s/blog_4b3485000100slj9.html

            //定义四个文本部分,便于分别应用不同的文本格式
           string firstString = "大山的骄傲\r\n ";
           string secondString = "——eheartcool其人\r\n\r ";
           string thirdString = "\t七十年代末的一个夜晚,没有星星,也没有月亮," +"中国湖北恩施的小山村正沉浸在睡梦中,几声婴儿的哭嘀和紧接的爆竹声" + "冲破了夜的沉默,一个新的生命诞生了!\r\n";
           string fourthString = "未完待续…… ";
           rtb.Text = firstString;
           rtb.Text += secondString;
           rtb.Text += thirdString;
           rtb.Text += fourthString;

           //对标题应用格式
           rtb.SelectionStart = 0;
           rtb.SelectionLength = firstString.Length - 1;
           rtb.SelectionFont = new System.Drawing.Font("黑体 ", 18F,
               System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,
               ((System.Byte)(0)));
           rtb.SelectionColor = System.Drawing.Color.Blue;
           rtb.SelectionAlignment = HorizontalAlignment.Center;

           //对副标题应用格式
           rtb.SelectionStart = firstString.Length - 1;
           rtb.SelectionLength = secondString.Length;
           rtb.SelectionFont = new System.Drawing.Font("黑体 ", 12F,