Beispiel #1
0
        /// <summary>
        /// 将编辑框的文本转到预览框
        /// </summary>
        /// <param name="txtEdit"></param>
        /// <param name="txtPreview"></param>
        public static void ConvertToPreviewTextBox(MyTextBox txtEdit, MyRichTextBox txtPreview)
        {
            txtEdit.lockTextChange = true;
            txtPreview.lockTextChange = true;

            MyRichTextBox txtPreviewClone = new MyRichTextBox() { BackColor = txtPreview.BackColor, ForeColor = txtPreview.ForeColor };

            int cursorLocation = txtEdit.SelectionStart;
            txtEdit.Text = txtEdit.Text.Replace(Environment.NewLine, "|n");// 转换回车换行为|n
            txtEdit.SelectionStart = cursorLocation;
            char[] charArray = txtEdit.Text.ToCharArray();// 将原来的字符串转为字符数组处理,并转换回车换行为|n
            StringBuilder buffer = new StringBuilder();// 用于缓冲还没输出的字符

            for (int i = 0; i < charArray.Length; i++)
            {
                if (charArray[i] == '|')// 转义字符
                {
                    if (((i + 1) < charArray.Length) && ((charArray[i + 1] == 'n') || (charArray[i + 1] == 'N')))// 换行
                    {
                        Color currentColor = txtPreviewClone.SelectionColor;// 保存当前颜色
                        txtPreviewClone.AppendText(buffer.ToString() + Environment.NewLine);// 将缓冲字符和换行输出
                        buffer = new StringBuilder();// 清空缓冲区
                        txtPreviewClone.SelectionColor = currentColor;// 恢复颜色
                        i++;// 跳过n或N
                        continue;
                    }
                    else if (((i + 9) < charArray.Length) && ((charArray[i + 1] == 'c') || (charArray[i + 1] == 'C')))// 颜色开始
                    {
                        string strColor = charArray[i + 2].ToString() + charArray[i + 3].ToString()// A
                                        + charArray[i + 4].ToString() + charArray[i + 5].ToString()// R
                                        + charArray[i + 6].ToString() + charArray[i + 7].ToString()// G
                                        + charArray[i + 8].ToString() + charArray[i + 9].ToString()// B
                                        ;
                        if (IsColor(strColor) == true)// 可以转化为颜色
                        {
                            txtPreviewClone.AppendText(buffer.ToString());// 将缓冲字符和换行输出
                            buffer = new StringBuilder();// 清空缓冲区
                            txtPreviewClone.SelectionColor = ConvertToColor(strColor);
                            i += 9;// 跳过c或C和8位的颜色值
                            continue;
                        }
                    }
                    else if (((i + 1) < charArray.Length) && ((charArray[i + 1] == 'r') || (charArray[i + 1] == 'R')))// 颜色结束
                    {
                        txtPreviewClone.AppendText(buffer.ToString());// 将缓冲字符和换行输出
                        buffer = new StringBuilder();// 清空缓冲区
                        txtPreviewClone.SelectionColor = Color.White;
                        i++;// 跳过r或R
                        continue;
                    }
                }
                buffer.Append(charArray[i].ToString());// 将字符输入到缓冲区
            }
            txtPreviewClone.AppendText(buffer.ToString());// 将剩余的字符输出

            txtPreview.Rtf = txtPreviewClone.Rtf;
            txtPreviewClone.Dispose();
            txtEdit.lockTextChange = false;
            txtPreview.lockTextChange = false;
        }
Beispiel #2
0
        public static void SetTxtPreviewSingleColor(MyRichTextBox txtPreview, Color color)
        {
            txtPreview.lockTextChange = true;
            int cursorLocation = txtPreview.SelectionStart;             // 保存编辑位置
            int selectLength   = txtPreview.SelectionLength;            // 保存处理字符串的长度

            txtPreview.SelectionColor  = color;                         // 设置颜色
            txtPreview.SelectionStart  = cursorLocation + selectLength; // 移动光标
            txtPreview.SelectionLength = 0;                             // 清空选择
            txtPreview.lockTextChange  = false;
            txtPreview.Select();                                        // 激活控件
        }
Beispiel #3
0
        public static void SetTxtPreviewGradual(MyRichTextBox txtPreview, Color startColor, Color endColor)
        {
            int selectLength = txtPreview.SelectionLength;// 需要处理的长度

            if (selectLength > 1)
            {
                txtPreview.lockTextChange = true;
                for (int i = 0; i < selectLength; i++)
                {
                    txtPreview.SelectionLength = 1;
                    Color tempColor = Color.FromArgb(startColor.R + (endColor.R - startColor.R) * i / (selectLength - 1), startColor.G + (endColor.G - startColor.G) * i / (selectLength - 1), startColor.B + (endColor.B - startColor.B) * i / (selectLength - 1));// 计算当前字的颜色
                    txtPreview.SelectionColor  = tempColor;
                    txtPreview.SelectionLength = 0;
                    txtPreview.SelectionStart++;
                }
                txtPreview.lockTextChange = false;
                txtPreview.Select();// 激活控件
            }
            else
            {
                SetTxtPreviewSingleColor(txtPreview, startColor);
            }
        }
