CRichEdit in a Dialog? To Replace CEdit.

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.
Who is Participating?
mikeblasConnect With a Mentor Commented:
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

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

dbetzAuthor Commented:
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.
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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

dbetzAuthor Commented:
Code...Code in Context....
Well I was pasting that in, Mike B submitted  an answer which you perhaps you don't need my CRichEditCtrl comments ?

dbetzAuthor Commented:
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....
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

dbetzAuthor Commented:
The "hassle" of this is that I need almost an entire application's source code to show me how it is used.
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
dbetzAuthor Commented:
All "Code in context" means is I need to see it used.  I got tons of from from does not work), but nothing is actually is in context.
dbetzAuthor Commented:
I got VC5, but I do not see that in the palette.  That is what I was talking about.
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

dbetzAuthor Commented:
I want colors and stuff.....fonts and stuff too....

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

you get points for WORKING RICHEDIT IN DIALOG project.
dbetzAuthor Commented:
Cool deal man, that works.

Thanks for putting up with me.

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

dbetzAuthor Commented:
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?
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

dbetzAuthor Commented:
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.

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
dbetzAuthor Commented:
Huh?  I gave an "A".  Must be a problem in the system, they have had a lot of problems lately.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.