如何使用RichTextBox控件将 line附加到RTF中
使用Microsoft RichTextBox控件时,可以添加这样的新行……
richtextbox.AppendText(System.Environment.NewLine); // appends rn
但是,如果您现在查看生成的rtf, r n字符将转换为 par not line
如何将 line控制代码插入生成的RTF中?
什么行不通:
令牌替换
黑客喜欢在字符串的末尾插入一个令牌,然后在事实之后替换它,所以像这样:
string text = "my text"; text = text.Replace("||" "|"); // replace any '|' chars with a double '||' so they aren't confused in the output. text = text.Replace("rn", "_|0|_"); // replace rn with a placeholder of |0| richtextbox.AppendText(text); string rtf = richtextbox.Rtf; rtf.Replace("_|0|_", "\line"); // replace placeholder with line rtf.Replace("||", "|"); // set back any || chars to |
这几乎可以工作,如果你必须支持从右到左的文本,它会分解,因为从右到左的控制序列总是在占位符的中间结束。
发送密钥消息
public void AppendNewLine() { Keys[] keys = new Keys[] {Keys.Shift, Keys.Return}; SendKeys(keys); } private void SendKeys(Keys[] keys) { foreach(Keys key in keys) { SendKeyDown(key); } } private void SendKeyDown(Keys key) { user32.SendMessage(this.Handle, Messages.WM_KEYDOWN, (int)key, 0); } private void SendKeyUp(Keys key) { user32.SendMessage(this.Handle, Messages.WM_KEYUP, (int)key, 0); }
这也最终被转换为 par
有没有办法将消息直接发布到msftedit控件以插入控制字符?
我完全难过,任何想法的家伙? 谢谢你的帮助!
在我的测试中显示,添加Unicode“行分隔符”(U + 2028)确实有效:
private void Form_Load(object sender, EventArgs e) { richText.AppendText("Hello, World!u2028"); richText.AppendText("Hello, World!u2028"); string rtf = richText.Rtf; richText.AppendText(rtf); }
当我运行程序时,我得到:
Hello, World! Hello, World! {rtf1ansiansicpg1252deff0deflang1031{fonttbl{f0fnilfcharset0 Courier New;}} {colortbl ;red255green255blue255;} viewkind4uc1pardcf1f0fs17 Hello, World!line Hello, World!linepar }
它确实添加了line
而不是par
。
由于您想使用不同的RTF代码,我认为您可能需要忘记简单的AppendText()方法并直接操作RichTextBox的.Rtf属性。 这是一个示例(测试)来演示:
RichTextBox rtb = new RichTextBox(); //this just gets the textbox to populate its Rtf property... may not be necessary in typical usage rtb.AppendText("blah"); rtb.Clear(); string rtf = rtb.Rtf; //exclude the final } and anything after it so we can use Append instead of Insert StringBuilder richText = new StringBuilder(rtf, 0, rtf.LastIndexOf('}'), rtf.Length /* this capacity should be selected for the specific application */); for (int i = 0; i < 5; i++) { string lineText = "example text" + i; richText.Append(lineText); //add a line and CRLF to separate this line of text from the next one richText.AppendLine(@"line"); } //Add back the final } and newline richText.AppendLine("}"); System.Diagnostics.Debug.WriteLine("Original RTF data:"); System.Diagnostics.Debug.WriteLine(rtf); System.Diagnostics.Debug.WriteLine("New Data:"); System.Diagnostics.Debug.WriteLine(richText.ToString()); //Write the RTF data back into the RichTextBox. //WARNING - .NET will reformat the data to its liking at this point, removing //any unused colors from the color table and simplifying/standardizing the RTF. rtb.Rtf = richText.ToString(); //Print out the resulting Rtf data after .NET (potentially) reformats it System.Diagnostics.Debug.WriteLine("Resulting Data:"); System.Diagnostics.Debug.WriteLine(rtb.Rtf);
输出:
原始RTF数据:
{ rtf1 ansi ansicpg1252 deff0 deflang1033 { fonttbl { f0 fnil fcharset0 Microsoft Sans Serif;}} viewkind4 UC1 PARD F0 fs17 看齐 }
新的RTF数据:
{ rtf1 ansi ansicpg1252 deff0 deflang1033 { fonttbl { f0 fnil fcharset0 Microsoft Sans Serif;}} viewkind4 UC1 PARD F0 fs17 看齐 示例text0 line 示例text1 line 示例text2 line 示例text3 line 示例text4 line }
产生的RTF数据:
{ rtf1 ansi ansicpg1252 deff0 deflang1033 { fonttbl { f0 fnil fcharset0 Microsoft Sans Serif;}} viewkind4 UC1 PARD F0 fs17 看齐 示例text0 line示例text1 line示例text2 line示例text3 line示例text4 par }
如果您使用段落写入richtextbox,您可以使用如下所示的LineBreak()相同的代码
Paragraph myParagraph = new Paragraph(); FlowDocument myFlowDocument = new FlowDocument(); // Add some Bold text to the paragraph myParagraph.Inlines.Add(new Bold(new Run(@"Test Description:"))); myParagraph.Inlines.Add(new LineBreak()); // to add a new line use LineBreak() myParagraph.Inlines.Add(new Run("my text")); myFlowDocument.Blocks.Add(myParagraph); myrichtextboxcontrolid.Document = myFlowDocument;
希望这可以帮助!
上述就是C#学习教程:如何使用RichTextBox控件将 line附加到RTF中分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1043999.html