Setting selected text to read-only
Posted
by Zafir Anjum
on October 1st, 2002
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 codevoid 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:37amTry 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
ReplyUnprotecting text
Posted by Legacy on 09/11/2002 12:00amOriginally 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
-
ReplyUnprotecting text
Posted by Falko on 04/06/2005 09:56amI 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.ReplyTom
Posted by Legacy on 08/31/2002 12:00amOriginally 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!
-
ReplyWhy nobody answers on your questions??? To change the backcolor
Posted by __nogc on 09/02/2006 06:42amUse the ITextRange.ITextFont.BackColor property
ReplySample of EN_PROTECTED
Posted by Legacy on 04/15/2002 12:00amOriginally 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
ReplyRich Edit
Posted by Legacy on 03/29/2002 12:00amOriginally 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.
ReplyThanks.
How to enable scroll Bar in a DISABLED Rich edit control
Posted by Legacy on 12/31/2001 12:00amOriginally posted by: SUBIN
Please reply if you know "How to enable scroll Bar in a DISABLED Rich edit control" Thanks
ReplyIs there a way to deprotect the protected RANGE?
Posted by Legacy on 08/12/2001 12:00amOriginally 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!
ReplyRich Edit Control
Posted by Legacy on 08/03/2000 12:00amOriginally posted by: Kevin Schubach
Add AfxInitRichEdit() under InitInstance()
Replyand 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 12:00amOriginally 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
ReplyThanks
Kevin