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

Tagged as

Go to top

CTextRender class

, 20 Oct 2005
Rate this:
Please Sign up or sign in to vote.
An article on an alternative for TextOut (ExtTextOut) function.

TextRender - Sample project.jpg

Introduction

This article is about improvements made for the TextOut GDI function. With the provided text rendering class, one will be able to perform simple text formatting using RTF tags ("\b", "\i", "\u", "\par", "\r", "\n") and also simple text alignment (left, center, right, top, middle, bottom) in a given bounding rectangle.

Please note that you are not restricted in any way to use this class only in MFC applications.

Background

There are articles on CodeProject on similar subjects, but this is just a first step in developing an easy-to-use but powerful text-formatting function.

Using the code

To use the code include the header file "TextRender.h" in your project. Then add an instance of the CTextRender class. Call the EnhDrawText method with the provided text and that's it.

   #include "TextRender.h"

   // hDC is the device context for text rendering

   CTextRender m_TextRender;
   char text[] = "\\b1Hello, World !!!\\b0\\par\\par\\i1Thi" 
                 "s is a simple demonstration\\par of new\\i0"
                 " \\ul1EnhDrawText\\ul0 \\i1function...\\i0";
   RECT margins = {10, 5, 10, 5};
   RECT textRect = {100, 100, 300, 300};
   DWORD textAlignment = THA_CENTER | TVA_MIDDLE;
   m_TextRender.EnhDrawText( hDC, text, strlen(text), 
                &textRect, &margins, textAlignment );

Now, let's see what the other arguments of EnhDrawText are. The first argument is the handle of the device context for text rendering. The second and third arguments are the text with the formatting tags and the length of the text, respectively. The fourth argument is the text bounding rectangle (all word-breaking will be done in this rectangle). The fifth argument is text margins, and finally the last argument is the text alignment flag. It can have the following values for horizontal alignment:

  • THA_LEFT
  • THA_CENTER
  • THA_RIGHT

Also, it can take the following values for vertical alignment:

  • TVA_TOP
  • TVA_MIDDLE
  • TVA_BOTTOM

Combine these values to achieve different results.

Text formatting tags

The text formatting is performed using basic RTF (Rich Text Format) tags. The following tags are supported in this version of CTextRender class:

  • \b1 - bold text on
  • \b0 - bold text off
  • \i1 - italic text on
  • \i0 - italic text off
  • \u1 - underline text on
  • \u0 - underline text off
  • \par - new line flag, (which is the same as the following combination)
  • \r - carriage-return flag, (if stands alone does nothing)
  • \n - line-feed flag, (can stand alone)

Calculating text height

To calculate the formatted text height use the CalculateTextHeight method of the CTextRender class. See an example below:

   int textHeight = CalculateTextHeight( hDC, text, strlen(text), 
                              textRect, margins, width, height );

   // To set width and height arguments do next
   // width = textRect->right - textRect->left - (margins->right+margins->left);
   // height = textRect->bottom - textRect->top - (margins->top+margins->bottom);

Points of Interest

Working on this problem I realised that basic support for text rendering through ExtTextOut or even DrawTextEx is not enough, so I am trying to upgrade these GDI functions to the level they can be used for advanced text rendering.

History

In this version of CTextRender class basic text formatting and alignment is supported. For the next upgrade it is planned to extend the list of supported RTF tags and to add text justification, and also a UNICODE text support.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

darkoman
Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
He has a master degree in Computer Science at Faculty of Electronics in Nis (Serbia), and works as a C++/C# application developer for Windows platforms since 2001. He likes traveling, reading and meeting new people and cultures.

Comments and Discussions

 
Questionhow to support a full rtf text string Pinmemberyangyancheng13-Oct-06 22:27 
GeneralNice concept, implementation could be better PinmemberSam Levy16-Aug-06 17:35 
GeneralSubscript & Superscript PinmemberRalf Wirtz18-Nov-05 6:51 
GeneralRe: Subscript & Superscript Pinmemberdarkoman19-Nov-05 0:16 
GeneralMore details Pinmembergo_gilly20-Oct-05 19:06 
GeneralRe: More details Pinmemberdarkoman20-Oct-05 21:09 

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
Web01 | 2.8.140926.1 | Last Updated 20 Oct 2005
Article Copyright 2005 by darkoman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid