如何使用RichTextBox控件添加到\line RTF [英] How to append \line into RTF using RichTextBox control

查看:245
本文介绍了如何使用RichTextBox控件添加到\line RTF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Microsoft RichTextBox控件,可以增加新的线路像这样...

When using the Microsoft RichTextBox control it is possible to add new lines like this...

richtextbox.AppendText(System.Environment.NewLine); // appends \r\n



但是,如果您现在查看生成的RTF的\ r\\\
字符被转换为\par不\line

However, if you now view the generated rtf the \r\n characters are converted to \par not \line

我要如何插入一个\line控制代码到生成的RTF?

How do I insert a \line control code into the generated RTF?

什么简化版,工作方式:

令牌替换

像在字符串的末尾插入令牌,然后在事后替换它,所以像这样黑客:

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.

发送关键信息

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

This also ends up being converted to a \par

有没有一种方法来发布直接传递消息到msftedit控制插入控制字符?

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!

推荐答案

添加一个Unicode行分隔符(U + 2028),并就工作,我的测试表明

Adding a Unicode "Line Separator" (U+2028) does work as far as my testing showed:

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);
}

当我运行程序时,我得到:

When I run the program, I get:

Hello, World!
Hello, World!
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Courier New;}}
{\colortbl ;\red255\green255\blue255;}
\viewkind4\uc1\pard\cf1\f0\fs17 Hello, World!\line Hello, World!\line\par
}

它没有添加 \line 而不是 \par

这篇关于如何使用RichTextBox控件添加到\line RTF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