Search code examples
delphiplaintextrichedit

RichEdit not respecting PlainText with pasted content


I create a new application, drop on a TRichedit and set the PlainText property to true. I then run the application and paste some rich formatted text into the RichEdit.

I would expect it to display as plain text however it shows the content with the formatting.

Anyone know how to use a TRichedit just as plain text (and not using a memo :))


Solution

  • You'll need to do the paste manually ensuring that the formatting is ignored.

    if Clipboard.HasFormat(CF_TEXT) then
      RichEdit.SelText := Clipboard.AsText; 
    

    Run this code from a message handler for WM_PASTE.

    I currently do not know how to intercept the CTRL+V keypress and replace it with this code. The WM_PASTE message is not sent to rich edit controls.


    As Cody suggests in the comment, one solution is as follows:

    • Make sure that all the text in the edit control is marked as protected.
    • Subclass TRichEdit and override CNNotify.
    • Handle the EN_PROTECTED message, and if msg=WM_PASTE then use the paste as text code above and return 1 from the message handler to indicate that the requested operation (a rich paste) is rejected.