Discussion:
Why can't I set font and color as default in rich edit control?
(too old to reply)
j***@gmail.com
2006-04-27 06:09:54 UTC
Permalink
Hi everybody:
I'm working under Windows XP + SP2 + VC6, C language, using WIN32
function (NOT MFC).
I load library by: LoadLibraryEx("RICHED32.DLL",NULL,0);
I use window class: RICHEDIT_CLASS
Now I'm wring a function Insert(text, font, text_color) to insert some
text to this common control with given font and text_color, but I do
not want the default color and font be effected. That is, when I type
sth in rich edit control, it use the default font and color:
void Insert (text, font, text_color)
{
CHARFORMAT dft_cft;
SendMessage(hwnd,EM_GETCHARFORMAT ,
SCF_SELECTION,(LPARAM)&dft_cft); // save default font and color
.......................//here we insert the text
//then we reset the rich edit control's every insertion point's font
and color
{
int new_sel_range;
int i;
int n;
SendMessage(hwnd,EM_SETSEL,0,-1); //select all
new_sel_range = SendMessage(hwnd,EM_GETSEL,0,0);
n = HIWORD(new_sel_range)-2; //n is the number of chars in
rich edit now
for(i=0;i<=n;++i){
SendMessage(hwnd,EM_SETSEL,i,i); //select at position i
SendMessage(hwnd,EM_SETCHARFORMAT ,
SCF_SELECTION,(LPARAM)&dft_cft);//set insertion point i to use default
font and color
}
}
}
But I'm sad to find that it does not work. After Insert is called, if
I type sth in rich edit control, the char will follow the font and
color of its previous neighbourhood,that is, the char just before it!

So what's wrong? Any advice is welcome, thx.
Allan M. Bruce
2006-04-27 09:03:03 UTC
Permalink
Post by j***@gmail.com
I'm working under Windows XP + SP2 + VC6, C language, using WIN32
function (NOT MFC).
I load library by: LoadLibraryEx("RICHED32.DLL",NULL,0);
I use window class: RICHEDIT_CLASS
Now I'm wring a function Insert(text, font, text_color) to insert some
text to this common control with given font and text_color, but I do
not want the default color and font be effected. That is, when I type
void Insert (text, font, text_color)
{
CHARFORMAT dft_cft;
SendMessage(hwnd,EM_GETCHARFORMAT ,
SCF_SELECTION,(LPARAM)&dft_cft); // save default font and color
.......................//here we insert the text
//then we reset the rich edit control's every insertion point's font
and color
{
int new_sel_range;
int i;
int n;
SendMessage(hwnd,EM_SETSEL,0,-1); //select all
new_sel_range = SendMessage(hwnd,EM_GETSEL,0,0);
n = HIWORD(new_sel_range)-2; //n is the number of chars in
rich edit now
for(i=0;i<=n;++i){
SendMessage(hwnd,EM_SETSEL,i,i); //select at position i
SendMessage(hwnd,EM_SETCHARFORMAT ,
SCF_SELECTION,(LPARAM)&dft_cft);//set insertion point i to use default
font and color
}
}
}
But I'm sad to find that it does not work. After Insert is called, if
I type sth in rich edit control, the char will follow the font and
color of its previous neighbourhood,that is, the char just before it!
So what's wrong? Any advice is welcome, thx.
I am not 100% on this but it could be similar to Word in that any additional
chars are displayed using the font/colour of the last char. Therefore if
you change your font/colour back to normal and then add a space with it,
then try typing.
Allan
Kellie Fitton
2006-04-27 16:04:38 UTC
Permalink
Hi,

You can use the following API to set the background color for
a rich edit control:

EM_SETBKGNDCOLOR

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditmessages/em_setbkgndcolor.asp

Hope these suggestions helps,

