Click here to Skip to main content
Licence 
First Posted 20 Oct 2005
Views 31,743
Bookmarked 27 times

CTextRender class

By | 20 Oct 2005 | Article
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 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

darkoman

Software Developer (Senior)
Elektromehanika d.o.o. Nis
Serbia Serbia

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow to support a full rtf text string Pinmemberyangyancheng22:27 13 Oct '06  
GeneralNice concept, implementation could be better PinPopularmemberSam Levy17:35 16 Aug '06  
GeneralSubscript & Superscript PinmemberRalf Wirtz6:51 18 Nov '05  
GeneralRe: Subscript & Superscript Pinmemberdarkoman0:16 19 Nov '05  
GeneralMore details Pinmembergo_gilly19:06 20 Oct '05  
GeneralRe: More details Pinmemberdarkoman21:09 20 Oct '05  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 20 Oct 2005
Article Copyright 2005 by darkoman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid