Solved

CRichEdit in a Dialog?  To Replace CEdit.

Posted on 1999-01-02
20
620 Views
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.
0
Comment
Question by:dbetz
  • 11
  • 6
  • 3
20 Comments
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
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


0
 

Author Comment

by:dbetz
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
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
         API
              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
              created.

              You can find SDK style code at this URL (but this shows you how to use WM_SETFONT and WM_GETFONT)
              http://www.wildcomputer.com/h2winnofadialogcontrol.htm

              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,

              e.g.
              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
         Invalidate()
              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
              Windows).


0
 

Author Comment

by:dbetz
Comment Utility
Code...Code in Context....
0
 
LVL 8

Expert Comment

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


0
 

Author Comment

by:dbetz
Comment Utility
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....
0
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
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


0
 

Author Comment

by:dbetz
Comment Utility
The "hassle" of this is that I need almost an entire application's source code to show me how it is used.
0
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
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
0
 

Author Comment

by:dbetz
Comment Utility
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.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:dbetz
Comment Utility
I got VC5, but I do not see that in the palette.  That is what I was talking about.
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
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

0
 

Author Comment

by:dbetz
Comment Utility
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.
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 250 total points
Comment Utility
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
   AfxInitRichEdit();

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

   // find its location
   CRect rect;
   pWnd->GetWindowRect(rect);
   ScreenToClient(rect);

   // destroy the existing window
   pWnd->DestroyWindow();

   // create the new window
   // remember to add your own styles here!
   DWORD dwStyle = WS_VISIBLE | WS_TABSTOP | WS_BORDER | WS_CHILD;
   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

   FINDTEXTEX find;
   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;
      m_richEdit.GetSel(oldSel);

      // select the text we wish to format
      m_richEdit.SetSel(find.chrgText);

      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
      m_richEdit.SetSelectionCharFormat(fmt);

      // restore the previous section
      m_richEdit.SetSel(oldSel);
   }              

And that's that.

B ekiM

0
 

Author Comment

by:dbetz
Comment Utility
Cool deal man, that works.

Thanks for putting up with me.

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

With that CHARFORMAT?
0
 

Author Comment

by:dbetz
Comment Utility
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?
0
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
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


0
 

Author Comment

by:dbetz
Comment Utility
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.

0
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
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
0
 

Author Comment

by:dbetz
Comment Utility
Huh?  I gave an "A".  Must be a problem in the system, they have had a lot of problems lately.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now