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(); // 激活控件 }
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); } }
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();// 激活控件 }