Solved

Colored Text w/ Edit Boxes (MSVC++)

Posted on 1998-01-19
9
247 Views
Last Modified: 2013-12-03

  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..
0
Comment
Question by:tcola
  • 5
  • 3
9 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
Comment Utility
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.
0
 

Author Comment

by:tcola
Comment Utility
  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
0
 

Author Comment

by:tcola
Comment Utility
Since you don't seem to be responding to this I'm going to reopen the question.
0
 
LVL 4

Accepted Solution

by:
tflai earned 50 total points
Comment Utility
Look at the example Rich Edit Control codes at:
http://www.codeguru.com/richedit/richedit_toc.shtml
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:tcola
Comment Utility
 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 (?).
0
 
LVL 4

Expert Comment

by:tflai
Comment Utility
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(...)
0
 

Author Comment

by:tcola
Comment Utility
 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.
0
 
LVL 4

Expert Comment

by:tflai
Comment Utility
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 ) );
0
 

Author Comment

by:tcola
Comment Utility
I have MSVC++ 5.0
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now