Ejemplo n.º 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;
        }
Ejemplo n.º 2
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;
        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 4
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;
        }