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

A Powerful and Flexible Text Drawing Function which has Simple Markup Tags

0.00/5 (No votes)
1 Sep 2004 1  
This function makes for you to draw a fancy text easy.

Introduction

Have you ever wanted a more powerful and flexible function for drawing of text? If you want to draw a text like below, you would call many dc functions.

Hello, World.

You should call SetBkColor to change a text color, and call SelectObject to change a text font. Maybe your code looks like this:

create a bold font
select a bold font
draw "he"
select a original font
delete a bold font object
draw "llo, "
change a text color red.
draw "W"
change a text color original.
draw "orld."

What a bored code it is! Furthermore, if you use codes like the above, you make a mistake easily. Finally, I decided to write a function which supports some simple markup tags. This article shows you how to use it.

Function Description

int FmtUtil::TextOut(CDC *pDC, CString &str, int x=0, int y=0, int nSpace=0);
  • pDC

    [in] Handle to the device context

  • str

    [in] The string to be drawn

  • x

    [in] Specifies the x-coordinate, in logical coordinates

  • y

    [in] Specifies the y-coordinate, in logical coordinates

  • nSpace

    [in] Specifies the space between lines

This function supports the following simple tags like HTML. Note that tags included in str are interpreted by this function, and those are not drawn. So, if you want to draw [ or ], you should use [[ or ]].

  • [B][/B]

    This tag makes a font bold.

  • [I][/I]

    This tag makes a font italic.

  • [U][/U]

    This tag makes a font underline.

  • [Cxxxxxx][/C]

    This tag changes a font color. xxxxxx is a color code. This is hexadecimal value, e.g., FF0000 for red.

Using the Code

If you want to use it in your project - follow these simple steps.

  • Add fmttextout.h and fmttextout.cpp files to your project.
  • Now you can draw a text by calling FmtUtil()->TextOut.

The following code is a WM_PAINT message handler used in the demo project:

void CChildView::OnPaint() 
{
    CPaintDC dc(this); // device context for painting
    
    // TODO: Add your message handler code here
    
    // Do not call CWnd::OnPaint() for painting messages
    CString str("Hello, [/B][/B][/B]W[/B][CFE09FF]or[/C][U]ld[/U] 
          [I]Shin, YoungJin[/I]\r\naskdfljaslkfjaslkfjlkewrlkewr");
    FmtUtil()->TextOut(&dc, str, 10, 10, 8);
}

History

  • 30th August, 2004 - Initial version

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