Beispiel #4
0
        public static void ConvertToEditTextBox(MyRichTextBox txtPreview, MyTextBox txtEdit)
        {
            txtEdit.lockTextChange = true;

            #region 克隆控件
            MyRichTextBox txtPreviewClone = new MyRichTextBox()
            {
                Rtf = txtPreview.Rtf,
                SelectionStart = 0,// 从开头开始处理
                SelectionLength = 1// 每次的处理长度
            };
            #endregion

            bool isHighLight = false;// 标志是否高亮中
            StringBuilder sb = new StringBuilder();// 存储已经处理的文本
            Color currentColor = txtPreviewClone.SelectionColor;// 当前颜色

            #region 处理第一个字符
            if (currentColor != Color.White)
            {
                sb.Append("|c" + ColorToHex(txtPreviewClone.SelectionColor));
                isHighLight = true;
            }
            #endregion

            int length = txtPreviewClone.TextLength;// 处理的总文本长度

            while (txtPreviewClone.SelectionStart < length)
            {
                if (txtPreviewClone.SelectionColor != currentColor)// 改变颜色
                {
                    if (isHighLight)// 结束高亮
                    {
                        sb.Append("|r");
                        isHighLight = false;
                    }
                    if (txtPreviewClone.SelectionColor != Color.White)// 不是默认颜色,开始高亮
                    {
                        sb.Append("|c" + ColorToHex(txtPreviewClone.SelectionColor));
                        isHighLight = true;
                    }
                    currentColor = txtPreviewClone.SelectionColor;
                }
                #region 将回车转为|n
                if (txtPreviewClone.SelectedText == "\n")
                {
                    sb.Append("|n");
                    txtPreviewClone.SelectionStart++;
                    continue;
                }
                #endregion
                sb.Append(txtPreviewClone.SelectedText);
                txtPreviewClone.SelectionStart++;
            }
            if (isHighLight)
            {
                sb.Append("|r");
            }

            txtEdit.Text = sb.ToString();
            txtEdit.AddToUndoStack(sb.ToString());

            txtPreviewClone.Dispose();// 销毁克隆对象
            txtEdit.lockTextChange = false;
        }
Beispiel #5
0
 public static void SetTxtPreviewGradual(MyRichTextBox txtPreview, Color startColor, Color endColor)
 {
     int selectLength = txtPreview.SelectionLength;// 需要处理的长度
     if (selectLength > 1)
     {
         txtPreview.lockTextChange = true;
         for (int i = 0; i < selectLength; i++)
         {
             txtPreview.SelectionLength = 1;
             Color tempColor = Color.FromArgb(startColor.R + (endColor.R - startColor.R) / (selectLength - 1) * i, startColor.G + (endColor.G - startColor.G) / (selectLength - 1) * i, startColor.B + (endColor.B - startColor.B) / (selectLength - 1) * i);// 计算当前字的颜色
             txtPreview.SelectionColor = tempColor;
             txtPreview.SelectionLength = 0;
             txtPreview.SelectionStart++;
         }
         txtPreview.lockTextChange = false;
     }
     else
     {
         SetTxtPreviewSingleColor(txtPreview, startColor);
     }
 }
Beispiel #6
0
 public static void SetTxtPreviewSingleColor(MyRichTextBox txtPreview, Color color)
 {
     txtPreview.lockTextChange = true;
     int cursorLocation = txtPreview.SelectionStart;// 保存编辑位置
     int selectLength = txtPreview.SelectionLength;// 保存处理字符串的长度
     txtPreview.SelectionColor = color;// 设置颜色
     txtPreview.SelectionStart = cursorLocation + selectLength;// 移动光标
     txtPreview.lockTextChange = false;
 }
Beispiel #7
0
        public static void ConvertToEditTextBoxEx(MyRichTextBox txtPreview, MyTextBox txtEdit)
        {
            txtEdit.lockTextChange = true;

            string rtf = txtPreview.Rtf;

            int colorTableStart = rtf.IndexOf(@"\colortbl");// 颜色表开始位置
            int colorTableEnd = rtf.IndexOf('}', colorTableStart);// 颜色表结束位置
            string[] colorTable = rtf.Substring(colorTableStart, colorTableEnd - colorTableStart).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);// 获取颜色表子串并分割

            List<Color> colors = new List<Color>();// 存储rtf所用到的所有颜色
            for (int i = 1; i < colorTable.Length; i++)
            {
                string[] color = colorTable[i].Replace("red", string.Empty).Replace("green", string.Empty).Replace("blue", string.Empty).Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);// 获取颜色字符串的RGB值
                colors.Add(Color.FromArgb(int.Parse(color[0]), int.Parse(color[1]), int.Parse(color[2])));// 将颜色存入集合
            }

            string processStr = rtf.Substring(rtf.IndexOf(@"\fs18") + @"\fs18".Length).Replace(Environment.NewLine, string.Empty);// 截取处理字符串并去除换行符
            processStr = processStr.Substring(0, processStr.LastIndexOf(@"\par"));// 去除结尾的\par

            StringBuilder finishStr = new StringBuilder();// 处理完成字符串            
            for (int i = 0; i < processStr.Length; i++)
            {
                if (processStr[i] == '\\')
                {
                    if (i + 1 < processStr.Length && processStr[i + 1] == '\\')// '\'
                    {
                        finishStr.Append('\\');
                        i++;
                    }
                    else if (i + 7 < processStr.Length && processStr[i + 1] == '\'' && processStr[i + 4] == '\\' && processStr[i + 5] == '\'')// 中文等非ASCII字符
                    {
                        finishStr.Append(Encoding.GetEncoding(936).GetString(new byte[] { (byte)EncodingConvert(processStr[i + 2], processStr[i + 3]), (byte)EncodingConvert(processStr[i + 6], processStr[i + 7]) }));
                        i += 7;
                    }
                    else if (i + 3 < processStr.Length && processStr[i + 1] == 'c' && processStr[i + 2] == 'f' && processStr[i + 3] >= '0' && processStr[i + 3] <= '9')// 颜色
                    {
                        int j = i + 3;
                        string num = string.Empty;
                        while (j < processStr.Length && processStr[j] >= '0' && processStr[j] <= '9')
                        {
                            num += processStr[j];
                            j++;
                        }
                        i = j - 1;
                        if (j < processStr.Length && processStr[j] == ' ')
                        {
                            i++;
                        }
                        int colorIndex = int.Parse(num) - 1;
                        if (colors[colorIndex].R == 255 && colors[colorIndex].G == 255 && colors[colorIndex].B == 255)
                        {
                            finishStr.Append("|r");
                        }
                        else
                        {
                            finishStr.Append("|c" + ColorToHex(colors[colorIndex]));
                        }
                    }
                    else if (i + 3 < processStr.Length && processStr[i + 1] == 'p' && processStr[i + 2] == 'a' && processStr[i + 3] == 'r')// 换行
                    {
                        finishStr.Append("|n");
                        i += 3;
                    }
                }
                else
                {
                    finishStr.Append(processStr[i]);
                }
            }

            txtEdit.Text = finishStr.ToString();
            txtEdit.AddToUndoStack(finishStr.ToString());

            txtEdit.lockTextChange = false;
        }
Beispiel #8
0
        public static void ConvertToEditTextBox(MyRichTextBox txtPreview, MyTextBox txtEdit)
        {
            txtEdit.lockTextChange = true;

            #region 克隆控件
            MyRichTextBox txtPreviewClone = new MyRichTextBox()
            {
                Rtf             = txtPreview.Rtf,
                SelectionStart  = 0,   // 从开头开始处理
                SelectionLength = 1,   // 每次的处理长度
                ignoreRefresh   = true // 忽略控件自身的重绘,提高响应速度
            };
            #endregion

            bool          isHighLight  = false;                          // 标志是否高亮中
            StringBuilder sb           = new StringBuilder();            // 存储已经处理的文本
            Color         currentColor = txtPreviewClone.SelectionColor; // 当前颜色

            #region 处理第一个字符
            if (currentColor != Color.White)
            {
                sb.Append("|c" + ColorToHex(txtPreviewClone.SelectionColor));
                isHighLight = true;
            }
            #endregion

            int length = txtPreviewClone.TextLength;// 处理的总文本长度

            while (txtPreviewClone.SelectionStart < length)
            {
                if (txtPreviewClone.SelectionColor != currentColor) // 改变颜色
                {
                    if (isHighLight)                                // 结束高亮
                    {
                        sb.Append("|r");
                        isHighLight = false;
                    }
                    if (txtPreviewClone.SelectionColor != Color.White)// 不是默认颜色,开始高亮
                    {
                        sb.Append("|c" + ColorToHex(txtPreviewClone.SelectionColor));
                        isHighLight = true;
                    }
                    currentColor = txtPreviewClone.SelectionColor;
                }
                #region 将回车转为|n
                if (txtPreviewClone.SelectedText == "\n")
                {
                    sb.Append("|n");
                    txtPreviewClone.SelectionStart++;
                    continue;
                }
                #endregion
                #region 将|转为||
                else if (txtPreviewClone.SelectedText == "|")
                {
                    sb.Append("||");
                    txtPreviewClone.SelectionStart++;
                    continue;
                }
                #endregion
                sb.Append(txtPreviewClone.SelectedText);
                txtPreviewClone.SelectionStart++;
            }
            if (isHighLight)
            {
                sb.Append("|r");
            }

            txtEdit.Text = sb.ToString();
            txtEdit.AddToUndoStack(sb.ToString());

            txtPreviewClone.Dispose();// 销毁克隆对象
            txtEdit.lockTextChange = false;
        }
