Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rich Text

Posted on 1999-07-08
12
Medium Priority
?
499 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:jessicalim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 1

Expert Comment

by:DARKMOON
ID: 1263425
hello,
i know this is off line ur question but did u take ur elem in pchs?
0
 

Expert Comment

by:kralubo
ID: 1263426
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...
 luc
 


   
0
 
LVL 4

Accepted Solution

by:
wylliker earned 100 total points
ID: 1263427
jessicalim,

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

CreateGDIObjects();

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

case WM_CTLCOLOREDIT:
     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;
LOGBRUSH LogBrush;

      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

DeleteGDIObjects();

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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:jessicalim
ID: 1263428
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?
0
 

Author Comment

by:jessicalim
ID: 1263429
Wylliker,
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",
WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_SUNKEN|ES_SAVESEL|ES_LEFT|WS_BORDER,0, y/4, 391*x/400, 79*y/120, hEditWnd, NULL, ghInstance, NULL);
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...

0
 
LVL 4

Expert Comment

by:wylliker
ID: 1263430
OK,

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 CharFormat;

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.


0
 

Author Comment

by:jessicalim
ID: 1263431
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.
0
 

Author Comment

by:jessicalim
ID: 1263432
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.
0
 
LVL 4

Expert Comment

by:wylliker
ID: 1263433
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.




0
 

Author Comment

by:jessicalim
ID: 1263434
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?
0
 
LVL 4

Expert Comment

by:wylliker
ID: 1263435
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 )
or
hwndDialog = CreateDialog ( ... mydialog parameters )

Reusing your example code it would be

hEdit = CreateWindowEx(WS_EX_WINDOWEDGE,"RichEdit","Rich Edit Rich Edit",
WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_SUNKEN|ES_SAVESEL|ES_LEFT|WS_BORDER,0, y/4, 391*x/400, 79*y/120,
hwndMain, NULL, ghInstance, NULL);

or

hEdit = CreateWindowEx(WS_EX_WINDOWEDGE,"RichEdit","Rich Edit Rich Edit",
WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_SUNKEN|ES_SAVESEL|ES_LEFT|WS_BORDER,0, y/4, 391*x/400, 79*y/120,
 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?






0
 

Author Comment

by:jessicalim
ID: 1263436
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question