Click here to Skip to main content
15,880,469 members
Articles / Multimedia / GDI

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

Rate me:
Please Sign up or sign in to vote.
4.65/5 (21 votes)
1 Sep 20042 min read 67.2K   1.4K   27   8
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

C++
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:

C++
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.


Written By
Chief Technology Officer Wellbia.com Co., Ltd.
Korea (Republic of) Korea (Republic of)
YoungJin is a co-founder of Wellbia.com Co., Ltd., a security company in South Korea, and Visual C++ Microsoft Most Valuable Professional. He has developed anti-cheat program called XIGNCODE since 2007. He wrote several PC security programs like PC Firewall, Anti-Spyware, and Keyboard Security Software. He has contributed a number of articles about Windows programming to Microsoftware, the famous programming magazine of South Korea. He also hosts a blog (http://www.jiniya.net) that includes articles about system programming on Windows.

Comments and Discussions

 
GeneralError in your sample Pin
Hadi Dayvary8-Nov-06 0:22
professionalHadi Dayvary8-Nov-06 0:22 
GeneralAn advice Pin
snipersun17-Apr-06 19:23
snipersun17-Apr-06 19:23 
Generalquestion Pin
Member 110820413-Sep-04 6:09
Member 110820413-Sep-04 6:09 
GeneralRe: question Pin
YoungJin Shin3-Sep-04 17:33
YoungJin Shin3-Sep-04 17:33 
GeneralExcellent Code!!! Pin
Balkrishna Talele1-Sep-04 20:52
Balkrishna Talele1-Sep-04 20:52 
GeneralRe: Excellent Code!!! Pin
YoungJin Shin2-Sep-04 6:09
YoungJin Shin2-Sep-04 6:09 
Generalsimple and effective Pin
Emilio Garavaglia31-Aug-04 20:39
Emilio Garavaglia31-Aug-04 20:39 
GeneralRe: simple and effective Pin
YoungJin Shin2-Sep-04 6:08
YoungJin Shin2-Sep-04 6:08 

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.