Click here to Skip to main content
Click here to Skip to main content

Textout with wordwrapping and text selection

, 1 Jul 2001
Rate this:
Please Sign up or sign in to vote.
This is a simple text display view with wordwrapping and text selection capabilities.

Sample Image - textviewshot.jpg

Background

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");
AddLine(strText);

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.

CIRCTextView??

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

Copyright

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

History

  • Date Posted: July 2nd 2001

License

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

About the Author

selom ofori

United States United States
No Biography provided

Comments and Discussions

 
GeneralAddLine & Scrolling problem [modified] PinmemberSoniaGeorge3-Jun-06 1:22 
GeneralRe: AddLine & Scrolling problem [modified] Pinmemberldw1986hf12321-Mar-12 21:32 
GeneralNice... PinmemberM.Khadem13-Nov-03 16:58 
GeneralNeed Help! PinmemberMahatab-ur-Rashid23-May-03 2:20 
GeneralRe: Need Help! Pinmemberspring15-Aug-03 19:42 
GeneralPossible bug Pinmembermschuckmann8-May-03 13:49 
GeneralREG_QWORD PinmemberMel28-Nov-02 6:06 
GeneralRe: REG_QWORD Pinmembermschuckmann8-May-03 9:10 
GeneralRe: REG_QWORD Pinmemberspring15-Aug-03 19:37 
GeneralPossibility to run in a dialog?! :) PinmemberLargie17-Jul-01 21:42 
GeneralRe: Possibility to run in a dialog?! :) PinmemberAnonymous18-Jul-01 7:31 
GeneralSlow... Pinmemberanonymous1-Jul-01 23:31 
GeneralRe: Slow... PinmemberAnonymous2-Jul-01 6:17 
GeneralRe: Slow... PinmemberNikelman200015-Mar-02 6:19 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 2 Jul 2001
Article Copyright 2001 by selom ofori
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid