Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing Text Color in Rich Edit Control

901 views
Skip to first unread message

defau...@domain.com

unread,
May 28, 1997, 3:00:00 AM5/28/97
to

I'm trying to implement syntax highlighting in a Rich Edit Control and
am running into
a few problems :

The way I'm changing the color of text is as follows :

1. I call SetSel(begin, end)
2. I get the selected text by calling GetSelText()
3. I tokenize the text and set the color of each token using
SetSel(), CHARFORMAT.TextColor and SetCharFormat
4. To finally replace the text with the new color text I call
ReplaceSel()

This works fine, however with all the selection going on in the
background there is a "flicker" when the text changes. Is there a
better way to do the same.

Unfortunately, I cannot get a handle to the text block of the
rich edit control which would have made life easier.

What I need to be able to do is get a range of text from the control
without actually selecting it and then overwriting existing text with
the new colored text so that the transition looks smooth.

Another problem I'm having is that the text gets colorized only if the
font I use is
"Bold". The dwMask settings do not work if I turn the CFE_BOLD option
off.

Anybody have any clues....

Thanks in advance.

Regards

Sameer Singh
Software Engineer
Silicon Graphics Inc.


defau...@domain.com

unread,
May 28, 1997, 3:00:00 AM5/28/97
to

Vance Kessler

unread,
May 29, 1997, 3:00:00 AM5/29/97
to

I don't know why colors only work when you use bold, but to stop the
flickering just use SetRedraw(FALSE) before you start and
SetRedraw(TRUE); InvalidateRect(NULL) after you are done.

Also, when I change the apperance of the text I first place the text and
then call SetSel() and then SetSelectionCharFormat().

Gert Rijs

unread,
May 29, 1997, 3:00:00 AM5/29/97
to

defau...@domain.com wrote in article <338CCB8C...@domain.com>...

> I'm trying to implement syntax highlighting in a Rich Edit Control and
> am running into
> a few problems :
>
> The way I'm changing the color of text is as follows :
>
> 1. I call SetSel(begin, end)
> 2. I get the selected text by calling GetSelText()
> 3. I tokenize the text and set the color of each token using
> SetSel(), CHARFORMAT.TextColor and SetCharFormat
> 4. To finally replace the text with the new color text I call
> ReplaceSel()
>
> This works fine, however with all the selection going on in the
> background there is a "flicker" when the text changes. Is there a
> better way to do the same.
>
> Unfortunately, I cannot get a handle to the text block of the
> rich edit control which would have made life easier.
>
> What I need to be able to do is get a range of text from the control
> without actually selecting it and then overwriting existing text with
> the new colored text so that the transition looks smooth.
>
> Another problem I'm having is that the text gets colorized only if the
> font I use is
> "Bold". The dwMask settings do not work if I turn the CFE_BOLD option
> off.
You can use HideSelection(TRUE, FALSE) before you select the text and
HideSelection(FALSE,FALSE) when your done.
Instead of SetSel/GetSelText you could also use GetLine(...) or StreamOut
wich dont have this flickering behaviour.
perhaps your bold-problem stems from the font you use, have you tried
Courier New??
greetings,
Gert
(reply also posted to comp.os.ms-windows.programmer.tools.mfc)

Sameer Singh

unread,
May 29, 1997, 3:00:00 AM5/29/97
to va...@kali.net

Vance Kessler wrote:

> I don't know why colors only work when you use bold, but to stop the
> flickering just use SetRedraw(FALSE) before you start and
> SetRedraw(TRUE); InvalidateRect(NULL) after you are done.
>
> Also, when I change the apperance of the text I first place the text
> and
> then call SetSel() and then SetSelectionCharFormat().
>

Yep, that worked...

Thanks a lot...

I was also able to fix the bold color problem by setting CFM_BOLD in the
dwMask
variable and setting dwEffects=0...

Thanks once again...

Regards

Sameer


Sameer Singh

unread,
May 30, 1997, 3:00:00 AM5/30/97
to sam...@sgi.com

I'm trying to implement Tooltips in a Rich Edit Control and need to
get the text under the cursor for the same..

In a Edit Control there is a function "CharFromPos(CPoint p)" which
returns the line and character indices of the character nearest to the
point p. There is also a corresponding function "PosFromChar(int
char_index)" which returns the top left corner of the character at
"char_index".

Strangely, the RichEditControl has only the PosFromChar function. I
looked up the documentation and EM_CHARFROMPOS is the message
corresponding to the CEdit
PosFromChar function. I tried Sending this message to both the Edit
Control and the
RichEditControl. It works fine for the Edit Control, however, I get an
"Access violation"
in riched32.dll when I try to send the message to the Rich Edit Control.

Has anybody seen this before? Alternatively, is there another way to
figure out the text
under an arbitrary point in a RichEdit Control?

Thanks..

Sameer Singh

unread,
May 30, 1997, 3:00:00 AM5/30/97
to

This is a multi-part message in MIME format.
--------------89B64996DDEF7A938304C843
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


--------------89B64996DDEF7A938304C843
Content-Type: message/rfc822
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Message-ID: <338E8D56...@sgi.com>
Date: Fri, 30 May 1997 01:18:30 -0700
From: Sameer Singh <sam...@sgi.com>
Organization: Silicon Graphics Inc.
X-Mailer: Mozilla 4.0b4 [en] (WinNT; I)
MIME-Version: 1.0
Newsgroups: comp.os.ms-windows.programmer.tools.mfc,microsoft.public.vc.language,microsoft.public.vc.mfc.docview,comp.os.ms-windows.programmer.win32
To: sam...@sgi.com
Subject: Getting the text under the cursor in a RichEdit Control (Tooltips)
X-Priority: 3 (Normal)
References: <338CCBE7...@domain.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Thanks..

--------------89B64996DDEF7A938304C843--


Sameer Singh

unread,
May 30, 1997, 3:00:00 AM5/30/97
to

This is a multi-part message in MIME format.
--------------B153574F2A63DEA192A0F00E

Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


--------------B153574F2A63DEA192A0F00E

Thanks..

--------------B153574F2A63DEA192A0F00E--


Greg

unread,
May 31, 1997, 3:00:00 AM5/31/97
to

Sameer, I don't know how to help you with this but *PLEASE* post when you
find out; I have been trying to figure this out for a week.

Sameer Singh (sam...@sgi.com) wrote:
: I'm trying to implement Tooltips in a Rich Edit Control and need to

Gert Rijs

unread,
May 31, 1997, 3:00:00 AM5/31/97
to

Sameer Singh <sam...@sgi.com> wrote in article
<338F32BD...@sgi.com>...

>I'm trying to implement Tooltips in a Rich Edit Control and need to
>get the text under the cursor for the same..
>
<SNIP>

>
>Has anybody seen this before? Alternatively, is there another way to
>figure out the text
>under an arbitrary point in a RichEdit Control?
>
>Thanks..

Sameer,

This is the way i do it (m_ctlSource is a CRichEditCtrl):
// get the current selection
long s, e, o, i;
m_ctlSource.GetSel(s, e);
// get the current line
o=e;
char buf[512];
i=m_ctlSource.GetLine(m_ctlSource.LineFromChar(m_ctlSource.LineIndex(-1)),
buf, 512);
buf[i]=0; // zero terminate
// current position on this line
e-=m_ctlSource.LineIndex(-1);
// find the start of this word
while(e>-1)
{
if(isalnum(buf[e])) e--;
else break;
}
e++;
// find the end
s = e;
while(buf[s]!='\0')
{
if(isalnum(buf[s])) s++;
else break;
}
buf[s]=0;
MessageBox(&buf[e], "Current Word");


Sameer Singh

unread,
May 31, 1997, 3:00:00 AM5/31/97
to Gert Rijs

Gert Rijs wrote:

> This is the way i do it (m_ctlSource is a CRichEditCtrl):
> // get the current selection
> long s, e, o, i;
> m_ctlSource.GetSel(s, e);
> // get the current line
> o=e;
> char buf[512];
> i=m_ctlSource.Get

> ine(m_ctlSource.LineFromChar(m_ctlSource.LineIndex(-1)),
> buf, 512);
> buf[i]=0; // zero terminate
> // current position on this line
> e-=m_ctlSource.LineIndex(-1);
> // find the start of this word
> while(e>-1)
> {
> if(isalnum(buf[e])) e--;
> else break;
> }
> e++;
> // find the end
> s = e;
> while(buf[s]!='\0')
> {
> if(isalnum(buf[s])) s++;
> else break;
> }
> buf[s]=0;
> MessageBox(&buf[e], "Current Word");

Hi Gert,

Thanks for your reply. Your method works if the user has clicked at a
particular character. What I need is to be able to compute the text
without the user having to
click. So if you just move the cursor over a word, I should be able to
compute the word
under the cursor.

Have you been able to achieve that functionality yet?

Thanks

Sameer

Sameer Singh

unread,
May 31, 1997, 3:00:00 AM5/31/97
to Greg

Greg wrote:

> Sameer, I don't know how to help you with this but *PLEASE* post when
> you
> find out; I have been trying to figure this out for a week.

Hi Greg,

I think it is a bug in MFC. Maybe you can try out this piece of code and
let me know
what happens?

Let's assume you have your RichEditCtrl in m_ctrl :
------------------------------------------------------------------------------------------------------------------------

// This should change the background color to yellow
m_ctrl.SendMessage(EM_SETBKGNDCOLOR, (WPARAM)0, (LPARAM)RGB(255,255,0));

// This should return the Cpoint corresponding to character position 1
of the Control
CPoint pt;
m_ctrl.SendMessage(EM_POSFROMCHAR, (WPARAM)&pt, (LPARAM)1);

TRACE("Got point => %d, %d\n",pt.x,pt.y);

// This should return 1 since we pass pt.x & pt.y as the LPARAM to
SendMessage
long charpos;
charpos = (long) m_ctrl.SendMessage(EM_CHARFROMPOS,
(WPARAM)0, (LPARAM)MAKELPARAM(pt.x,pt.y));

TRACE("Got charpos = %d, corresponding to point %d,
%d\n",charpos,pt.x,pt.y);
---------------------------------------------------------------------------------------------------------------------------

When I try this it give me a Access Violation error in riched32.dll. If
you comment out the last
send message call. The others should work fine....

Another reason why I believe this is a bug, is that the exact same piece
of code works
fine with a Edit Control...

There might be one other way to compute the point, i.e. by figuring out
the line on which
the point lies, then figuring out the first character of the line and
then stepping through each
character until you find an overlap.

So basically, GetClientRect() would give me the upper and lower bounds
of the Control area rectangle, then I would get the current font and use
the height+External Leading to figure out the # of lines in this
control. There is another call GetFirstVisibleLine() which would give me
the first line. I can then step through the number of lines and figure
out if pt.y is between cur_line.y1 and cur_line.y2. If so then the point
lies on the current line.

I would use the same method in the x direction (maybe compute xbegin and
xend for
each word) and calculate whether pt.x is between xbegin and xend.

I haven't implemented it as yet, but should get to it next week. In the
meantime let me
know if you have some success...

Regards

Sameer


0 new messages