c#RTB - 粘贴没有颜色/字体的纯文本?

Tho*_*mas 6 c# richtextbox winforms

我在C#应用程序中使用Rich Text对象.我遇到的唯一问题是,当用户从另一个应用程序粘贴格式化文本时,它仍然是我不能拥有的格式.有没有办法如何只粘贴字符串并忽略格式?谢谢!

小智 29

KeyDown-event 添加处理程序以拦截标准粘贴并仅手动插入纯文本:

private void rtb_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.V)
    {
        ((RichTextBox)sender).Paste(DataFormats.GetFormat("Text"));
            e.Handled = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 哦。完美的。不过也需要 Shift+Insert 处理。 (2认同)

Bil*_*llW 9

假设WinForms:尝试这个:使用KeyDown事件处理程序定义一个RichTextBox,如下所示:

仅附加示例:

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V) 
        {
            richTextBox1.Text += (string)Clipboard.GetData("Text"); 
            e.Handled = true; 
        }
    }
Run Code Online (Sandbox Code Playgroud)

[编辑]

在当前插入点(选择开始)示例中将剪贴板RTF添加到RichTextBox:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Control && e.KeyCode == Keys.V)  
    { 
            // suspend layout to avoid blinking
            richTextBox2.SuspendLayout();

            // get insertion point
            int insPt = richTextBox2.SelectionStart;

            // preserve text from after insertion pont to end of RTF content
            string postRTFContent = richTextBox2.Text.Substring(insPt);

            // remove the content after the insertion point
            richTextBox2.Text = richTextBox2.Text.Substring(0, insPt);

            // add the clipboard content and then the preserved postRTF content
            richTextBox2.Text += (string)Clipboard.GetData("Text") + postRTFContent;

            // adjust the insertion point to just after the inserted text
            richTextBox2.SelectionStart = richTextBox2.TextLength - postRTFContent.Length;

            // restore layout
            richTextBox2.ResumeLayout();

            // cancel the paste
            e.Handled = true;
    } 
} 
Run Code Online (Sandbox Code Playgroud)

[结束编辑]

注意0:文本粘贴的要在RichTextBox的效果承担当前的风格设置:如果你有"前景色设置为"蓝色:粘贴的文本将是蓝色.

注1:这是我快速拼凑的东西,并且通过使用写字板为剪贴板创建一些多色和奇怪格式的RTF进行了几次测试:然后在运行时粘贴到RichTextBox1中:它确实剥离了所有的颜色,缩进等

由于未经过全面测试,请谨慎使用.

注2:显然,这不会处理"插入"或"通过上下文菜单粘贴"的情况.

欢迎对这个答案的所有批评,如果它不是"标记",将立即将其删除.


pas*_*sti 5

我正在寻找纯文本,richtextbox但还没有在网上找到解决方案。

为什么纯文本RichTextBox而不是TextBox? 例如,因为RichTextBox具有可用的撤消/重做功能等等。

最后我通过深入研究 Richedit 控件的 C 头文件找到了一个完美的解决方案:ARichTextBox可以切换到纯文本模式,之后它不接受来自剪贴板的格式化文本和图像以及类似的东西,并且表现得像一个正常的TextBox格式化。像图像这样的花哨的东西不能粘贴,它通过删除格式来粘贴格式化的文本。

class PlainRichTextBox : RichTextBox
{
    const int WM_USER = 0x400;
    const int EM_SETTEXTMODE = WM_USER + 89;
    const int EM_GETTEXTMODE = WM_USER + 90;

    // EM_SETTEXTMODE/EM_GETTEXTMODE flags
    const int TM_PLAINTEXT = 1;
    const int TM_RICHTEXT = 2;          // Default behavior 
    const int TM_SINGLELEVELUNDO = 4;
    const int TM_MULTILEVELUNDO = 8;    // Default behavior 
    const int TM_SINGLECODEPAGE = 16;
    const int TM_MULTICODEPAGE = 32;    // Default behavior 

    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

    bool m_PlainTextMode;

    // If this property doesn't work for you from the designer for some reason
    // (for example framework version...) then set this property from outside
    // the designer then uncomment the Browsable and DesignerSerializationVisibility
    // attributes and set the Property from your component initializer code
    // that runs after the designer's code.
    [DefaultValue(false)]
    //[Browsable(false)]
    //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public bool PlainTextMode
    {
        get
        {
            return m_PlainTextMode;
        }
        set
        {
            m_PlainTextMode = value;
            if (IsHandleCreated)
            {
                IntPtr mode = value ? (IntPtr)TM_PLAINTEXT : (IntPtr)TM_RICHTEXT;
                SendMessage(Handle, EM_SETTEXTMODE, mode, IntPtr.Zero);
            }
        }
    }

    protected override void OnHandleCreated(EventArgs e)
    {
        // For some reason it worked for me only if I manipulated the created
        // handle before calling the base method.
        PlainTextMode = m_PlainTextMode;
        base.OnHandleCreated(e);
    }
}
Run Code Online (Sandbox Code Playgroud)