是否可以将文本粘贴到富文本框中,同时保留富文本框中使用的字体?
换句话说,我想从Word复制一些格式化的内容(即使用字体X并带有下划线和蓝色的文本),然后将其粘贴到我的RichTextBox中。
我希望被粘贴的内容具有与RichTextBox相同的字体,但保留其原始颜色和下划线。
这种情况是否可能?
我使用winforms。
谢谢
是否可以将文本粘贴到富文本框中,同时保留富文本框中使用的字体?
换句话说,我想从Word复制一些格式化的内容(即使用字体X并带有下划线和蓝色的文本),然后将其粘贴到我的RichTextBox中。
我希望被粘贴的内容具有与RichTextBox相同的字体,但保留其原始颜色和下划线。
这种情况是否可能?
我使用winforms。
谢谢
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字体,使其与插入符号位置前的字符字体相同。
如果你粘贴的文本较大,这可能会很快变得棘手。此外,可以进行优化,仅对具有与Hans建议相同基本字体的一行中的所有字符设置字体。
更新:
以下是经过优化的版本,它为具有相同原始字体的一组连接字符设置字体:
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;
}
这段代码比较丑陋,我相信它可以得到改进和优化。但是它确实完成了应该完成的任务。
如果剪贴板上的RTF包含/font指令片段,那么显然这种方法无法按照您的意愿工作。这种情况非常普遍。过滤RTF片段只能通过将其粘贴到辅助RichTextBox中来实现。使用SelectionFont属性,然后将其复制回剪贴板并进行粘贴操作。或者直接这样做:
int oldpos = richTextBox1.SelectionStart;
richTextBox1.SelectionLength = 0;
richTextBox1.Paste();
int newpos = richTextBox1.SelectionStart;
richTextBox1.SelectionStart = oldpos;
richTextBox1.SelectionLength = newpos - oldpos;
richTextBox1.SelectionFont = richTextBox1.Font;
richTextBox1.SelectionStart = newpos;
我知道这个方法有些老旧;但是丹尼尔的回答对我有用,只需要将所有实例的richTextBox1.Selection替换为直接引用整个richTextBox1的字体和大小即可。在这种情况下,我粘贴的任何RTF都将继承当前richTextBox1正在使用的字体系列和字号,同时保留RTF样式。
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.Font.FontFamily, richTextBox1.Font.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.Rtf = helperRichTextBox.Rtf;
}
我知道有点晚了,但我遇到了同样的问题,这是我的解决方案(希望能帮助其他人):
首先,处理RichTextBox的KeyDown事件:
this.richTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.RichTextBoxKeyDown);
private void RichTextBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.V)
{
try
{
Clipboard.SetText(Clipboard.GetText());
}
catch (Exception)
{
}
}
}