Setting selected text to read-only


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()
{
	CRichEditView::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(CMyRichEditView)
	:
	:
	//}}AFX_MSG
	afx_msg void OnProtected(NMHDR* pNMHDR, LRESULT* pResult);
	DECLARE_MESSAGE_MAP()

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)
	//{{AFX_MSG_MAP(CMyRichEditView)
	:
	:
	//}}AFX_MSG_MAP
	ON_NOTIFY_REFLECT(EN_PROTECTED, OnProtected)
END_MESSAGE_MAP()


void CMyRichEditView::OnProtected(NMHDR* pNMHDR, LRESULT* pResult) 
{
	ENPROTECTED* pENPROTECTED = (ENPROTECTED* )pNMHDR;


	// 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() 
{
	CHARFORMAT cf;

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

	GetRichEditCtrl().SetSelectionCharFormat( cf );
}



Comments

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

    Posted by __nogc on 09/02/2006 06:37am

    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

    Reply
  • Unprotecting text

    Posted by Legacy on 09/11/2002 12: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?
    Cheers

    • Unprotecting text

      Posted by Falko on 04/06/2005 09:56am

      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)
      {
      ENPROTECTED* pENPROTECTED = (ENPROTECTED* )pNMHDR;
      
      
      // Returning non zero will disallow change !!!!
      *pResult = TRUE;
      }
      
      So if you set *pResult = FALSE
      You can delete selected Text even if it is protected.

      Reply
    Reply
  • Tom

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

    Originally posted by: Tomcat

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

    Thanks
    best regards!

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

      Posted by __nogc on 09/02/2006 06:42am

      Use the ITextRange.ITextFont.BackColor property

      Reply
    Reply
  • Sample of EN_PROTECTED

    Posted by Legacy on 04/15/2002 12: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

    Reply
  • Rich Edit

    Posted by Legacy on 03/29/2002 12: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.
    Thanks.

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

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

    Originally posted by: SUBIN

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

    Reply
  • Is there a way to deprotect the protected RANGE?

    Posted by Legacy on 08/12/2001 12: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!

    Reply
  • Rich Edit Control

    Posted by Legacy on 08/03/2000 12: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?

    Reply
  • Using this technique with CRichEditCtrl on a dialog

    Posted by Legacy on 10/16/1998 12: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
    Thanks
    Kevin

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

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds