CRichEdit in a Dialog?  To Replace CEdit.

Posted on 1999-01-02
Medium Priority
Last Modified: 2013-11-20
I have a CEdit that shows the results of what the rest of the application does.  But then I have another function of my application that goes back though and "parses"(not really) the text and I want it to add color and stuff.  And I want to use one special font.  

So I think I need CRichEdit.  I have NO IDEA HOW TO DO IT!!  This MFC book I got SUX!!! (Peter Norton MFC thing).

Can someone please help me make my CEdit into a CRichEdit.  So I can have the application also add color and a font(one uniform font).

I am giving 200 points, just because this is a hassle.
Question by:dbetz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 3
LVL 11

Expert Comment

ID: 1327125
1) Delete the edit control in your dialog.

2) Draw a rich edit control in its place.

3) Make sure you call AfxInitRichEdit() before your dialog is displayed.

That's it: I don't see what the hassle is. Maybe you meant to ask a more specific question.

B ekiM


Author Comment

ID: 1327126
Yeah, actually it is more specific.  I want to know how to do fonts and stuff.  And that stuff you told me means nothing.  I need code.

Expert Comment

ID: 1327127
For a CEdit you can change the font and color using the following mechanisms (this only gives you 1 font, and 1 background and 1 foreground color).  This may be enough for you app.  If not read my comment below.  Please note these comments were originally written for buttons/statics but also work with edits.

1. Use the WM_SETFONT message to change the font of control (e.g. button or static).  Basically you need an HFONT (you can get one from a CFont or use the
              functions - GetStockObject _or_ ::CreateFont _or_ ::CreateFontIndirect_) to get one.

              Then send it to the control window

              If you are doing it from the control itself, you can do this at any point after the control has been created (or attached to an existing window - remember
         OnCreate is not
              called for control windows which are on dialogs, the Windows dialog manager creates the Windows, then the control windows are attached)

              Each CStatic, CButton (and all other classes derived from CWnd) include an HWND variable called m_hWnd.  This is the window to set the font change to.

              Before your control is destroyed (e.g. OnDestroy), set the font back (you can get the previous font by using the WM_GETFONT message), and destroy any
         fonts you have

              You can find SDK style code at this URL (but this shows you how to use WM_SETFONT and WM_GETFONT)

              It is not necessary to derive a control class.  You can use these messages even for the standard MFC classes.

              2. To change color of a control, handle the WM_CTLCOLOR message

              If you are using a derived class of CButton or CStatic etc.. use the version with = inside.

              Set the foreground color by doing

              pDC->SetTextColor( RGB(some_value_) ) ;

              Set the background color by doing

              pDC->SetBkColor( RGB(some_value) ) ;
              and return an HBRUSH handle to the background brush,

              i. add to class an HBRUSH member
              ii. Create a solid brush color in your constructor using CreateSolidBrush
              iii. In destructor use DeleteObject on the brush.

              If you want to change the color on the fly, recreate the brush (delete any old one first, store the colors in class member variables, and then force a repaint using
              followed by UpdateWindow() ).

              Again it is not necessary to derive from CStatic/CButton etc. to do this, but it is often easier.  The other way is to allow the containing window (e.g. a dialog
         box with the
              controls to handle the WM_CTLCOLOR message),  when you do it this way, you should remember the dialog may have many controls each requesting what
         color they
              should be - however this version of OnCtlColor gives you extra parameters telling you which window is about to be painted (so you can supply different colors
         for different

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.


Author Comment

ID: 1327128
Code...Code in Context....

Expert Comment

ID: 1327129
Well I was pasting that in, Mike B submitted  an answer which you rejected...so perhaps you don't need my CRichEditCtrl comments ?


Author Comment

ID: 1327130
How do I "draw" a Rich Edit?  The toolbar only has a text area....then I used class wizard to make it a member with CEdit.....There is no CRichEdit....
LVL 11

Expert Comment

ID: 1327131
Answers2000 is all wet: since you're using the rich edit control, you don't want to handle WM_CTLCOLOR.

To set the font or color of text in a rich edit control, you need to select the text you want to change with the SetSel() member.  Then, you'll call SetSelectionCharFormat() to change the format of that text.  SetSelectionCharFormat() takes a CHARFORMAT structure, and that structure includes obvious members that will format the text. CHARFORMAT::crTextColor, for example, is a COLORREF that sets the text color, while CHARFORMAT::szFaceName sets the font's face name.

If you want "code in context", you'll need to provide the context! We can only answer the question you actually ask.

B ekiM


Author Comment

ID: 1327132
The "hassle" of this is that I need almost an entire application's source code to show me how it is used.
LVL 11

Expert Comment

ID: 1327133
Visual C++ 5.0 and newer have a rich edit control on the controls palette in the dialog editor. If you're using an older version, then you need to create the rich edit control on the fly yourself.

If you're using an older version, actually tell me that (you don't mention anything in your question) and I can provide code that will create the rich edit control for you.

B ekiM

Author Comment

ID: 1327134
All "Code in context" means is I need to see it used.  I got tons of from from codeguru.com(that does not work), but nothing is actually is in context.

Author Comment

ID: 1327135
I got VC5, but I do not see that in the palette.  That is what I was talking about.

Expert Comment

ID: 1327136
Mike - >> Answers2000 is all wet:
My advice was to use a standard edit rather than a rich edit (perhaps I wasn't clear enough about this in my original answer), because as I understand the app only needs one font and one color foreground and background.  Therefore the standard edit control is adequate, and there's no need to use a rich edit (unless the app requires additional rich edit functionality, or just for the heck of it.

dbetz - "code in context"
no time to do that at the moment.  Also I want to be clear whether you really need/want to use the rich edit, or a standard edit is adequate, before adding any more text


Author Comment

ID: 1327137
I want colors and stuff.....fonts and stuff too....

Will try to do it in API here in a few days....it may be impossible with MFC... I do not know....

you get points for WORKING RICHEDIT IN DIALOG project.
LVL 11

Accepted Solution

mikeblas earned 1000 total points
ID: 1327138
When I start VC++ 5.0, I see the button for the RichEdit control in the toolbar. The button shows the letters "ab" on its face. The letters are itallic and underlined.

Does that button really not show on your installation?

In the meantime:

1) Add a CRichEdit instance to your dialog's member data. I'll refer to it in future steps as "m_richEdit".

2) Add a regular edit control to your dialog box. I'll refer to it as IDC_EDIT1.

3) Then, you can add this code to your OnInitDialog() function to convert the regular edit control to a rich edit control at runtime:

   // make sure the rich edit control is initialized

   // get a pointer to the existing plain edit window
   CWnd* pWnd = GetDlgItem(IDC_EDIT1);
   ASSERT(pWnd != NULL);

   // find its location
   CRect rect;

   // destroy the existing window

   // create the new window
   // remember to add your own styles here!
   m_richEdit.Create(dwStyle, rect, this, IDC_EDIT1);

4) Now that you have a reference to the control, you can add some text to it:

   // set some text into the control
   m_richEdit.SetWindowText("we will change this text to have a different font");

5) Character formatting in the rich edit control works by changing the formatting attributed to a particular selection, or to the default. Changing the default sets the formatting for new text the user types. To change the formatting for text you've programatically added, you'll need to select the text and reformat it.

Add some code to find the word "this". Then, reformat it to a different color and make it bold:

   // find the first instance of the string "this" within the control

   find.chrg.cpMin = 0;
   find.chrg.cpMax = -1;
   find.lpstrText = "this";
   long lFound = m_richEdit.FindText(0, &find);

   // if it was found, change its formatting
   if (lFound != -1)
      // get the current selection
      CHARRANGE oldSel;

      // select the text we wish to format

      CHARFORMAT fmt;
      fmt.cbSize = sizeof(fmt);
      // bright red
      fmt.crTextColor = RGB(255, 0, 0);
      fmt.dwEffects = CFE_BOLD;
      fmt.dwMask = CFM_COLOR | CFM_BOLD;

      // set the format

      // restore the previous section

And that's that.

B ekiM


Author Comment

ID: 1327139
Cool deal man, that works.

Thanks for putting up with me.

Listen, what is the simplest way to add a font here?


Author Comment

ID: 1327140
Hey, that messed up my \r\n things.  Now all my text is on one line with this Rich Edit.  How do I get the LF/CR in there?
LVL 11

Expert Comment

ID: 1327141
Like the comment in my sample code says, you need to watch out which styles you use when you create the rich edit control. Since the old control is being deleted, the styles you used in the dialog editor are meaningless. Sounds like you're missing ES_MULTILINE; since you've ignored the comments, you're probably missing a lot of other things.

You can read the documentation for CHARFORMAT; it describes all the flags and the members necessary to change the font face.

You never answered my question about not seeing the rich edit control in the tool palette, by the way.

B ekiM


Author Comment

ID: 1327142
I seen that CHARFORMAT thing on MSDN, I finally figured it out.

The small "ab" that I got says "Edit Box"  that is what I refer to as the Edit Control.  There is not a separate Edit and Richedit.  

You code worked with the small "ab" that I had originally.

Oh, I got that font to work too.  

I had too do this:

fmt.dwMask = CFM_COLOR | CFM_FACE;
lstrcpy(fmt.szFaceName, "Courier New");

I do have one other small question, this richedit allows me to drag text with in it.  How can I disable that?  I do not want to accidentally screw up my text with the mouse.  This is probobally in the Mask too?

Thanks again.

LVL 11

Expert Comment

ID: 1327143
You're sure asking a lot of follow-up questions for only giving a "B" grade.

You can set ES_READONLY if you don't want changes to happen in the control.

You can mark individual ranges of text in the control as protected using the CFM_PROTECTED mask and the CFE_PROTECTED effect in CHARFORMAT. If the user tries to change the protected text, you'll get an EN_PROTECTED notification.

There's no way to disable drag-and-drop of text in the control. You only have control over the OLE drag and drop for the control.

B ekiM

Author Comment

ID: 1327144
Huh?  I gave an "A".  Must be a problem in the system, they have had a lot of problems lately.

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question