delphidetectpasteinterceptmemo

How to intercept (detect) a Paste command into a TMemo?


How to catch Paste command and change text of Clipboard before that text is pasted into a TMemo, but, after Paste, text in Clipboard must be same like before changing?

Example, Clipboard have text 'Simple Question', text that go in the TMemo is 'Симплe Qуeстиoн', and after that text in Clipboard is like before changing, 'Simple Question'.


Solution

  • Derive 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 would like to prohibit any paste operation at all, empty the WMPaste handler.