News: :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

TextOut in VC++

Started by sukishan, Jul 12, 2009, 06:43 PM

Previous topic - Next topic


The TextOut function writes a character string at the specified location, using the currently selected font, background color, and text color.

BOOL TextOut(

HDC hdc, // handle to device context

int x, // x-coordinate of starting position

int y, // y-coordinate of starting position

LPCTSTR lpString, // pointer to string

int cbString // number of characters in string


The first argument is the handle to the device context either the hdc value returned from GetDC or from BeginPaint during the processing of a WM_PAINT message. The second and third arguments x and y defines the starting point of the character string within the client area. The fourth argument lpsring is a pointer to character string and the last argument cbString indicates the number of characters in the string.

Eg: TextOut (hdc,x,y,"LeftButtonPressed",17);

System Font and Text Metrics

The device context defines the font that windows uses to display the text when TextOut function is called. The default font is a system font which is used by windows for text strings in title bars, menus and dialog boxes. In earlier versions of windows the system font was a fixed pitch font in which all the characters had the same width. But now the system font is a "raster font" which means that the characters are defined as blocks of pixels. The size of a character is based on the size of the video display.

To display multiple lines of text by using the TextOut function the dimensions of characters in the font should be known The successive lines of the text can be spaced based on the height of the characters and the columns of the text can be spaced on the average width of the characters. Just as a program can determine information about the sizes or metrics of user interface items by calling the GetSystemMetrics function, a program can determine font sizes by calling GetTextMetrics. GetTextMetrics requires a handle to a device context because it returns information about the font currently selected in the device context. The TEXTMETRIC structure has 20 fields but only the first seven fields are essential.

typedef struct tagTEXTMETRIC


LONG tmHeight;

LONG tmAscent;

LONG tmDescent;

LONG tmInternalLeading;

LONG tmExternalLeading;

LONG tmAveCharWidth;

LONG tmMaxCharWidth;

[ other structure fields]


To use the GetTextMetrics function a structure variable tm should be defined.


To determine the text metrics you get a handle to a device context and call GetTextMetrics

hdc = GetDC (hwnd) ;

GetTextMetrics (hdc , &tm);

ReleaseDC (hwnd, hdc);

The most important value in TEXTMETRIC is the tmHeight which is the sum of tmAscent and tmDescent. These two values represent the maximum vertical extents of characters in the font above and below base line. The term leading refers to the space that the printer inserts between the lines of text. In the TEXTMETRIC structure internal leading is the space in which accent marks appear and the external leading is not included in the tmHeight value. It is an amount of space that the designer of the font suggests be added between successive rows of displayed text. The TEXTMETRIC structure contains two fields that describes the character width one is tmAveCharWidth field which is a weighted average of lowercase characters and another is tmMaxCharWidth which is the width of the widest character in the font.
A good beginning makes a good ending

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview