Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

If you do not want to allow any editing in the control you can call the function SetReadOnly(). This will disable all editing in the control. But that's not what this topic is about. The rich edit control also allows setting any text in the control to read only, somewhat akin to setting the text color. You can actually set the attribute of any selection of text to protected. Thereafter, if the user tries to change the text a EN_PROTECTED notification can be generated by the control.

Step 1: Set the ENM_PROTECTED event mask

For performance reasons, the rich edit control will generate some event notification only if the programmer has set the proper event mask. Since we are going to be using protected text, we have to enable the EN_PROTECTED notification message. We do this by calling the SetEventMask() function. If you are using a CRichEditView sub class, you can use the following code
void CMyRichEditView::OnInitialUpdate()

	long eventmask = GetRichEditCtrl().GetEventMask() | ENM_PROTECTED;
	GetRichEditCtrl().SetEventMask( eventmask );

If instead, you are deriving from CRichEditCtrl, you can set the event mask in the PreSubclassWindow() function after calling the base class version of the function.

Step 2: Add handler for EN_PROTECTED

As I've already mentioned, the EN_PROTECTED notification message is sent whenever the user tries to modify a protected text. If you don't set the event mask as explained in step 1, then the control allows changing the protected text. If you do set the event mask but do not handle the notification, then the control still allows changing the protected text. In the EN_PROTECTED handler, you can decide whether to allow the modification or not.

The class wizard does not help in adding a handler for the EN_PROTECTED notification; you will have to add it manually. First the code snippet from the header file.

	afx_msg void OnProtected(NMHDR* pNMHDR, LRESULT* pResult);

Now the message map entry in the implementation file and the function definition. In the handler we simply set the result to TRUE. This will disallow any change. If you want to allow editing in special circumstances, this is where you would add to code to decide whether to allow the edit.
BEGIN_MESSAGE_MAP(CMyRichEditView, CRichEditView)

void CMyRichEditView::OnProtected(NMHDR* pNMHDR, LRESULT* pResult) 

	// Returning non zero will disallow change
	*pResult = TRUE;

Step 3: Change the character format to protected

Now we are ready to change the attribute of any selection to protected. Here's a function that will change the currect selection to protected text.
void CMyRichEditView::SetSelectionProtected() 

	cf.cbSize = sizeof( cf );
	cf.dwMask = CFM_PROTECTED;
	cf.dwEffects = CFE_PROTECTED;

	GetRichEditCtrl().SetSelectionCharFormat( cf );


  • This known solution doesn't work with the ****ing Microsofts' RichEdit Control

    Posted by __nogc on 09/02/2006 01:37pm

    Try to protect text "{123}" by the described way. Next step: type the space character after the protected range, you give the foolowing text: "{123} ". Then click the sequence (word deleting routine). You NEVER hit into the EN_PROTECTED notification handling. Your text is looks like "{123" now - w/out closing bracket!!! ****ing Microsift

  • Unprotecting text

    Posted by Legacy on 09/11/2002 07:00am

    Originally posted by: Steven Francis

    Is this protecting a portion of the text a one stop thing as I can't seem to get it to unprotect it

    Has anybody else had a solution to this?

    • Unprotecting text

      Posted by Falko on 04/06/2005 04:56pm

      I haven't found a way to get the unprotectedflag away, but you can delete the protected text and reenter it unprotected.
      The way is described in the Article:
      void CMyRichEditView::OnProtected(NMHDR* pNMHDR, LRESULT* pResult)
      // Returning non zero will disallow change !!!!
      *pResult = TRUE;
      So if you set *pResult = FALSE
      You can delete selected Text even if it is protected.

  • Tom

    Posted by Legacy on 08/31/2002 07:00am

    Originally posted by: Tomcat

    I'm a VB Programmer and interested to use TOM (Text Object Model) Using TextDocument COM Based and its Subsequent Components (textfont,textpara,..) to manipulate rich textbox Content. Also like to know how can I change backcolor of a word or paragraph of rtf text.

    best regards!

    • Why nobody answers on your questions??? To change the backcolor

      Posted by __nogc on 09/02/2006 01:42pm

      Use the ITextRange.ITextFont.BackColor property

  • Sample of EN_PROTECTED

    Posted by Legacy on 04/15/2002 07:00am

    Originally posted by: Zarin Thomas

    Could you pls give us the sample code for setting the protected flag. I could not find any samples for doing this

  • Rich Edit

    Posted by Legacy on 03/29/2002 08:00am

    Originally posted by: Avin

    I am creating gui wizard. One topicc in that is to direct program output to display, So I don't know what to select List Box or Rich Edit. By reading this topic I think Rich Edit with read only would be the good idea. Can any one suggest how to direct buffer text or file text to rich edit control in read only method, any example would be nice.

  • How to enable scroll Bar in a DISABLED Rich edit control

    Posted by Legacy on 12/31/2001 08:00am

    Originally posted by: SUBIN

    Please reply if you know "How to enable scroll Bar in a DISABLED Rich edit control" Thanks

  • Is there a way to deprotect the protected RANGE?

    Posted by Legacy on 08/12/2001 07:00am

    Originally posted by: Jackioth

    Thank you for your PROTECT code in this article, however, in some situations I have to deprotect my RANGE that I protected before. I tried below code, but I failed.

    CHARFORMAT cf; cf.cbSize = sizeof( cf );
    cf.dwMask = CFM_PROTECTED;
    cf.dwEffects &= ~CFE_PROTECTED;
    GetRichEditCtrl().SetSelectionCharFormat( cf );

    Have you ever encountered this kind of problems? Please give me some advices. Thanks!

  • Rich Edit Control

    Posted by Legacy on 08/03/2000 07:00am

    Originally posted by: Kevin Schubach

    Add AfxInitRichEdit() under InitInstance()
    and the dialog box opens. But the RichText Control
    doesn't accept files (OLE) how do you enable this?
    Do you have a sample?

  • Using this technique with CRichEditCtrl on a dialog

    Posted by Legacy on 10/16/1998 07:00am

    Originally posted by: Kevin Vasquez

    I can not make this work using CRichEditCtrl on a dialog. You mention how to do this using CRichEditCtrl, have you actually done it? If so do you have an example

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date