Click here to Skip to main content
15,867,834 members
Articles / Desktop Programming / MFC
Article

CRichEditCtrl revised

Rate me:
Please Sign up or sign in to vote.
4.62/5 (30 votes)
2 Apr 2003CPOL4 min read 257.9K   11.1K   56   59
A derived CRichEditCtrl class which focuses on streaming and on the visual aspects of formatting. Also includes the use of the link-formatting.

Sample Image - CRichEditCtrlGS.jpg

CRichEditCtrl revised

This heir of the MS-RichEditCtrl class has its main emphasis on support for visual appearance and streaming functionality, like CStrings, CbyteArrays, resources and files. Although some features like font or colour dialogging were tempting, I did not put it into the class, these are provided in the sample application. This is because I use this class in a static library, and there the access to the GUI is restricted.

Never mind, I think there is a lot that will help you, when dealing with CRichEditCtrl.

The source code comes with a help file in CHM-format. It contains the documentation of the CRichEditGS class. You need Internet Explorer V4.0 or higher to get access to that file.
Thanks to the people of FAR form The Helpware Group for their fine programm.

To use this class follow the usual wizardry. That is:
 • create a CRichEditCtrl with ResourceWizard
 • create a member of that control (not its content) with ClassWizard
 • copy the files "RichEditCtrlGS.cpp/h" into your project directory with CopyWizard.
 • replace all occurences of CRichEditCtrl by CRichEditCtrlGS with ReplaceWizard
 • include the headerfile "RichEditCtrlGS.h" either in stdafx.h or your dialogs/form/view header file. Sorry no IncludeWizard, so use the keyboard.

First of all, do not worry how to bind to RichEdit version 2 or version 3. This is done by some magic in the operating system.
The sample program was developed with Visual C++ Service Pack 5 and is running on WIN98SE and W2K equally well. Although the visual appearance of the links in WIN98SE is not that what I expected.
So if you have updated your operating system and your compiler in the last few years, chances are good that you may use the features of Richedit version 2.0.

You can use a CRichEditCtrl as a static control, when you deactivate this window. There you have all the formatting features of a rich text by just streaming in a resource.
How?. Well write a rich formatted text and save it as file into you res-directory (use the example application for that purpose).
After that open the resource editor. Tell it to import a new ressource. Select your newly created file and when the editor asks for the resource type give it the name RTF.
In your application code you should set the background colour of that CRichEditCtrl to COLOR_BTNFACE. Then call the stream in function for the resources of your derived CRichEditCtrl class (or SetRTF if you use mine).
Remember that a deactivated window will not get or send any message! So you will never get notified when the user hovers over a link.

While there is support from the operating system, there is no support from class wizard. So if you want support for the EN_LINK message notification for instance, tell class wizard that you need assistance for let's say EN_PROTECTED. After letting the class wizard do the job, load your source files and change every occurence of protected to link. Keep in mind to preserve capitals in this case. Then do the coding.

See the sample application how it works.
See the source code what must be done to get it working.

FGAs

aka Frequently Given Answers

General Purpose
 • I don't know.
 • Yes.
 • No.
 • Send me an e-mail with further details.
 • Have a closer look at the source code of the example provided.

Specific
 • The link bit can only be set by the application. It will not be saved, although it will be copied. If you need it for internet purposes only, consider to send the EM_AUTOURLDETECT message to your control. Remember, in this case you will lose all link bits set by your program. You still have to do the hard programming part of the EN_LINK notification message.
 • Actually it is working correctly. The queer behaviour comes from the fact, that the format styles of characters are flags, while the paragraph alignment is an enumeration. So there is no way to determine if a 3( = centered) results from 3 | 0 or 1(=left) | 2(=right) or 2 | 1 or any or-combination of 0,1,2,3. That's why the alignment of paragraphs gets flagged wrong when your selection stretches more than one paragraph. Morale: never or enumerated values.
 • This comes in quite handy if you use a database, where long binary datas are bound to a CByteArray variable.
 • According to Microsoft the justify alignment will not be displayed, it will be stored however. That is true. Also according to Microsoft the text will be displayed as being left aligned. I saw it once being displayed centered.

