首页 文章

C#:在RichTextBox中粘贴RTF,但保持着色和格式化(即:粗体,下划线等)

提问于
浏览
2

是否可以将文本粘贴到富文本框中,同时保持字体在粘贴内容的富文本框中使用?

换句话说,我想从Word中复制一些格式化的内容(即:使用字体X并加下划线和蓝色的文本),然后将其粘贴到我的RichTextBox中 .

我希望粘贴的内容与我的RichTextBox具有相同的字体,但保留其原始着色和下划线 .

这样的事情可能吗?

我用winforms .

谢谢

5 回答

  • 1

    开箱即用是不可能的 . 但你可以这样做:

    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的字体更改为粘贴时插入符号位置前面的字符的字体 .
    如果您粘贴的文本很大(呃),我认为这会很快出问题 . 另外,这可以通过某种方式进行优化,它只为汉堡建议使用相同基本字体的行中的所有字符设置一次字体 .

    更新:
    以下是优化版本,它使用相同的原始字体设置连接字符集的字体:

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

    这是非常难看的代码,我相信它可以改进和清理 . 但它做到了它应该做的事情 .

  • 1

    显然,如果剪贴板上的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;
    
  • 1

    我知道这有点晚了,但我遇到了同样的问题,这是我的解决方案(希望这将有助于其他人):

    首先,处理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)
                    {
                    }
                }
            }
    

    说明:我首先要说的是,这只是用.NET 4.0测试过的 . 假设所使用的所有函数都没有被更改,这也适用于旧版本的.NET .

    调用Clipboard.GetText()以纯文本格式(不包括RTF标记)返回内容 . 然后我们通过使用从Clipboard.GetText()获取的纯文本调用Clipboard.SetText()来更改要粘贴的文本 . 现在,当事件完成并传递给控件时,它将执行粘贴从剪贴板中获取最新文本(我们的更改版本) . 它被包装在try / catch块中的原因是因为SetText有时会抛出异常,即使它成功地将文本复制到剪贴板 . 您当然可以使用Clipboard提供的其他方法来获取/设置文本,这只是解决方案的基本版本 .

    新粘贴的文本将继承光标位置的格式,类似于手动键入RTB .

    不幸的是,这也将删除文本的样式(粗体,着色等)

    希望这可以帮助!

  • 0

    老我知道; Daniel的回答对我有用,但前提是我替换了richTextBox1.Selection的任何实例,只是简单地引用整个richTextBox1的字体和大小 . 在这种情况下,任何RTF I粘贴都将继承richTextBox1当前正在使用的fontfamily和fontsize,同时保留和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;
    

    }

  • 3

    我试图从word文档中复制文本并在运行时将其粘贴到RichTextBox . 一切正常 . 我没有调整任何具体的东西 . 只需将RichTextBox放到表单上,然后从MS Word文档中复制格式化文本即可 .

相关问题