Click here to Skip to main content
15,609,135 members
Articles / Desktop Programming / MFC
Posted 30 Jun 2001


23 bookmarked

Textout with wordwrapping and Text Selection

Rate me:
Please Sign up or sign in to vote.
4.25/5 (7 votes)
1 Jul 20013 min read
This is a simple text display view with wordwrapping and text selection capabilities.

Sample Image - textviewshot.jpg


CTextView is a CView derived class that displays read-only text on the view. What makes it special is the ability to do word wrapping and text selection. This class is a modification of a class I made to parse and display IRC messages exactly like the view in MIRC. The only difference here is the direction of the text. IRC views usually display text from bottom going up, but CTextView does it normally from top to bottom just like Notepad.

You may prefer CTextView over CEditView in instances where you regularly append text to a read-only view and do not want the scrollbar jumping all over the place, where you want to easily add colors without resorting to subclassing the edit control, where you want something slick, smooth and not too bulky. CTextView has no text limit. It uses the IRC color coding scheme to display colors. The picture above is a display of an MIRC log file containing colors.

Using CTextView in Your Code

To use CTextView in your code, you must change your CView derived class to CTextView derived one. This can be accomplished with a simple text replacement in your derived classes' .h and .cpp files. Import TextView.h, TextView.cpp, Memdc.h, Memdc.cpp, FontObject.h, FontObject.cpp, AutoFont.h, AutoFont.cpp, RegistryManager.h and RegistryManager.cpp into your project.

Adding Text

CTextView::AddLine(CString str)

Appends a line of text to the view. To display colors, format it using the IRC color scheme model, e.g.:

strText.Format("\x03%02d%s\x03 %s",8,"hello","world");

will display "hello" in yellow and "world" in the default color. The \x03 are color markers. The first \x03 means start color and the last \x03 means stop color. The actual color number must be in xx format, e.g., 08 or 02 or 11 (don't exceed 15).

Word Wrapping

void CTextView::SetWordWrap(bool bWrapText)

Changes the word-wrapping mode. If word-wrapping is enabled, a horizontal bar is displayed. Otherwise, it is hidden.

Text Selection

The selected text is automatically copied to the clipboard when the mouse is released. Just like in MIRC.

Changing the Font

void CTextView::SetFont( LOGFONT& rFont )

Sets the font using a logfont structure. I included a class I made called CFontObject which can serialize the font to the registry. I also include my registry class which CFontObject uses for that purpose. Another class called CAutoFont I found on CodeProject is used by CFontObject to create fonts easily. Just call CFontObject("Arial") and a properly sized Arial font will be used. If you don't specify a font, the system font is used.

Background, Foreground and Color Table

These can be changed in the constructor. You can easily add functions to change them at will.

Flat Scrollbars

#define USEFLATSB 1

to enable flat scrollbars. Comment it out to use normal scrollbars.

To Those Who May Modify the Code

For normal use, the only function you'll probably deal with is AddLine. But if you intend to change the source code or tweak your derived classes, there are a few things you should know. If the size of the window or font changes, you should call recalclines(), updatevscroll() and updatehscroll(). If you override OnPaint() in your derived class, please call CTextView::Paint() instead of CTextView::OnPaint(). Remember to pass the correct client rect. If the client rect changes, call SetClientRect() with the correct client rect. CTextView uses the HWND's scrollbars for scrolling. If you need to modify this (especially if you intend to add your own editboxes and listboxes or whatever to the view), replace the scrollbar calls with calls to your own scrollbar class and remove WM_VSCROLL and WM_HSCROLL style from the HWND.


I actually did that before this. But I'm still tweaking it. Will release it soon enough.


This code is free, as in free sand in the desert. No copyrights except for the portions where other classes are used, namely CMemDc and CAutoFont.


  • 2nd July, 2001: Date posted


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralAddLine & Scrolling problem [modified] Pin
SoniaGeorge3-Jun-06 1:22
SoniaGeorge3-Jun-06 1:22 
GeneralRe: AddLine & Scrolling problem [modified] Pin
ldw1986hf12321-Mar-12 21:32
ldw1986hf12321-Mar-12 21:32 
GeneralNice... Pin
M.Khadem13-Nov-03 16:58
M.Khadem13-Nov-03 16:58 
GeneralNeed Help! Pin
Member 29512823-May-03 2:20
Member 29512823-May-03 2:20 
GeneralRe: Need Help! Pin
spring15-Aug-03 19:42
spring15-Aug-03 19:42 
GeneralPossible bug Pin
mschuckmann8-May-03 13:49
mschuckmann8-May-03 13:49 
GeneralREG_QWORD Pin
Ancient Dragon28-Nov-02 6:06
Ancient Dragon28-Nov-02 6:06 
GeneralRe: REG_QWORD Pin
mschuckmann8-May-03 9:10
mschuckmann8-May-03 9:10 
GeneralRe: REG_QWORD Pin
spring15-Aug-03 19:37
spring15-Aug-03 19:37 
GeneralPossibility to run in a dialog?! :) Pin
Largie17-Jul-01 21:42
Largie17-Jul-01 21:42 
GeneralRe: Possibility to run in a dialog?! :) Pin
18-Jul-01 7:31
suss18-Jul-01 7:31 
GeneralSlow... Pin
1-Jul-01 23:31
suss1-Jul-01 23:31 
GeneralRe: Slow... Pin
2-Jul-01 6:17
suss2-Jul-01 6:17 
GeneralRe: Slow... Pin
15-Mar-02 6:19
suss15-Mar-02 6:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.