how can I change color of part of the text? I'm making chat application and I wanna make Moderator's name blue, I would like to use RGB so if u have any command for that or something it would be great :) (I don't wanna make whole text blue, just name)
If you are the one drawing the text yourself then DrawText() (or any other GDI text-drawing functions) will draw the text using the fore color set in the device context.
You can't do what you want with stock dialog forms. You'll have to create your own dialog window and override the window procedure and handle the WM_PAINT yourself.
If you swap your Edit control to a Rich Edit you can control the color using RTF markup.
1 2 3 4 5 6 7 8
std::string strRTF = _T("{\\rtf1\\ansi\\ansicpg0\\deff0");
strRTF += _T("{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}");
strRTF += _T("This line is the default color\\line");
strRTF += _T("\\cf2 ");
strRTF += _T("This line is red\\line");
strRTF += _T("\\cf1 ");
strRTF += _T("This line is the default color");
strRTF += _T("}");
Where "\\cf2 " = use 2nd entry in color table
But I don't think you can use SetDlgItemText to set this kind of text. I use EM_SETTEXTEX, after using EM_SETTEXTMODE to set the correct mode (TM_RICHTEXT) and EM_SETTARGETDEVICE to set the device (hdc = NULL, line width = 0)
CONTROL "",IDC_EDIT_ANSWER,"RichEdit20A",ES_MULTILINE | ES_READONLY,13,214,301,29
how does it affect my program? can I use same commands like with edit control?
Most of the standard Edit control's functionality is supported by Rich Edit. But some events have to be enabled using EM_SETEVENTMASK, though this might not be an issue if you just want to display colored text.
The overall string is made up of a header which defines the encoding (ansi) and codepage (0), and then defines a color table. This one contains just two colors.
then it defines 3 lines. \cf1, cf2 are switching the color of the text (the number is the 1-based index into the color table); \line is the RTF line break command, used instead of \n or \r\n
1 2 3
This line is the default color\line
\cf2 This line is red\line
\cf1 This line is the default color
and close the rtf doc (like C++, braces are used to define scopes)
main.cpp(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
main.cpp(5): error C2365: 'LoadLibraryA' : redefinition; previous definition was 'function'
winbase.h(7160) : see declaration of 'LoadLibraryA'
main.cpp(5): error C2440: 'initializing' : cannot convert from 'const char [13]' to 'int'
There is no context in which this conversion is possible
okay, it's working, but,
#1 how can I make it to automatically, if I send long message, it will "break" it and then put it in next line? (In output), I'll send u a picture to explain it better :P
cuz when I write a lot of messages, it will write them there, but you can't see them (and I wanna make it automatically scroll down when new message is sended)
#1 Make sure you are not using ES_AUTOHSCROLL -- when auto hscroll is off, and it's multi-line, it should wrap
#2 You need WS_VSCROLL and ES_AUTOVSCROLL -- the former will cause a vertical scrollbar to appear when needed; the auto vscroll shouldl scroll the window to display the new text when added.
Andy
(P.S. the above all need to be added/removed from the .rc file)
ehh, it's working, again :D thanks, I have almost everything, I just have problems with writing multiline text in rich edit, and with that colors, this is what I have:
as u can see, it will write it to one line, I tried both, \r\n and \\line, but nothing is working, why? O_O + my name is not red in first message, this happens when I send second message
If I had to guess, the text you're getting back from your call to GetDlgItemText has lost its RTF markup. So when you rebuild the string, only the new text has any markup.
To test this, trying writing the contents of messages to a file or the debugger
it's working well, thanks, I just need to explain that last 4 lines :P (17-20)
edit: I just noticed that if I write something and rich edit will be full, that scoll bar on the right will appear, but it won't automatically scroll down, how to fix that? I need to scoll it down manually
The last 4 lines are appending text by:
- getting the length of the existing text
- setting the current selection to a zero length range at the end of the string
- replacing the selected text with the new text
The assert is prob totally neurotic
This is the standard way to do append text. See following for more info: