Click here to Skip to main content
Licence 
First Posted 31 Aug 2004
Views 47,361
Bookmarked 24 times

A powerful and flexible text drawing function which has simple markup tags

By | 1 Sep 2004 | Article
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 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.

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

  • 30 Aug 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

About the Author

YoungJin Shin



Korea (Republic Of) Korea (Republic Of)

Member



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
GeneralError in your sample PinmemberHadi Dayvary0:22 8 Nov '06  
GeneralAn advice Pinmembersnipersun19:23 17 Apr '06  
Generalquestion PinsussAnonymous6:09 3 Sep '04  
GeneralRe: question PinmemberShin, YoungJin17:33 3 Sep '04  
GeneralExcellent Code!!! PinmemberBalkrishna Talele20:52 1 Sep '04  
GeneralRe: Excellent Code!!! PinmemberShin, YoungJin6:09 2 Sep '04  
Generalsimple and effective Pinmemberemilio_grv20:39 31 Aug '04  
GeneralRe: simple and effective PinmemberShin, YoungJin6:08 2 Sep '04  

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
Web03 | 2.5.120517.1 | Last Updated 2 Sep 2004
Article Copyright 2004 by YoungJin Shin
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid