3

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

user541686
  • 205,094
  • 128
  • 528
  • 886

3 Answers3

3

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.

DavidK
  • 3,929
  • 1
  • 19
  • 26
3

This answer is probably a bit late but for anybody else looking for an answer to this questions, the best way that I've found have total control of paste operations in a Rich edit control is to provide an implementation of IRichEditOleCallback::QueryAcceptData and then return S_FALSE to stop them all together or to filter out certain clip board formats by changing the lpcfFormat parameter.

The CRichEditView::QueryAcceptData function in MFC provides an excellent example of how this can be done. This will work for all kinds of paste operations including drag and drop so is the best way to get full control of what happens.

peterchen
  • 40,917
  • 20
  • 104
  • 186
Karl Edwall
  • 231
  • 2
  • 8
2

Even later :)

SendMessage(wndEdit, EM_SETEDITSTYLE, SES_EMULATESYSEDIT, SES_EMULATESYSEDIT)

seems to do the trick: paste pastes plain text, and the formatting hot keys are disabled.

SES_EMULATESYSEDIT: When this bit is on, rich edit attempts to emulate the system edit control (default: 0).

You still retain some of the "bonus" features of the richedit, such as scrollbars on demand.

Note: While this will prevent the pasting of richly formatted text into the RichEdit control, it will also prevent you from programatically formatting the text; all rich formatting is disabled.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
peterchen
  • 40,917
  • 20
  • 104
  • 186