Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type
- TRichEditBeforePasteEvent = procedure(Sender: TObject; var Content: string; var CanPaste: Boolean) of object;
- TRichEdit = class(Vcl.ComCtrls.TRichEdit)
- private
- FOnAfterPaste: TNotifyEvent;
- FOnBeforePaste: TRichEditBeforePasteEvent;
- procedure CNNotify(var AMessage: TWMNotifyRE); message CN_NOTIFY;
- published
- property OnAfterPaste: TNotifyEvent read FOnAfterPaste write FOnAfterPaste;
- property OnBeforePaste: TRichEditBeforePasteEvent read FOnBeforePaste write FOnBeforePaste;
- end;
- implementation
- { TRichEdit }
- procedure TRichEdit.CNNotify(var AMessage: TWMNotifyRE);
- var
- Content: string;
- CanPaste: Boolean;
- begin
- if (AMessage.NMHdr.code = EN_PROTECTED) and (AMessage.ENProtected.msg = WM_PASTE) then
- begin
- Content := Clipboard.AsText;
- CanPaste := True;
- if Assigned(FOnBeforePaste) then
- begin
- FOnBeforePaste(Self, Content, CanPaste);
- // modify the clipboard (I don't like if someone touches what I copy there, as a
- // small workaround for that might be the try..finally block which would finally
- // restore the clipboard to the state before (and if) the OnBeforePaste modified
- // the content
- Clipboard.Clear;
- Clipboard.AsText := Content;
- end;
- // return 0 if the action is allowed; non-zero otherwise
- AMessage.Result := IfThen(CanPaste, 0, 1);
- // here I don't know how to process the "outgoing" message
- if Assigned(FOnAfterPaste) then
- FOnAfterPaste(Self);
- end
- else
- inherited;
- end;
- // you must set the DefAttributes.Protected to True to get the above notification
- RichEdit1.DefAttributes.Protected := True;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement