winapirichedittext-formatting

Rich Edit Control: Prevent Rich Formatting?


How do you prevent the user from changing anything other than the text in a Win32 Rich Edit control?

(i.e. They shouldn't be able to change the formatting of any text, add graphics, etc.; if they copy-paste new text, only the text should be kept, and associated formatting should be discarded.)


Solution

  • I've never found a particularly elegant way to handle this: what I've done in the past is:

    1) Catch WM_KEYDOWN messages for the control and discard all formatting keys (Ctrl+E,J,R,L,1,2,5,+, and Ctrl+Shift+A,7)

    2) Catch all paste operations by catching WM_COMMAND messages with an id of ID_EDIT_PASTE, and replace the paste message with a message EM_PASTESPECIAL,CF_UNICODETEXT to the control. (This is with MFC: Depending on what framework or language you're using, this may require catching Ctrl+V and similar rather than ID_EDIT_PASTE.)

    Not pretty, I conceed, but it seems to work.