I need to prohibit copying text from RichEdit I decided to redefine WM_COPY on 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

  • WM_COPY used for Plain text . Perhaps it is easier to disable text selection in (for example) OnSelectionChange ? - kami
  • c OnSelectionChange not a very good option, since the user can select and delete selected text, for example. it is allowed but not copied - gregor

1 answer 1

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 number 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; 
  • It is not quite clear how to prohibit copying from RichEdit - gregor from the original piece of code (Option 2)