【问题标题】:C#: Paste RTF in RichTextBox but keep coloring and formatting (i.e: bold, underline, etc)C#:在 RichTextBox 中粘贴 RTF,但保留颜色和格式(即:粗体、下划线等)
【发布时间】:2011-07-25 13:29:15
【问题描述】:

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

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

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

这样的事情可能吗?

我使用winforms。

谢谢

【问题讨论】:

  • 您的问题的答案已在我的帖子中找到。 “我使用 winforms。”
  • 我很确定完成阅读是提出问题的先决条件,但谢谢你,我会把它添加到标签中;)。
  • 你没那么快 ;-) 很抱歉打扰了。

标签: c# winforms formatting richtextbox paste


【解决方案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 的字体更改为粘贴时插入符号位置之前的字符的字体。
如果您粘贴的文本很大(呃),我认为这会很快出现问题。此外,这可以通过某种方式进行优化,即它只为一行中的所有字符设置一次字体,其基本字体与 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;
}

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

【讨论】:

    【解决方案2】:

    如果剪贴板上的 RTF 包含带有 /font 指令的片段,显然这不会按您希望的方式工作。这很有可能。过滤 RTF 片段只能通过粘贴到帮助程序 RichTextBox 中来实现。使用 SelectionFont 属性,然后将其复制回剪贴板和 Paste()。或者直接做:

            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;
    

    【讨论】:

    • 我想提供完全相同的代码。但是,它不起作用,因为它还会删除粗体或斜体等修饰符!
    • 可以通过循环使用相同字体的片段来完成。这需要一次迭代文本一个字符,直到 SelectionFont 更改。我将把它留给 OP。
    • 感谢汉斯和丹尼尔。我不太明白最后一部分需要我遍历文本。如果我按照您发布的代码进行操作,我将在粘贴后覆盖字体(即:richTextBox1.SelectionFont =richTextBox1.Font;),这是否意味着 SelectionFont 属性永远不会改变?
    • 好吧,使用任何你想要的字体。关键是您更改任何粘贴到您想要的字体的字体。
    • 谢谢你们,我希望我可以将两个答案标记为已接受。
    【解决方案3】:

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

    首先,处理 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 有时会引发异常,即使它成功地将文本复制到剪贴板。您当然可以使用剪贴板提供的其他方法来获取/设置文本,这只是解决方案的基本版本。

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

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

    希望这会有所帮助!

    【讨论】:

      【解决方案4】:

      老了,我知道;丹尼尔的回答对我有用,但前提是我替换了richTextBox1.Selection 的任何实例并简单地引用整个richTextBox1 的字体和大小。在这种情况下,我粘贴的任何 RTF 都将继承 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;
      

      }

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 您好,感谢您的回答。粘贴工作正常,但尝试在 Word 中调整文本大小,然后将其粘贴到 RichTextBox 中。 RichTextBox 中的文本将与 Word 文档中的文本大小相同。我希望它与我的 RichTextBox 的基本字体大小相同。
        猜你喜欢
        • 2010-12-30
        • 1970-01-01
        • 2010-09-16
        • 1970-01-01
        • 2016-11-18
        • 1970-01-01
        • 2019-04-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多