Rich Text

I am building an interface which consist of a edit text. To display text in the edit box, I have to file the text in the .txt file (notepad) and display on the edit box by using the SetWindowText. Any way, I found that I have to align the text so that it can be displayed beautifully. This is not a good way in programming. Furthermore, I can't cahnge the text color if I use the SetWindowText(). I want to change the edit box to rich text because the rich text can be edited, copied and pasted. How can I use the rich text? Should I change the edit box formating? Please show me the way and the example.
Who is Participating?
wyllikerConnect With a Mentor Commented:

If you only need to change the color of all of the text - not just some of it, the following should help.

An edit control that is not read-only or disabled sends the WM_CTLCOLOREDIT message to its parent window when the control is about to be drawn. By responding to this message, the parent window can use the specified device context handle to set the text and background colors of the edit control.

here are some colors - they correspond to an RGB structure
though a long value can be specified as such (note reversal is due to intel endian, so effectively RGB = 0BGR) Each 'byte' of the long ranges 0 to 255 to set the amount of each color in the mix - first should be 0 but is probably ignored.

Somewhere at the top of your code ...

#define COLOR_RED     0x00000080
#define COLOR_GREEN   0x00008000
#define COLOR_BLUE    0x00800000
#define COLOR_PURPLE  0x00800080

static HBRUSH WhiteBrush = (HBRUSH) NULL;
static HBRUSH LtGrayBrush = (HBRUSH) NULL;

static HBRUSH RedBrush = (HBRUSH) NULL;
static HBRUSH GreenBrush = (HBRUSH) NULL;
static HBRUSH BlueBrush = (HBRUSH) NULL;

Somewhere in the winmain function - before entering the message loop add the following call


See the function I have provided below - it creates a set of background brushes so that you can set the background color as well.

In your message loop add the following - modify as necessary for your colors - this will produce Green text on a light gray background

     hDC = (HDC) wParam;
     SetBkMode(hDC, TRANSPARENT);

     if(GetDlgCtrlID((HWND) lParam) == IDC_ENTRY)
         SetTextColor(hDC, (COLORREF) COLOR_GREEN);

     return (LRESULT) LtGrayBrush;

If an application processes this message, it must return the handle of a brush. The system uses the brush to paint the background of the edit control.

This function create background brushes - these can be any color (again in RGB format)

int CreateGDIObjects ( void )
int RetCode = 0;

      WhiteBrush = (HBRUSH) GetStockObject(WHITE_BRUSH);
      LtGrayBrush = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

      LogBrush.lbStyle = BS_SOLID;
      LogBrush.lbHatch = 0;

      LogBrush.lbColor = COLOR_RED;
      RedBrush = CreateBrushIndirect(&LogBrush);

      LogBrush.lbColor = COLOR_GREEN;
      GreenBrush = CreateBrushIndirect(&LogBrush);

      LogBrush.lbColor = COLOR_BLUE;
      BlueBrush = CreateBrushIndirect(&LogBrush);

      return RetCode;

Finally, somewhere at the end of winmain before you exit, call the following


int DeleteGDIObjects ( void )
int RetCode = 0;

 DeleteObject((HGDIOBJ) RedBrush);
 DeleteObject((HGDIOBJ) BlueBrush);
 DeleteObject((HGDIOBJ) GreenBrush);

// it is not necessary, but not harmful to also delete the
// stock objects

 DeleteObject((HGDIOBJ) WhiteBrush);
 DeleteObject((HGDIOBJ) LtGrayBrush);

    return RetCode;

This uses module level globals, so isn't the cleanest code in the world but it should give you an idea of how to do it.
Do some reading on the functions used in the code sample, there are many options that you have - again if this is all or nothing (the whole edit contents not just some of it).

If you need something more sophisticated then you will have to go with RichEdit.

Good luck.
i know this is off line ur question but did u take ur elem in pchs?
I assume, you are writing code using the windows API.If I'm right, use a rich edit control (richedXX.dll). It is included in standard win installations (>= WIN95).
Easy way for accessing the RTF control is using a framework
(e.g. Inprise,Borland - TRichEdit, Microsoft - CRichEditView ... )
If you have above compilers.., you will find examples...

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

jessicalimAuthor Commented:
Thanks. I can change the edit box's background to lightgray. I am using SetWindowText() to display the text. I can't use TextOut() as the TextOut() can't diplay the text well. The background of the text still white if I use the SetWindowText(). Is there any way to change the backgrouns of the text to lightgray, the font to arial n the text color to blue?
jessicalimAuthor Commented:
Can you please show me how to create rich edit and give me some example? Actually, I have tried to create the rich edit by using the CreateWindowEx. But the rich edit do not appear.

// here is part of the source code
hEdit = CreateWindowEx(WS_EX_WINDOWEDGE,"RichEdit","Rich Edit Rich Edit",
ShowWindow(hEdit, SW_SHOW);
SendMessage(hEdit, EM_GETLIMITTEXT, 200, 1);
// I try to display the text
SetWindowText(hEdit, (LPSTR) "Rich Edit Text");

Have I done anything wrong?
I have loaded the RICHED32.DLL, n I have included the RICHEDIT.H...


Do the following to set up the display for the richedit.

// to set the font for the entire control to
// Arial 10pt Bold Color Blue on a light gray background

#define COLOR_GREEN  0x00008000
#define COLOR_LTGRAY 0x00808080

long lResult = 0;

CharFormat.cbSize = sizeof(CHARFORMAT);
CharFormat.dwMask = CFM_BOLD | CFM_SIZE | CFM_FACE;
CharFormat.dwEffects = CFE_BOLD;
CharFormat.yHeight = 200;    // in twips 1/20 of a point
CharFormat.szFaceName = "Arial";  
CharFormat.crTextColor = COLOR_GREEN;

lResult = SendMessage(hRiched, EM_SETCHARFORMAT, (WPARAM) SCF_ALL, (LPARAM) &CharFormat);

lResult = SendMessage(hRiched, EM_SETBKGNDCOLOR, (WPARAM) 1, (LPARAM) COLOR_LTGRAY);

Do the show window call after these messages.

jessicalimAuthor Commented:
I have tried. But the rich edit box still cannot diplay. It is very strange because I have loaded the DLL and I have also initialized the InitCommonControlsEx(). I called the cc.dwICC = ICC_WIN95_CLASSES; as there is no specific command for the rich edit box. And with the source code I posted to you before, the rich edit box suppose can be created. I still can't neither display the rich edit box nor display text on it.
jessicalimAuthor Commented:
I used the TextOut() to display the text after I showed the window. I found the rich edit box appear before the main window is created. n It won't appear inside the main window.
Is hEditWnd (hwndParent - The 4th from last parameter) the handle of your main window?   Check if this value is NULL prior to your create window call, to be sure that it has a value.  It has to be the handle of your main window (or dialog if you are using a dialog - I suppose you aren't).

You are creating this in the WM_CREATE handler for hEditWnd?
If from a dialog you would do it at WM_INITDIALOG.  If you had a multiple document interface, you might do this at WM_MDICREATE.

BTW ...
You don't need to call ShowWindow for this window, if you have specified visible as a style.

jessicalimAuthor Commented:
Yes, I created the rich edit box at the WM_CREATE:  at first. Finally, I found the rich edit box can only come out when I click on a button or a item of the menu. I don't know why it acts in this way. Although I can get the rich edit right now, I am still want it to be created when the main window appear.

The hEditWnd is compatible with the hWnd actually. Why I can't create the rich edit box in WM_CREATE or WM_INITDIALOG?
I am not sure what you mean by "hEditWnd is compatible with the hWnd" - Is hWnd the main window handle or not - The parent window of the edit control MUST be the main window or dialog window or it will not display as part of that window no matter what you do.  In order for the richedit window to be redrawn and updated, and to send notification messages to the main window (or dialog if you are using one), this must be set properly eg. hwndMain or hwndDialog (whatever the window handle is for:

hwndMain = CreateWindowEx( ... main window parameters )
hwndDialog = CreateDialog ( ... mydialog parameters )

Reusing your example code it would be

hEdit = CreateWindowEx(WS_EX_WINDOWEDGE,"RichEdit","Rich Edit Rich Edit",
hwndMain, NULL, ghInstance, NULL);


hEdit = CreateWindowEx(WS_EX_WINDOWEDGE,"RichEdit","Rich Edit Rich Edit",
 hwndDialog, NULL, ghInstance, NULL);

Also ...

If the contents of the rich edit is only appearing after you press a button -  didn't you post a similar question about this concerning a list box and trying to populate an edit control with the current selection and it not working?

Are you not handling your event loop (message procedure) properly?

Are you using SetWindowText after your show window call instead of before?

When you update the text in the window are you Invalidating the window and calling update window to force a redraw of the window?  Not that you should have to - but just to see if you can force it.

Better yet, during the handling of what message are you calling SetWindowText to display your text?

jessicalimAuthor Commented:
I got the rich edit box now. I programmed the create rich edit box on item menu. So, it can be displayed after the menu item been pressed. I tried to put the source in other graphics components. It's works. Why can't I create the rich edit in the WM_CREATE?

In the source code, the hWnd is the main window handler.
All Courses

From novice to tech pro — start learning today.