So if you have a question, have a look first at the FGA section. The answer might be already there.

Also of good help is the wordpad source code provided with VC++ and MSDN.

G. Steudtel

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalsetting the Text Height fails Pin
Member 31442507-Jun-11 19:40
Member 31442507-Jun-11 19:40 
GeneralScrollbars are not displayed Pin
MikeTheDwarf26-Sep-08 9:34
MikeTheDwarf26-Sep-08 9:34 
QuestionCan I use your source code? Pin
Member 255468227-Jun-08 7:36
Member 255468227-Jun-08 7:36 
AnswerRe: Can I use your source code? Pin
G. Steudtel30-Jun-08 10:18
G. Steudtel30-Jun-08 10:18 
GeneralRe: Can I use your source code? Pin
Member 25546823-Jul-08 6:43
Member 25546823-Jul-08 6:43 
QuestionHow to prevent image from resizing Pin
kanam77719-Mar-08 3:55
kanam77719-Mar-08 3:55 
GeneralVery useful ! Pin
Marc Dispa29-Sep-07 6:14
Marc Dispa29-Sep-07 6:14 
GeneralRe: Very useful ! Pin
G. Steudtel30-Jun-08 10:23
G. Steudtel30-Jun-08 10:23 
Questionhow to make the same text all have link ? Pin
meadow-jn27-Sep-07 22:10
meadow-jn27-Sep-07 22:10 
QuestionHow to set focus on links in richeditctrl through tab Pin
Shaffiq27-Sep-07 15:56
Shaffiq27-Sep-07 15:56 
QuestionReplace and Copy Wizards???? Pin
rypo27-Jan-07 20:44
rypo27-Jan-07 20:44 
Hi everyone. Can anybody explain me, what are those copywizard and replacewizard, that the author was talking about. Where can I find them? Thanks in advance for help.

Cheers, Rypo.
AnswerRe: Replace and Copy Wizards???? Pin
S Douglas27-Jan-07 21:42
professionalS Douglas27-Jan-07 21:42 
AnswerRe: Replace and Copy Wizards???? Pin
G. Steudtel30-Jun-08 10:26
G. Steudtel30-Jun-08 10:26 
GeneralATL activex or other rich text control Pin
cordoval22-Jan-07 17:19
cordoval22-Jan-07 17:19 
Generalurls from rich edit control Pin
dkdsnaidu28-Apr-06 1:32
dkdsnaidu28-Apr-06 1:32 
GeneralWell done! Pin
FamousDetective25-Mar-06 15:55
FamousDetective25-Mar-06 15:55 
GeneralCRichEditCtrl and displaying margins. Pin
delphidab11-Feb-06 10:13
delphidab11-Feb-06 10:13 
GeneralSearch and chage color for text in the control Pin
yuvald5-Oct-05 6:49
yuvald5-Oct-05 6:49 
Questionhow to unselect text Pin
vjedlicka1-Feb-05 10:49
vjedlicka1-Feb-05 10:49 
AnswerRe: how to unselect text Pin
Sriram Chitturi3-Jun-07 5:42
Sriram Chitturi3-Jun-07 5:42 
AnswerRe: how to unselect text Pin
lzdeak29-Nov-07 1:52
lzdeak29-Nov-07 1:52 
GeneralRe: how to unselect text Pin
simbakid2-Aug-23 23:09
simbakid2-Aug-23 23:09 
GeneralColumns in RicheditCtrl Pin
lvidaguren2-Nov-04 23:48
lvidaguren2-Nov-04 23:48 
QuestionHow to find width of text in RichEdit? Pin
Anonymous21-Jul-04 1:18
Anonymous21-Jul-04 1:18 
AnswerRe: How to find width of text in RichEdit? Pin
Filomela1-Oct-04 22:45
Filomela1-Oct-04 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.