简体   繁体   English

C#RichTextBox彩色文本

[英]C# RichTextBox colored text

I've a RichTextBox and I want color text in it. 我有一个RichTextBox,我想在其中添加彩色文本。 Is there any tag option? 有标签选项吗? I'd like something like this [color:red]nick[/color] some message . 我想要类似[color:red]nick[/color] some message Because I need to save it as text and I want on reload have also colored text. 因为我需要将其另存为文本,并且希望在重新加载时也为文本添加彩色。

Can I do something like this without writing own method? 我可以在不编写自己的方法的情况下做这样的事情吗?

You can set color for text in RichTextBox with SelectionColor 您可以使用SelectionColor为RichTextBox中的文本设置颜色

And if you want to save your rtf as plain text, then you will have to look at rtf format . 而且,如果您要将rtf保存为纯文本格式,则必须查看rtf format Example: 例:

{\\rtf1\\ansi\\deff0 {\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;} This line is the default color\\line \\cf2 This line is red\\line \\cf1 This line is the default color } {\\ rtf1 \\ ansi \\ deff0 {\\ colortbl; \\ red0 \\ green0 \\ blue0; \\ red255 \\ green0 \\ blue0;}这行是默认颜色\\ line \\ cf2这行是red \\ line \\ cf1这行是默认颜色}

EDIT: 编辑:

From this example - first of all you have to declare color table \\colortbl in fololowing format: 在此示例中-首先,必须以以下格式声明颜色表\\colortbl

{\colortbl; color1; color2; ... ; colorN;} 

And then in the text you will have to enclose text with {\\cfN YOUR_TEXT} where N is a number of color from table; 然后,在文本中,您必须用{\\cfN YOUR_TEXT}将文本括{\\cfN YOUR_TEXT} ,其中N是表中颜色的数量; you can not specify the boundaries of the block {}, then everything after \\ cfN will be one color. 您不能指定块{}的边界,那么\\ cfN之后的所有内容都是一种颜色。

As the name says RichTextBox contains RichText 顾名思义,RichTextBox包含RichText

to change the Rtf text with 'rtf specific-tags' you can set/use the 要使用“ rtf specific-tags”更改Rtf文本,您可以设置/使用

RichTextBox.RtfProperty RichTextBox.RtfProperty

also take a look at RichTextBox.SelectionColor to color text patterns in code 还看一看RichTextBox.SelectionColor为代码中的文本图案上色

but when you don't want to use rtf, you said 但是当您不想使用rtf时,您说

need to save as text. 需要另存为文本。

you could write your own 'markup' there is no built in expect rtf/html? 您可以编写自己的“标记”,但没有内置期望rtf / html?

but rtf is text - at all 但是rtf是文本 -完全是

Example to use RichTextBix.SelectionColor to Color the text 使用RichTextBix.SelectionColor为文本着色的示例

richTextBox1.Text = "Hello";
richTextBox1.Select(0,2);
richTextBox1.SelectionColor = Color.Red;

colors the start of "Hello" red and now you can access the 'taggeg' text in the RTFProperty of the RichTextBox 将“ Hello”的开头颜色为红色,现在您可以在RichTextBox的RTFProperty中访问“ taggeg”文本

If you need examples of how things are encoded in RTF, you can create the document manually in Word or Wordpad, and save it as RTF. 如果需要有关如何在RTF中进行编码的示例,则可以在Word或写字板中手动创建文档,然后将其另存为RTF。 This will give you a hint about how to encode your formatting. 这将为您提供有关如何编码格式的提示。 Furthermore, if you're for instance creating help-documents, you can include them as an embedded resource and load them directly into the RichTextBox, with all the formatting included. 此外,例如,如果您要创建帮助文档,则可以将它们作为嵌入式资源包含在内,并直接将它们加载到RichTextBox中,其中包括所有格式。

rtfMain.SaveFile(dlgSave.FileName);

来自引用使用C#从富文本框中保存文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM