Click here to Skip to main content
11,933,180 members (62,911 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked

CTextRender class

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

TextRender - Sample project.jpg


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.


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:


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


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.


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.


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


About the Author

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.

You may also be interested in...

Comments and Discussions

Questionhow to support a full rtf text string Pin
yangyancheng13-Oct-06 23:27
memberyangyancheng13-Oct-06 23:27 
GeneralNice concept, implementation could be better Pin
Sam Levy16-Aug-06 18:35
memberSam Levy16-Aug-06 18:35 
GeneralSubscript & Superscript Pin
Ralf Wirtz18-Nov-05 7:51
memberRalf Wirtz18-Nov-05 7:51 
GeneralRe: Subscript & Superscript Pin
darkoman19-Nov-05 1:16
memberdarkoman19-Nov-05 1:16 
GeneralMore details Pin
go_gilly20-Oct-05 20:06
membergo_gilly20-Oct-05 20:06 
GeneralRe: More details Pin
darkoman20-Oct-05 22:09
memberdarkoman20-Oct-05 22:09 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 20 Oct 2005
Article Copyright 2005 by darkoman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid