Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Textout with wordwrapping and Text Selection

0.00/5 (No votes)
1 Jul 2001 1  
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

  • 2nd July, 2001: Date posted

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.

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