Kellie.
Jason
2006-04-28 07:42:50 UTC
Permalink
Thank you. But I'm afraid it's not the same as what I have met. It is about
the whole control's background.
Post by Kellie Fitton
Hi,
You can use the following API to set the background color for
EM_SETBKGNDCOLOR
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditmessages/em_setbkgndcolor.asp
Hope these suggestions helps,
Kellie.
Jason
2006-04-28 07:36:48 UTC
Permalink
Thank you. I tried. And I found a better way in Word: suppose the caret is
now after a char which has the special font/color(such as: its font is bold)
but you want what you will type is NOT bold, you can just click the "B"
button in the toolbar! That is, Word allows you to set up font/color for a
certain insertion point! Form spy++, I saw Word's edit window's class is
_WwG. Is there any secret API? Win32 API seem powerless.
Post by Allan M. Bruce
Post by j***@gmail.com
I'm working under Windows XP + SP2 + VC6, C language, using WIN32
function (NOT MFC).
I load library by: LoadLibraryEx("RICHED32.DLL",NULL,0);
I use window class: RICHEDIT_CLASS
Now I'm wring a function Insert(text, font, text_color) to insert some
text to this common control with given font and text_color, but I do
not want the default color and font be effected. That is, when I type
void Insert (text, font, text_color)
{
CHARFORMAT dft_cft;
SendMessage(hwnd,EM_GETCHARFORMAT ,
SCF_SELECTION,(LPARAM)&dft_cft); // save default font and color
.......................//here we insert the text
//then we reset the rich edit control's every insertion point's font
and color
{
int new_sel_range;
int i;
int n;
SendMessage(hwnd,EM_SETSEL,0,-1); //select all
new_sel_range = SendMessage(hwnd,EM_GETSEL,0,0);
n = HIWORD(new_sel_range)-2; //n is the number of chars in
rich edit now
for(i=0;i<=n;++i){
SendMessage(hwnd,EM_SETSEL,i,i); //select at position i
SendMessage(hwnd,EM_SETCHARFORMAT ,
SCF_SELECTION,(LPARAM)&dft_cft);//set insertion point i to use default
font and color
}
}
}
But I'm sad to find that it does not work. After Insert is called, if
I type sth in rich edit control, the char will follow the font and
color of its previous neighbourhood,that is, the char just before it!
So what's wrong? Any advice is welcome, thx.
I am not 100% on this but it could be similar to Word in that any
additional chars are displayed using the font/colour of the last char.
Therefore if you change your font/colour back to normal and then add a
space with it, then try typing.
Allan
Jason
2006-04-28 07:41:48 UTC
Permalink
PS: I'm ***@gmail.com. This time I posted in outlook express because
it works now:)
Post by Jason
Thank you. I tried. And I found a better way in Word: suppose the caret is
now after a char which has the special font/color(such as: its font is
bold) but you want what you will type is NOT bold, you can just click the
"B" button in the toolbar! That is, Word allows you to set up font/color
for a certain insertion point! Form spy++, I saw Word's edit window's
class is _WwG. Is there any secret API? Win32 API seem powerless.
Post by Allan M. Bruce
Post by j***@gmail.com
I'm working under Windows XP + SP2 + VC6, C language, using WIN32
function (NOT MFC).
I load library by: LoadLibraryEx("RICHED32.DLL",NULL,0);
I use window class: RICHEDIT_CLASS
Now I'm wring a function Insert(text, font, text_color) to insert some
text to this common control with given font and text_color, but I do
not want the default color and font be effected. That is, when I type
void Insert (text, font, text_color)
{
CHARFORMAT dft_cft;
SendMessage(hwnd,EM_GETCHARFORMAT ,
SCF_SELECTION,(LPARAM)&dft_cft); // save default font and color
.......................//here we insert the text
//then we reset the rich edit control's every insertion point's font
and color
{
int new_sel_range;
int i;
int n;
SendMessage(hwnd,EM_SETSEL,0,-1); //select all
new_sel_range = SendMessage(hwnd,EM_GETSEL,0,0);
n = HIWORD(new_sel_range)-2; //n is the number of chars in
rich edit now
for(i=0;i<=n;++i){
SendMessage(hwnd,EM_SETSEL,i,i); //select at position i
SendMessage(hwnd,EM_SETCHARFORMAT ,
SCF_SELECTION,(LPARAM)&dft_cft);//set insertion point i to use default
font and color
}
}
}
But I'm sad to find that it does not work. After Insert is called, if
I type sth in rich edit control, the char will follow the font and
color of its previous neighbourhood,that is, the char just before it!
So what's wrong? Any advice is welcome, thx.
I am not 100% on this but it could be similar to Word in that any
additional chars are displayed using the font/colour of the last char.
Therefore if you change your font/colour back to normal and then add a
space with it, then try typing.
Allan
Jason
2006-04-28 08:02:40 UTC
Permalink
I've searched and found a post whose problem is similar to me. I've tried
to email ***@online.no but failed.


by Alf Salte, 1999-06-09 12:00 AM

Anyone have any experience with RichEdit controls?

Hello guys,

I have tried to use RichEdit controls in my programs but I have had little
luck. The
problem appear to be as follows:


Say, I want display "Hello world" in the box with the word 'world' in blue
letters.


