如何使用 RichTextBox 控件将 \line 追加到 RTF 中
使用 Microsoft RichTextBox 控件时,可以添加这样的新行...
richtextbox.AppendText(System.Environment.NewLine); // appends \r\n
但是,如果您现在查看生成的 rtf,\r\n 字符将转换为 \par 而不是 \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("\r\n", "_|0|_"); // replace \r\n 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 控件以插入控制字符?
我完全被难住了,大家有什么想法吗?感谢您的帮助!
When using the Microsoft RichTextBox control it is possible to add new lines like this...
richtextbox.AppendText(System.Environment.NewLine); // appends \r\n
However, if you now view the generated rtf the \r\n characters are converted to \par not \line
How do I insert a \line control code into the generated RTF?
What does't work:
Token Replacement
Hacks like inserting a token at the end of the string and then replacing it after the fact, so something like this:
string text = "my text";
text = text.Replace("||" "|"); // replace any '|' chars with a double '||' so they aren't confused in the output.
text = text.Replace("\r\n", "_|0|_"); // replace \r\n 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 |
This almost worked, it breaks down if you have to support right to left text as the right to left control sequence always ends up in the middle of the placeholder.
Sending Key Messages
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);
}
This also ends up being converted to a \par
Is there a way to post a messaged directly to the msftedit control to insert a control character?
I am totally stumped, any ideas guys? Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我的测试显示,添加 Unicode“行分隔符”(U+2028)确实有效:
当我运行程序时,我得到:
它确实添加了
\line
而不是\par< /代码>。
Adding a Unicode "Line Separator" (U+2028) does work as far as my testing showed:
When I run the program, I get:
It did add
\line
instead of\par
.由于您想使用不同的 RTF 代码,我认为您可能需要忘记简单的 AppendText() 方法并直接操作 RichTextBox 的 .Rtf 属性。下面是一个用于演示的示例(经过测试):
输出:
原始 RTF 数据:
新 RTF 数据:
结果 RTF 数据:
Since you want to use a different RTF code, I think you may need to forget about the simplistic AppendText() method and manipulate the .Rtf property of your RichTextBox directly instead. Here is a sample (tested) to demonstrate:
Output:
Original RTF data:
New RTF Data:
Resulting RTF Data:
如果您使用段落写入 Richtextbox,您可以使用 LineBreak() 与下面所示的相同代码
希望这有帮助!
if you are using paragraphs to write to richtextbox you can use the LineBreak() same code shown below
Hope this helps!