How to intercept (detect) the Paste command in TMemo?

How to catch the “Paste” command and change the text of the clipboard before this text is pasted into TMemo, but after pasting the text in the clipboard should be the same as before the change?

For example, in Clipboard there is the text “Simple Question”, the text that is included in TMemo is “Simple Question”, and after that the text in Clipboard is similar before changing “Simple Question”.

+7
source share
2 answers

Output a new control that descends from "TMemo" to intercept the WM_PASTE message:

 type TPastelessMemo = class(TMemo) protected procedure WMPaste(var Message: TWMPaste); message WM_PASTE; end; uses clipbrd; procedure TPastelessMemo.WMPaste(var Message: TWMPaste); var SaveClipboard: string; begin SaveClipboard := Clipboard.AsText; Clipboard.AsText := 'Simple Question'; inherited; Clipboard.AsText := SaveClipboard; end; 

If you want to completely prohibit any insertion operation, clear the WMPaste handler.

+15
source

This is an alternative to Sertac's excellent answer, which is to override the WndProc control:

 // For detecting WM_PASTE messages on the control OriginalMemoWindowProc: TWndMethod; procedure NewMemoWindowProc(var Message: TMessage); //... // In the form OnCreate procedure: // Hijack the control WindowProc in order to detect WM_PASTE messages OriginalMemoWindowProc := myMemo.WindowProc; myMemo.WindowProc := NewMemoWindowProc; //... procedure TfrmMyForm.NewMemoWindowProc(var Message: TMessage); var bProcessMessage: Boolean; begin bProcessMessage := True; if (Message.Msg = WM_PASTE) then begin // Data pasted into the memo! if (SomeCondition) then bProcessMessage := False; // Do not process this message any further! end; if (bProcessMessage) then begin // Ensure all (valid) messages are handled! OriginalMemoWindowProc(Message); end; end; 
+3
source

All Articles