using System; using System.Drawing; using System.Windows.Forms; namespace BaseFunction { public 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(""); #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 PARSE THROUGH TEXT DATA m_RichTextBox.SelectedText = Environment.NewLine; })); } public static void SetCNCText(RichTextBox m_RichTextBox, string strText, float Size = 16) { m_RichTextBox.Invoke(((EventHandler)delegate { #region PARSE THROUGH TEXT DATA for (int i = 0; i < strText.Length; i++) { switch (strText[i]) { case '%': RichTextUnit.SetFont(m_RichTextBox, Color.Red, false, Size); m_RichTextBox.SelectedText = strText[i].ToString(); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': RichTextUnit.SetFont(m_RichTextBox, Color.DeepSkyBlue, false, Size); m_RichTextBox.SelectedText = strText[i].ToString(); break; default: RichTextUnit.SetFont(m_RichTextBox, Color.White, false, Size); m_RichTextBox.SelectedText = strText[i].ToString(); break; } } #endregion PARSE THROUGH TEXT DATA })); } } }