Ok, I start out to write 'Hello ' and then switch color to blue using
EM_SETCHARFORMAT
I then write 'world' and then "\r\n".


I then set the color back to black for the next line.


The problem is that the selection at the end is the empty selection, nothing
is selected
yet (If you did a EM_EXGETSEL you would get that cpMin and cpMax are equal).


Appearantly in this case, the rich edit control seem to ignore your request
to change
color. The result is that when text is later inserted into the control in
next line, the color
is blue and not black!


Ok, you try to make the range non-empty. You change the color to black
before the "\r\n"
is written out. The problem is that this doesn't appear to have any effect
since return and
new line are not visible characters (they have no glyphs) so there's no
color to be changed
because of them and so the request appear to be ignored.


What am I doing wrong?


I am sorry for this explanation which might seem vague to people but I'm
using Borland
C++ builder and I assume that people here understand the problem better in
terms of
Win32 call. I know it is not C++ builder's problem - you get the same
results if you do the
bare Win32 calls. However, C++ builder's terms the problem can be examined
by
the following piece of code:


RE -> SelStart = 0;
RE -> SelLength = 0;
RE -> SetSelTextBuf( "Hello " );
RE -> SelAttributes -> Color = clBlue;
RE -> SetSelTextBuf( "world" );
RE -> SelAttributes -> Color = clBlack;
RE -> SetSelTextBuf( "\r\n" );
RE -> SetSelTextBuf( "This is in wrong color!\r\n" );


The last line is displayed in blue and not in black as should be expected.


I assume the Win32 equivalent would be something like this:


CHARRANGE cr;
cr.cpMin = 0;
cr.cpMax = 0;
SendMessage( HWND_RE, EM_EXSETSEL, WPARAM(0), LPARAM(&cr));
SendMessage(HWND_RE,EM_REPLACESEL, WPARAM(FALSE), LPARAM("Hello "));
CHARFORMAT cf;
memset(&cf,0,sizeof(cf));
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_COLOR;
cf.crTextColor = RGB(0,0,255); // blue color.
SendMessage(HWND_RE,EM_SETCHARFORMAT, WPARAM(SCF_SELECTION),
LPARAM(& cf));
SendMessage(HWND_RE,EM_REPLACESEL, WPARAM(FALSE), LPARAM("world"));
cf.crTextColor = RGB(0,0,0); // black color
SendMessage(HWND_RE,EM_SETCHARFORMAT, WPARAM(SCF_SELECTION),
LPARAM(& cf));
SendMessage(HWND_RE,EM_REPLACESEL, WPARAM(FALSE), LPARAM("\r\n"));
SendMessage(HWND_RE,EM_REPLACESEL, WPARAM(FALSE),
LPARAM("This is in wrong color!\r\n"));


At least it comes in the wrong color when I try it....


What am I doing wrong and hwo can I go about to get rid of this annoying
'feature'?


Any help is appreciated, if you respond my sending me an E-mail it is even
more
appreciated as I do not often visit this group.


Alf
Post by j***@gmail.com
I'm working under Windows XP + SP2 + VC6, C language, using WIN32
function (NOT MFC).
I load library by: LoadLibraryEx("RICHED32.DLL",NULL,0);
I use window class: RICHEDIT_CLASS
Now I'm wring a function Insert(text, font, text_color) to insert some
text to this common control with given font and text_color, but I do
not want the default color and font be effected. That is, when I type
void Insert (text, font, text_color)
{
CHARFORMAT dft_cft;
SendMessage(hwnd,EM_GETCHARFORMAT ,
SCF_SELECTION,(LPARAM)&dft_cft); // save default font and color
.......................//here we insert the text
//then we reset the rich edit control's every insertion point's font
and color
{
int new_sel_range;
int i;
int n;
SendMessage(hwnd,EM_SETSEL,0,-1); //select all
new_sel_range = SendMessage(hwnd,EM_GETSEL,0,0);
n = HIWORD(new_sel_range)-2; //n is the number of chars in
rich edit now
for(i=0;i<=n;++i){
SendMessage(hwnd,EM_SETSEL,i,i); //select at position i
SendMessage(hwnd,EM_SETCHARFORMAT ,
SCF_SELECTION,(LPARAM)&dft_cft);//set insertion point i to use default
font and color
}
}
}
But I'm sad to find that it does not work. After Insert is called, if
I type sth in rich edit control, the char will follow the font and
color of its previous neighbourhood,that is, the char just before it!
So what's wrong? Any advice is welcome, thx.
Loading...