RichTextBox控件操作

来源:互联网 发布:win7安装apache 编辑:程序博客网 时间:2024/05/17 02:48

C#富文本控件RichTextBox 使用方法,文本框颜色、字体、格式设置;
以XML文本字符串,用不同颜色、字体、格式显示为例;

程序源码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using System.Windows.Forms; class RichTextUnit    {        public static float m_Size = 16;        public static void SetFont(RichTextBox m_RichTextBox, Color m_Color, bool bBold = false, float Size = 16)        {            m_RichTextBox.SelectionColor = m_Color;            if (bBold)                m_RichTextBox.SelectionFont = new System.Drawing.Font("宋体", Size, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));            else                m_RichTextBox.SelectionFont = new System.Drawing.Font("宋体", Size, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));        }        public static void SetText(RichTextBox m_RichTextBox, string strText, Color m_Color, bool bBold = false, float Size = 16)        {            m_RichTextBox.Invoke(((EventHandler)delegate            {                RichTextUnit.SetFont(m_RichTextBox, m_Color, bBold, Size);                m_RichTextBox.SelectedText = strText;            }));        }        public static void SetXmlText(RichTextBox m_RichTextBox, string strText,float Size=16)        {            m_RichTextBox.Invoke(((EventHandler)delegate            {                int iFirstInx = strText.IndexOf("<?");                int iSecondInx = strText.IndexOf("?>");                #region PARSE THROUGH TEXT DATA                for (int i = 0; i < strText.Length; i++)                {                    if (i >= iFirstInx && i <= iSecondInx + 1)                    {                        RichTextUnit.SetFont(m_RichTextBox, Color.Blue, false, Size);                        m_RichTextBox.SelectedText = strText[i].ToString();                    }                    else                    {                        switch (strText[i])                        {                            case '<':                                {                                    RichTextUnit.SetFont(m_RichTextBox, Color.Blue, false, Size);                                    m_RichTextBox.SelectedText = strText[i].ToString();                                    if (strText[i + 1] == '/')                                    {                                        m_RichTextBox.SelectedText = strText[i + 1].ToString();                                        i++;                                    }                                    RichTextUnit.SetFont(m_RichTextBox, Color.DarkRed, false, Size);                                }                                break;                            case '>':                                {                                    RichTextUnit.SetFont(m_RichTextBox, Color.Blue, false, Size);                                    m_RichTextBox.SelectedText = strText[i].ToString();                                    RichTextUnit.SetFont(m_RichTextBox, Color.Black, false, Size);                                }                                break;                            case '/':                                {                                    RichTextUnit.SetFont(m_RichTextBox, Color.Blue, false, Size);                                    m_RichTextBox.SelectedText = strText[i].ToString();                                }                                break;                            case '=':                                {                                    if (strText[i + 1] == '"')                                    {                                        RichTextUnit.SetFont(m_RichTextBox, Color.Black, true, Size);                                        m_RichTextBox.SelectedText = "=" + '"'.ToString();                                        i++;                                    }                                }                                break;                            case '"':                                {                                    RichTextUnit.SetFont(m_RichTextBox, Color.Blue, false, Size);                                    m_RichTextBox.SelectedText = strText[i].ToString();                                    if (strText[i - 1] == '=')                                        RichTextUnit.SetFont(m_RichTextBox, Color.Black, true, Size);                                    else                                        RichTextUnit.SetFont(m_RichTextBox, Color.DarkRed, false, Size);                                }                                break;                            case '!':                                {                                    RichTextUnit.SetFont(m_RichTextBox, Color.Green, false, Size);                                    m_RichTextBox.SelectedText = strText[i].ToString();                                }                                break;                            case '\r':                                {                                    if (strText[i + 1] == '\n')                                    {                                        m_RichTextBox.SelectedText = "\r\n";                                        i++;                                    }                                }                                break;                            default:                                m_RichTextBox.SelectedText = strText[i].ToString();                                break;                        }                    }                }                #endregion                m_RichTextBox.SelectedText = Environment.NewLine;            }));        }    }
原创粉丝点击