Beispiel #9
0
        /// <summary>
        /// 将编辑框的文本转到预览框
        /// </summary>
        /// <param name="txtEdit"></param>
        /// <param name="txtPreview"></param>
        public static void ConvertToPreviewTextBox(MyTextBox txtEdit, MyRichTextBox txtPreview)
        {
            txtEdit.lockTextChange    = true;
            txtPreview.lockTextChange = true;

            MyRichTextBox txtPreviewClone = new MyRichTextBox()
            {
                BackColor     = txtPreview.BackColor,
                ForeColor     = txtPreview.ForeColor,
                ignoreRefresh = true// 忽略控件自身的重绘,提高响应速度
            };

            int cursorLocation = txtEdit.SelectionStart;

            txtEdit.Text           = txtEdit.Text.Replace(Environment.NewLine, "|n"); // 转换回车换行为|n
            txtEdit.SelectionStart = cursorLocation;
            char[]        charArray = txtEdit.Text.ToCharArray();                     // 将原来的字符串转为字符数组处理,并转换回车换行为|n
            StringBuilder buffer    = new StringBuilder();                            // 用于缓冲还没输出的字符

            for (int i = 0; i < charArray.Length; i++)
            {
                if (charArray[i] == '|')                                       // 转义字符
                {
                    if ((i + 1) < charArray.Length && charArray[i + 1] == '|') // 字符'|'
                    {
                        buffer.Append('|');                                    // 将字符'|'输入到缓冲区
                        i++;
                        continue;
                    }
                    if (((i + 1) < charArray.Length) && ((charArray[i + 1] == 'n') || (charArray[i + 1] == 'N'))) // 换行
                    {
                        Color currentColor = txtPreviewClone.SelectionColor;                                      // 保存当前颜色
                        txtPreviewClone.AppendText(buffer.ToString() + Environment.NewLine);                      // 将缓冲字符和换行输出
                        buffer = new StringBuilder();                                                             // 清空缓冲区
                        txtPreviewClone.SelectionColor = currentColor;                                            // 恢复颜色
                        i++;                                                                                      // 跳过n或N
                        continue;
                    }
                    else if (((i + 9) < charArray.Length) && ((charArray[i + 1] == 'c') || (charArray[i + 1] == 'C'))) // 颜色开始
                    {
                        string strColor = charArray[i + 2].ToString() + charArray[i + 3].ToString()                    // A
                                          + charArray[i + 4].ToString() + charArray[i + 5].ToString()                  // R
                                          + charArray[i + 6].ToString() + charArray[i + 7].ToString()                  // G
                                          + charArray[i + 8].ToString() + charArray[i + 9].ToString()                  // B
                        ;
                        if (IsColor(strColor) == true)                                                                 // 可以转化为颜色
                        {
                            txtPreviewClone.AppendText(buffer.ToString());                                             // 将缓冲字符和换行输出
                            buffer = new StringBuilder();                                                              // 清空缓冲区
                            txtPreviewClone.SelectionColor = ConvertToColor(strColor);
                            i += 9;                                                                                    // 跳过c或C和8位的颜色值
                            continue;
                        }
                    }
                    else if (((i + 1) < charArray.Length) && ((charArray[i + 1] == 'r') || (charArray[i + 1] == 'R'))) // 颜色结束
                    {
                        txtPreviewClone.AppendText(buffer.ToString());                                                 // 将缓冲字符和换行输出
                        buffer = new StringBuilder();                                                                  // 清空缓冲区
                        txtPreviewClone.SelectionColor = Color.White;
                        i++;                                                                                           // 跳过r或R
                        continue;
                    }
                }
                buffer.Append(charArray[i].ToString());    // 将字符输入到缓冲区
            }
            txtPreviewClone.AppendText(buffer.ToString()); // 将剩余的字符输出

            txtPreview.Rtf = txtPreviewClone.Rtf;
            txtPreviewClone.Dispose();
            txtEdit.lockTextChange    = false;
            txtPreview.lockTextChange = false;
        }