RTFTextBox 在 Windows 上工作,Mono 抛出“RTF 解析失败:格式错误的颜色输入”
我有一些 C#.NET 代码来获取包含自定义颜色代码的字符串,并使用它们在 RichTextBox 中显示彩色文本。它在 Windows 上工作得很好,但在 Mono 2.4.2.3 上的 Linux 下运行时,我收到此错误:
RTF Parsing failure: Malformed color entry
RTF Stream Info: Pos:324 Line:1
TokenClass:Text, Major:8, Minor:0, Param:-10000000, Text:
代码如下:
/// <summary>
/// Populates a RichTextBox with text generated from color code strings.
/// </summary>
/// <param name="text"></param>
/// <param name="target"></param>
public static void BuildRTFString(string text, RichTextBox target)
{
// Add header and build color table.
const string rtfHeader = "{\\rtf\\ansi{\\colortbl\\red192\\green192\\blue192;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red0\\green255\\blue0;\\red255\\green0\\blue255;\\red255\\green0\\blue0;\\red255\\green255\\blue0;\\red255\\green255\\blue255;\\red0\\green0\\blue128;\\red0\\green128\\blue128;\\red0\\green128\\blue0;\\red128\\green0\\blue128;\\red128\\green0\blue0;\\red128\\green128\\blue0;\\red128\\green128\\blue128;\\red192\\green192\\blue192;}\\cf0 ";
// Replace each color one by one.
string parsedText = text.Replace("&+l", "\\cf1 ");
parsedText = parsedText.Replace("&+B", "\\cf2 ");
parsedText = parsedText.Replace("&+C", "\\cf3 ");
parsedText = parsedText.Replace("&+G", "\\cf4 ");
parsedText = parsedText.Replace("&+M", "\\cf5 ");
parsedText = parsedText.Replace("&+R", "\\cf6 ");
parsedText = parsedText.Replace("&+Y", "\\cf7 ");
parsedText = parsedText.Replace("&+W", "\\cf8 ");
parsedText = parsedText.Replace("&+b", "\\cf9 ");
parsedText = parsedText.Replace("&+c", "\\cf10 ");
parsedText = parsedText.Replace("&+g", "\\cf11 ");
parsedText = parsedText.Replace("&+m", "\\cf12 ");
parsedText = parsedText.Replace("&+r", "\\cf13 ");
parsedText = parsedText.Replace("&+y", "\\cf14 ");
parsedText = parsedText.Replace("&+L", "\\cf15 ");
parsedText = parsedText.Replace("&+w", "\\cf16 ");
parsedText = parsedText.Replace("&n", "\\cf16 ");
parsedText = parsedText.Replace("&N", "\\cf16 ");
target.Rtf = rtfHeader + parsedText + "}";
}
无论向该方法提供什么文本,都会发生此错误,并导致 RichTextBox 呈现为空。
我可以做些什么来完成这项工作,还是 Mono 下的 RichTextBox 还没有完全完成(或者太不同而无法按照我想要的方式使用)?
如果您想查看如何使用它的示例,请查看此屏幕截图: http://www.basternae.org/images/RtfTextBox.png
I have some C#.NET code to take a string containing custom color codes and use them show colorized text in a RichTextBox. It works great on Windows, but running under Linux on Mono 2.4.2.3 I get this error:
RTF Parsing failure: Malformed color entry
RTF Stream Info: Pos:324 Line:1
TokenClass:Text, Major:8, Minor:0, Param:-10000000, Text:
Here is the code:
/// <summary>
/// Populates a RichTextBox with text generated from color code strings.
/// </summary>
/// <param name="text"></param>
/// <param name="target"></param>
public static void BuildRTFString(string text, RichTextBox target)
{
// Add header and build color table.
const string rtfHeader = "{\\rtf\\ansi{\\colortbl\\red192\\green192\\blue192;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red0\\green255\\blue0;\\red255\\green0\\blue255;\\red255\\green0\\blue0;\\red255\\green255\\blue0;\\red255\\green255\\blue255;\\red0\\green0\\blue128;\\red0\\green128\\blue128;\\red0\\green128\\blue0;\\red128\\green0\\blue128;\\red128\\green0\blue0;\\red128\\green128\\blue0;\\red128\\green128\\blue128;\\red192\\green192\\blue192;}\\cf0 ";
// Replace each color one by one.
string parsedText = text.Replace("&+l", "\\cf1 ");
parsedText = parsedText.Replace("&+B", "\\cf2 ");
parsedText = parsedText.Replace("&+C", "\\cf3 ");
parsedText = parsedText.Replace("&+G", "\\cf4 ");
parsedText = parsedText.Replace("&+M", "\\cf5 ");
parsedText = parsedText.Replace("&+R", "\\cf6 ");
parsedText = parsedText.Replace("&+Y", "\\cf7 ");
parsedText = parsedText.Replace("&+W", "\\cf8 ");
parsedText = parsedText.Replace("&+b", "\\cf9 ");
parsedText = parsedText.Replace("&+c", "\\cf10 ");
parsedText = parsedText.Replace("&+g", "\\cf11 ");
parsedText = parsedText.Replace("&+m", "\\cf12 ");
parsedText = parsedText.Replace("&+r", "\\cf13 ");
parsedText = parsedText.Replace("&+y", "\\cf14 ");
parsedText = parsedText.Replace("&+L", "\\cf15 ");
parsedText = parsedText.Replace("&+w", "\\cf16 ");
parsedText = parsedText.Replace("&n", "\\cf16 ");
parsedText = parsedText.Replace("&N", "\\cf16 ");
target.Rtf = rtfHeader + parsedText + "}";
}
This error happens no matter what text is supplied to the method and causes the RichTextBox to render empty.
Is there anything I can do to make this work, or is the RichTextBox under Mono just not quite done yet (or too different to use the way I intend)?
If you'd like to see an example of how it is used, check this screenshot:
http://www.basternae.org/images/RtfTextBox.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论