best way to create line-by-line formatting in a textbox

As an assignment in my class, I need to make a small app that writes text to a textbox based on user input. I have completed the assignment, but would like to make the output prettier. As it is now, the output text is ...boring. I want to format the text line by line and add colored text for certain words. I've been researching rich edit controls and feel like that would be a good way to accomplish this feat. The only problem is that rich edit has no concept of lines.

My idea to work around this limitation is as follows:
Instead of writing the entire text to the control, I can break it up into 3 separate char arrays. The first char[] contains the text header, and would be bold and centered in the control. I would then add a newline (\r in richedit I believe) and get the current cursor position. The 2nd char[] is a string of words (e.g., "touch file1 file2 ...") and the first word in this array should be a different color than the other words. I could write a little loop to pick off the first word and calculate its length, then call SendMessage() to write from the current position to the end of the first word using one color. I get the (new) current position, then write the rest of the words in the array using a different color. The third char[] contains several sentences and can be written in any color, but will be formatted like a standard paragraph.

Does my idea sound good or is there a better way to do this? If anyone has implemented something like this before, could you share the general idea of your solution?





Last edited on
You can add text to a "RICHEDIT" text box and format the added text, changing font color bold underlined italic ect. The format is placed in struct CHARFORMAT or
CHARFORMAT2 and the text to add the new format to is selected by CHARRANGE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void RichEditControl::AppendText(char *text, bool isBold, bool isItalic,
               bool isUnderlined, COLORREF color, char *font, long fontHeight )
{
    CHARFORMAT cf;
    ZeroMemory(&cf,sizeof(cf));
    cf.cbSize=sizeof(cf);
    SendMessage(hRichEditWnd, EM_GETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
    DWORD dwEf = 0;
    if(isBold) dwEf =  CFE_BOLD;
    if(isItalic)dwEf = dwEf | CFE_ITALIC;
    if(isUnderlined) dwEf = dwEf | CFE_UNDERLINE;
    cf.crTextColor = color;
    if(font != NULL)
    {
        lstrcpy(cf.szFaceName,font);
    }
    cf.yHeight = fontHeight;
    cf.dwMask = CFM_UNDERLINE | CFM_COLOR | CFM_BOLD |CFM_ITALIC | CFM_FACE |  CFM_SIZE ;
    cf.dwEffects = dwEf;
    CHARRANGE cr;
    cr.cpMin = GetWindowTextLength (hRichEditWnd);
    cr.cpMax = cr.cpMin + strlen(text);
    SendMessage(hRichEditWnd, EM_EXSETSEL, 0, (LPARAM)&cr);
    SendMessage(hRichEditWnd,EM_SETCHARFORMAT,SCF_SELECTION, (LPARAM) &cf);
    SendMessage (hRichEditWnd, EM_REPLACESEL, 0, (LPARAM) ((LPSTR) text));
}

new line "\n"
demo http://spanish-house.co.uk/cprogram.html
Last edited on
Topic archived. No new replies allowed.