本文介绍了C#:在粘贴的RichTextBox RTF,但保持颜色和格式(即:粗体,下划线等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 是否可以将文本粘贴到一个格式文本框,同时保持字体,格式文本框被用于粘贴的内容?Is it possible to paste text into a Rich Text Box, while keeping the font being used in the Rich Text Box for the pasted content ?在换句话说,我想从Word时格式化(即:使用的字体X和带下划线,并以蓝色文本)复制的东西,然后将其粘贴在我的RichTextBoxIn other words, I'd like to copy something from Word that is formated (i.e: a text that uses a font X and is underlined and in blue), and then paste it in my RichTextBox.我想粘贴的内容具有相同的字体作为我的RichTextBox,但保持原有的色彩和下划线。I would like the pasted content to have the same font as that of my RichTextBox but keep its original coloring and underlining.这种事可能吗?我使用的WinForms。I use winforms.感谢推荐答案这是不可能的开箱。但是你可以做这样的事情:This is not possible out of the box. But you can do something like this:public void SpecialPaste(){ var helperRichTextBox = new RichTextBox(); helperRichTextBox.Paste(); for(int i=0;i<helperRichTextBox.TextLength;++i) { helperRichTextBox.SelectionStart = i; helperRichTextBox.SelectionLength = 1; helperRichTextBox.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size,helperRichTextBox.SelectionFont.Style); } richTextBox1.SelectedRtf = helperRichTextBox.Rtf;}这改变了粘贴RTF到该字符的插入符前的字体在贴的时间位置。结果我以为会得到问题的非常快,如果粘贴文本大(ER)。 。此外,这可以在某种程度上进行优化,它设置的字体只有一次的所有字符与汉斯·建议使用同一基本字体的行This changes the font of the pasted RTF to that of the character preceding the caret position at the time of the paste.I assume that will get problematic pretty fast, if the text you paste is large(er). Additionally, this can be optimized in a way, that it sets the font only once for all characters in a row with the same base font as Hans suggests.更新: 这里是优化版本,即设置字体用于与相同的原始字体的连接的字符集:Update:Here is the optimized version, that sets the font for a connected set of characters with the same original font:public void SpecialPaste(){ var helperRichTextBox = new RichTextBox(); helperRichTextBox.Paste(); helperRichTextBox.SelectionStart = 0; helperRichTextBox.SelectionLength = 1; Font lastFont = helperRichTextBox.SelectionFont; int lastFontChange = 0; for (int i = 0; i < helperRichTextBox.TextLength; ++i) { helperRichTextBox.SelectionStart = i; helperRichTextBox.SelectionLength = 1; if (!helperRichTextBox.SelectionFont.Equals(lastFont)) { lastFont = helperRichTextBox.SelectionFont; helperRichTextBox.SelectionStart = lastFontChange; helperRichTextBox.SelectionLength = i - lastFontChange; helperRichTextBox.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, helperRichTextBox.SelectionFont.Style); lastFontChange = i; } } helperRichTextBox.SelectionStart = helperRichTextBox.TextLength-1; helperRichTextBox.SelectionLength = 1; helperRichTextBox.SelectionFont = new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size, helperRichTextBox.SelectionFont.Style); richTextBox1.SelectedRtf = helperRichTextBox.Rtf;} 这是非常丑陋的代码,我相信它可以改善和清洁。但是它做什么它应该。It's pretty ugly code and I am sure it can be improved and cleaned. But it does what it should. 这篇关于C#:在粘贴的RichTextBox RTF,但保持颜色和格式(即:粗体,下划线等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-01 03:26