行から行へのキャレット位置を維持する方法のため、通常のテキストボックスではなく、RichTextBoxを使用する必要があります。ただし、貼り付けても常に同じフォントで表示する必要があります。
現時点では、テキスト全体を選択してフォントを元のフォント(Lucida Console)に変更していますが、青く点滅するため、貼り付けるとひどい感じになります。
行から行へのキャレット位置を維持する方法のため、通常のテキストボックスではなく、RichTextBoxを使用する必要があります。ただし、貼り付けても常に同じフォントで表示する必要があります。
現時点では、テキスト全体を選択してフォントを元のフォント(Lucida Console)に変更していますが、青く点滅するため、貼り付けるとひどい感じになります。
プログラムで貼り付けを処理している場合は、貼り付け方法を使用しないでください。代わりに、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
}
}
ダリンの方法は、カレットの位置を無視し、常にテキストの最後に追加します。
実際には、もっと良い方法があります。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
フォントは常にWingDingsに変更されます。私はこれをMSWordからコピーしました:
具体的には、上記の@idnで説明したプレーンテキスト形式の方法では、実際にはプレーンテキストを貼り付けるだけでしたが、フォントも変更されたという問題が発生していました。
次のコードは、KeyUpイベントを処理して、すべてのテキストを選択し、元の色とフォント(つまりフォーマット)を置き換えるだけです。これがちらつきとして画面に表示されないようにするために、ウィンドウの再描画イベントを無効にする特別な方法が採用されました。コントロール描画の無効化は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;