4

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 :))

Wizzard
  • 12,582
  • 22
  • 68
  • 101
  • Yes... ""To ignore the rich text information encoded in a file, set PlainText to true before streaming the text to the control. "" so I have set that to True, but not ignored. Or am I missing something :) – Wizzard May 18 '11 at 04:45

1 Answers1

6

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.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    Looks like you might be able to [handle the `EN_PROTECTED` notification](http://stackoverflow.com/questions/2250759/how-does-a-cricheditctrl-know-a-paste-operation-has-been-performed). Strange indeed that rich edit controls don't receive `WM_PASTE`. Good thing I haven't spent too much time with them. – Cody Gray - on strike May 18 '11 at 08:31
  • Perhaps you could use the `OnProtectChange` event. – Ondrej Kelle May 18 '11 at 11:12
  • Or, if that's not sufficient, you could try to handle `CN_NOTIFY` in a descendant and handle cases when `NMHdr^.code = WM_PASTE`. – Ondrej Kelle May 18 '11 at 11:18
  • @TOndrej sadly the Delphi `OnProtectChange` message has lost the useful information that identifies that this is a paste as opposed to some other change. – David Heffernan May 18 '11 at 11:30
  • That was my impression. In that case probably CN_NOTIFY is the easiest. I should correct my comment above, I meant `PENProtected(NMHdr)^.msg = WM_PASTE`. – Ondrej Kelle May 18 '11 at 11:34