我需要使用RichTextBox,而不是普通的文本框,因爲它保持插入位置的方式不同。 但即使粘貼,我也需要始終保持相同字體的文字。RichTextBox - 保留原始格式(字體),即使粘貼後
目前,我已經選擇了整個文本並將字體更改爲原始(Lucida控制檯),但當它粘貼到藍色閃爍時,它看起來很可怕。
我需要使用RichTextBox,而不是普通的文本框,因爲它保持插入位置的方式不同。 但即使粘貼,我也需要始終保持相同字體的文字。RichTextBox - 保留原始格式(字體),即使粘貼後
目前,我已經選擇了整個文本並將字體更改爲原始(Lucida控制檯),但當它粘貼到藍色閃爍時,它看起來很可怕。
如果您正在以編程方式處理粘貼,請不要使用Paste方法。而是使用Clipboard.GetDataObject()的GetData(DataFormats.Text)來獲得文本字符串,然後添加使用RTF或Text屬性RichTextBox的文本:
string s = (string)Clipboard.GetDataObject().GetData(DataFormats.Text);
richTextBox.Text += s;
否則,你可以處理按Ctrl + V按鍵:
void RichTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control == true && e.KeyCode == Keys.V)
{
string s = (string)Clipboard.GetDataObject().GetData(DataFormats.Text);
richTextBox.Text += s;
e.Handled = true; // disable Ctrl+V
}
}
Darin的方法忽略插入位置,並始終追加到文本的末尾。
其實還有更好的方法。使用RichTextBox.Paste()的超負荷:
DataFormats.Format plaintext_format = DataFormats.GetFormat(DataFormats.Text);
this.Paste(plaintext_format);
工程就像我的魅力。
兩個@Darin & @ IDN的回答都不錯,但是粘貼下面的富文本什麼時候能找到既不工作:
This is text after an arrow.
This is a new line
字體總是改爲宋體。我從MS Word中複製此:
具體來說,上述@idn確實只貼上純文字描述的純文本格式的方法,但什麼是發生在該字體被改變了。
以下代碼處理KeyUp事件以僅選擇所有文本並替換其原始顏色和字體(即格式)。爲了確保這在屏幕上不可見作爲閃爍,使用了special method of disabling window repaint events。控件繪製禁用發生在KeyDown事件中,RichTextBox控件自己處理粘貼事件,然後在最後重新啓用控制繪圖。最後,這只是發生了CTL + V和SHIFT + INS,這兩者都是標準粘貼命令:
/// <summary>
/// An application sends the WM_SETREDRAW message to a window to allow changes in that
/// window to be redrawn or to prevent changes in that window from being redrawn.
/// </summary>
private const int WM_SETREDRAW = 11;
private void txtRichTextBox_KeyDown(object sender, KeyEventArgs e)
{
// For supported Paste key shortcut combinations, suspend painting
// of control in preparation for RTF formatting updates on KeyUp
if ((e.Control && !e.Shift && !e.Alt && e.KeyCode == Keys.V) || // CTL+V
(!e.Control && e.Shift && !e.Alt && e.KeyCode == Keys.Insert)) // SHIFT+INS
{
// Send Suspend Redraw message to avoid flicker. Drawing is
// restored in txtRichTextBox_KeyUp event handler
// [this.SuspendLayout() doesn't work properly]
Message msgSuspendUpdate = Message.Create(
txtRichTextBox.Handle, WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero);
NativeWindow window = NativeWindow.FromHandle(txtRichTextBox.Handle);
window.DefWndProc(ref msgSuspendUpdate);
}
}
private void txtRichTextBox_KeyUp(object sender, KeyEventArgs e)
{
// Following supported Paste key shortcut combinations, restore
// original formatting, then resume painting of control.
if ((e.Control && !e.Shift && !e.Alt && e.KeyCode == Keys.V) || // CTL+V
(!e.Control && e.Shift && !e.Alt && e.KeyCode == Keys.Insert)) // SHIFT+INS
{
// Layout already suspended during KeyDown event
// Capture cursor position. Cursor will later be placed
// after inserted text
int selStart = txtRichTextBox.SelectionStart;
int selLen = txtRichTextBox.SelectionLength;
// Replace all text with original font & colours
txtRichTextBox.SelectAll();
txtRichTextBox.SelectionFont = txtRichTextBox.Font;
txtRichTextBox.SelectionColor = txtRichTextBox.ForeColor;
txtRichTextBox.SelectionBackColor = txtRichTextBox.BackColor;
// Restore original selection
txtRichTextBox.SelectionStart = selStart;
txtRichTextBox.SelectionLength = selLen;
txtRichTextBox.ScrollToCaret();
// Resume painting of control
IntPtr wparam = new IntPtr(1); // Create a C "true" boolean as an IntPtr
Message msgResumeUpdate = Message.Create(
txtRichTextBox.Handle, WM_SETREDRAW, wparam, IntPtr.Zero);
NativeWindow window = NativeWindow.FromHandle(txtRichTextBox.Handle);
window.DefWndProc(ref msgResumeUpdate);
txtRichTextBox.Invalidate();
txtRichTextBox.Refresh();
}
}
這種方法的一個需要注意的是,因爲該事件不抑制(e.Handled = true;
),標準CTL支持+ Z(撤消)操作。但是,此過程也會循環撤消格式更改。我不認爲這是一個大問題,因爲下次粘貼文本時,格式將再次被刪除。
這種方法並不完美,因爲如果將文本從RichTextBox複製並粘貼到另一個應用程序中,則新應用的格式仍然存在,但在我看來,這比失去撤消功能要好。如果撤消功能並不重要,然後更換文本選擇和格式應用程序與替換的文字刪除所有的格式,按照這樣的回答:https://stackoverflow.com/a/1557270/3063884
var t = txtRichTextBox.Text;
txtRichTextBox.Text = t;
也許使用'richTextBox.SelectedText = S;'相反,否則光標會在粘貼後跳回到開頭。 – 2016-04-30 01:06:00