delphi – How to prevent copying to clipboard

Question:

I need to prevent copying text from RichEdit I decided to override WM_COPY in my component

TArticleRichEdit = class(TRichEdit)
  private
    procedure WMCopy(var Message: TWMCopy); message WM_COPY;
  end;

procedure TArticleRichEdit.WMCopy(var Message: TWMCopy);
begin
//
end;

But for some reason, when copying ctrl + c or ctrl + ins, the handler does not work

Answer:

there are many options, for example

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
 if (Key=#22) or (Key=#3) then Key:=#0;   // 22 = [Ctrl+V] / 3 = [Ctrl+C]
end;

option №2 ( original )

...
 var
   Form1: TForm1;
   NextInChain : THandle;
 implementation
 uses ClipBrd; 
...

procedure WMDrawClipboard(var Msg: TMessage) ; message WM_DRAWCLIPBOARD;  
procedure WMChangeCBChain(var Msg: TMessage) ; message WM_CHANGECBCHAIN;
...

procedure TForm1.WMDrawClipboard(var Msg:TMessage) ; 
begin
  if Clipboard.HasFormat(cf_text) then
  begin
   Memo1.Lines.Clear;
   Memo1.PasteFromClipboard
  end
  else
  begin
   // работа других форматов
  end;  
  //pass the message on to the next window  
if NextInChain <> 0 then
   SendMessage(NextInChain, WM_DrawClipboard, 0, 0) 
end;

procedure TForm1.WMChangeCBChain(var Msg: TMessage) ;
 var   Remove, Next: THandle;
 begin
   Remove := Msg.WParam;
   Next := Msg.LParam;
   with Msg do
   if NextInChain = Remove then
    NextInChain := Next
   else
   if NextInChain <> 0 then
    SendMessage(NextInChain, WM_ChangeCBChain, Remove, Next) 
end;
...

procedure TForm1.FormCreate(Sender: TObject) ; 
begin
  NextInChain := SetClipboardViewer(Handle) ; 
end;
Scroll to Top