dbetz
asked on
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.
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.
ASKER
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.
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).
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).
ASKER
Code...Code in Context....
Well I was pasting that in, Mike B submitted an answer which you rejected...so perhaps you don't need my CRichEditCtrl comments ?
ASKER
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
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
ASKER
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
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
ASKER
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.
ASKER
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
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
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
Thanks for putting up with me.
Listen, what is the simplest way to add a font here?
With that CHARFORMAT?
ASKER
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
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
ASKER
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.
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
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
ASKER
Huh? I gave an "A". Must be a problem in the system, they have had a lot of problems lately.
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