2011-01-06 91 views
15

當使用Microsoft RichTextBox控件,可以增加新的線路像這樣追加\行成RTF ...如何使用RichTextBox控件

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

但是,如果您現在查看生成的RTF的\ r \ n字符轉換爲\ par不行\行

如何在生成的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); 
} 

這也最終被轉換成\相提並論

有沒有一種方法來發布直接傳遞消息到msftedit控制插入控制字符?

我完全難住,任何想法傢伙?謝謝你的幫助!

回答

17

添加一個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! 
{\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

+0

但是,注意,mono的RichTextBox仿真相當破碎,除其他不兼容外,不理解行分隔符。它在文本中顯示爲一個箱子字符。 – 2012-01-15 11:42:37

+0

這對我來說就像一個魅力,它允許我刪除我們已經到位的可怕的黑客。我們的客戶會感謝你! – 2012-01-25 20:25:58

6

既然您想使用不同的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\par 
} 

新RTF數據:

 
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}} 
\viewkind4\uc1\pard\f0\fs17\par 
example text0\line 
example text1\line 
example text2\line 
example text3\line 
example text4\line 
} 

所得RTF數據:

 
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}} 
\viewkind4\uc1\pard\f0\fs17\par 
example text0\line example text1\line example text2\line example text3\line example text4\par 
} 
5

,如果您使用的段落寫信給RichTextBox中您可以使用換行符()圖所示

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; 

希望這有助於相同的代碼!

相關問題