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
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
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.
Appends a line of text to the view. To display colors, format it using the IRC color scheme model e.g.
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).
void CTextView::SetWordWrap(bool bWrapText)
Changes the word-wrapping mode. If word-wrapping is enabled, a horizontal bar is displayed. Otherwise it is hidden.
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.
#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
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_HSCROLL style from the
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
- Date Posted: July 2nd 2001