Advertisement
TLama

Untitled

Mar 23rd, 2015
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.65 KB | None | 0 0
  1. type
  2.   TRichEditBeforePasteEvent = procedure(Sender: TObject; var Content: string; var CanPaste: Boolean) of object;
  3.  
  4.   TRichEdit = class(Vcl.ComCtrls.TRichEdit)
  5.   private
  6.     FOnAfterPaste: TNotifyEvent;
  7.     FOnBeforePaste: TRichEditBeforePasteEvent;
  8.     procedure CNNotify(var AMessage: TWMNotifyRE); message CN_NOTIFY;
  9.   published
  10.     property OnAfterPaste: TNotifyEvent read FOnAfterPaste write FOnAfterPaste;
  11.     property OnBeforePaste: TRichEditBeforePasteEvent read FOnBeforePaste write FOnBeforePaste;
  12.   end;
  13.  
  14. implementation
  15.  
  16. { TRichEdit }
  17. procedure TRichEdit.CNNotify(var AMessage: TWMNotifyRE);
  18. var
  19.   Content: string;
  20.   CanPaste: Boolean;
  21. begin
  22.   if (AMessage.NMHdr.code = EN_PROTECTED) and (AMessage.ENProtected.msg = WM_PASTE) then
  23.   begin
  24.     Content := Clipboard.AsText;
  25.     CanPaste := True;
  26.  
  27.     if Assigned(FOnBeforePaste) then
  28.     begin
  29.       FOnBeforePaste(Self, Content, CanPaste);
  30.       // modify the clipboard (I don't like if someone touches what I copy there, as a
  31.       // small workaround for that might be the try..finally block which would finally
  32.       // restore the clipboard to the state before (and if) the OnBeforePaste modified
  33.       // the content
  34.       Clipboard.Clear;
  35.       Clipboard.AsText := Content;
  36.     end;
  37.     // return 0 if the action is allowed; non-zero otherwise
  38.     AMessage.Result := IfThen(CanPaste, 0, 1);
  39.  
  40.     // here I don't know how to process the "outgoing" message
  41.     if Assigned(FOnAfterPaste) then
  42.       FOnAfterPaste(Self);
  43.   end
  44.   else
  45.     inherited;
  46. end;
  47.  
  48. // you must set the DefAttributes.Protected to True to get the above notification
  49. RichEdit1.DefAttributes.Protected := True;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement