Link to home
Start Free TrialLog in
Avatar of tcola
tcola

asked on

Colored Text w/ Edit Boxes (MSVC++)


  Currently in my windows program I need to "color" certain
letters in a read-only edit box I have in my dialog
based application.  So far, I've just been creating
objects of type CFont the same font as the dialog box,
figuring out the height/width of the font, and calling the GetDC function of the edit box, then over-writing the
appropriate text in the edit box with portions of the text
in different colors.

   As one could imagine this can be very tedious and its
hard to get everything lined up correctly!  The CEdit class
has a function for returning the topmost line which
is being viewed, so I can find that and make the changes
in my painting function, however CEdit has no function
for returning the horizontal scroll position.  And Horizontally, Edit boxes can be scrolled by pixel, not by column, and lining text up would be very tricky anyway.

  Soo, my question is, Is there any public domain class or
existic mfc class that I can use to just display text and
scroll through it just like CEdit does, and be able to
specify RGB values for characters/lines of text??  I DON'T
need the user to be able to input in such an object.

  Thanks in advance..
Avatar of Tommy Hui
Tommy Hui

Why don't you use the Rich Edit control? That gives you more flexibility in controlling the colors of each character if you need it.
Avatar of tcola

ASKER

  I forgot about that!  I never really looked close enough
into the its class functions though to realize you can control
it that much.  I guess one would have to select the text
to be "colored" with the SetSel function and then
call the SetSelAttrib(?) function to change it.

   When I put a Rich Edit box in the MSCV Editor in my dialog and then go into the class wizard, I can't associate a CString variable with it??  How do I put text into the edit box??

  thanks
Avatar of tcola

ASKER

Since you don't seem to be responding to this I'm going to reopen the question.
ASKER CERTIFIED SOLUTION
Avatar of tflai
tflai

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tcola

ASKER

 How do I associate an object with my rich edit box in the
resource editor?  I tried doing it in the class editor but
the ID for the rich edit box only shows up in the events tab
and not the member variables tab, so I can't pick it to associate
a control object with it (?).
Take the Rich Edit control out of the dialog
(with the resource editor), and then create the Rich edit control
using the Create method:
1) create a separate class for the rich edit control
2) define a variable (m_ctl) of the rich edit control class
3) call Create, like m_ctl.Create(...)
Avatar of tcola

ASKER

 Then you can't edit it in the resource editor, which is
something I want to be able to do so I don't have to recompile,
then modify, etc.
Okay, if you have MSVC 5.0, there is Rich Edit Control on resource editor.  With MSVC 4.2-, you would have to add Rich Edit OLE control, or dynamically create one as shown below:
(Very little work is required.  All you need is some static control to be a placeholder for the rich edit control.  That way you would know exactly what position/size is will be as shown on the resource editor.)

// IDC_EDIT static box is a placeholder for the rich edit control
CWnd* pwnd = GetDlgItem( IDC_EDIT);
ASSERT( pwnd );

// Convert the screen co-ordinates to client co-ordinates
CRect rect;
pwnd->GetWindowRect( &rect );
ScreenToClient( &rect );
VERIFY( m_ctrlRichEdit.Create( ES_MULTILINE | ES_AUTOVSCROLL |
  ES_SUNKEN | ES_NOHIDESEL | WS_VISIBLE | WS_TABSTOP | WS_CHILD |
WS_BORDER, rect, this, IDC_EDIT ) );
Avatar of tcola

ASKER

I have MSVC++ 5.0