Archived

This topic is now archived and is closed to further replies.

Changing the color of a win32 rich edit control

This topic is 5300 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My newbie attemps don''t work. I try this inside function : HDC hdc = GetDC(handle); SetTextColor (hdc, RGB(12,221,66)); UpdateWindow(handle); ReleaseDC(handle,hdc); Help.

Share this post


Link to post
Share on other sites
What you did was changing the color for GDI-text output, that means you just set the color that will be used when you call TextOut.

changing properties of a control usually involves telling the control what to do by sending it a windows-message.
in your case you''ll have to do the following:
first make a CHARFORMAT variable.
then call
SendMessage ( RichTextWindowHandle, EM_GETCHARFORMAT, (WPARAM)(BOOL)0, (LPARAM)(CHARFORMAT FAR *)(&YourCHARFORMATvar) );
to fill the structure with the current values (font etc.)

then set it''s crTextColor member to the color you want, it''s dwEffects to 0 and dwMask to CFM_COLOR
finally call
SendMessage ( RichTextWindowHandle, EM_SETCHARFORMAT, (WPARAM)(UINT)SCF_ALL, (LPARAM)(CHARFORMAT FAR *)(&YourCHARFORMATvar) );

to set the new charformat, and with it the new color

but note that this will change the color of all text in the control, if you just want to change the color of parts of it, you first have to select that and then call the above window-messages with SCF_SELECTION instead of SCF_ALL



Share this post


Link to post
Share on other sites
quote:
Original post by Burning_Ice
first make a CHARFORMAT variable.
then call
SendMessage ( RichTextWindowHandle, EM_GETCHARFORMAT, (WPARAM)(BOOL)0, (LPARAM)(CHARFORMAT FAR *)(&YourCHARFORMATvar) );
to fill the structure with the current values (font etc.)

then set it''s crTextColor member to the color you want, it''s dwEffects to 0 and dwMask to CFM_COLOR
finally call
SendMessage ( RichTextWindowHandle, EM_SETCHARFORMAT, (WPARAM)(UINT)SCF_ALL, (LPARAM)(CHARFORMAT FAR *)(&YourCHARFORMATvar) );

And that is why no sane persons write GUI stuff in raw Win32...



AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
And that is why no sane persons write GUI stuff in raw Win32...





but actually after doing some win32 coding it isn''t that bad anymore, since it really all isn''t that different...usually just finding the right windows message and looking up it''s parameters on MSDN/the help files

and you only have to write it once anyway, since you will either group it in reusable functions/classes or copy+paste when you need the stuff again

and it''s still much better and easier then MFC




Share this post


Link to post
Share on other sites
I''ve tried this, and it had no effect:

CHARFORMAT ch;
SendMessage ( hwndEdit,EM_GETCHARFORMAT,(WPARAM)(BOOL)0,(LPARAM)(CHARFORMAT FAR *)&ch );
ch.crTextColor = RGB(255,0,0);
ch.dwEffects = 0;
ch.dwMask = CFM_COLOR;
SendMessage (hwndEdit, EM_REPLACESEL, true, (LPARAM)"blabla");
SendMessage ( hwndEdit, EM_SETCHARFORMAT, (WPARAM)(UINT)SCF_ALL, (LPARAM)(CHARFORMAT FAR *)&ch );

First I sent "blabla" to the control, and then I attempted to change the color to red, but it stayed black. Then I typed in the control and it was black too. What am I doing wrong?

Share this post


Link to post
Share on other sites
you forgot
ch.cbSize = sizeof (CHARFORMAT);

if you add this before the first SendMessage it should work

Edit: sorry, was my fault, i just noticed that i forgot to tell in my first post that this is needed.
but it's something to remember: almost every WINAPI struct has some kind of size member that needs to be filled before the struct is used in any function




[edited by - burning_ice on June 7, 2003 7:33:01 AM]

Share this post


Link to post
Share on other sites