Click here to Skip to main content
15,881,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, how I can get the size in pixel of one character?

Like this command in C#

C#
Font font = getGraphics().getFont();

int textWidth = font.stringWidth("Comprimento em pixels desta string!");

int fontHeight = font.getHeight();


But, i want to know, how I can do this in C, using WinApi.
Thanks
Posted
Updated 21-Feb-22 13:03pm

Have a look at:
GDI+ Flat API Text Functions[^].

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sandeep Mewara 15-Jun-12 11:55am    
Good answer. 5!
Espen Harlinn 15-Jun-12 12:40pm    
Thank you, Sandeep!
VJ Reddy 20-Jun-12 22:55pm    
Good answer. 5!
Espen Harlinn 23-Jun-12 4:02am    
Thank you, VJ
C++
HDC		hDC;		// handle to device context
TEXTMETRIC	textMetric;	// text metric information
HFONT		hFont, hOldFont;

hDC = GetDC(hWnd);
// get a 10-point font and select it into the DC
int points = MulDiv(10, GetDeviceCaps(hDC, LOGPIXELSY), 72);
hFont = CreateFont(-points, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, L"Courier New");
hOldFont = SelectFont(hDC, hFont);

GetTextMetrics(hDC, &textMetric);

int currentY = textMetric.tmHeight; // character height in current font


[edit]
You should also check all the Font and Text functions[^], for further information.
[/edit]
 
Share this answer
 
v3
Comments
Alexandre Bencz 16-Jun-12 8:06am    
Thanks (:
VJ Reddy 20-Jun-12 22:54pm    
Good answer. 5!
Richard MacCutchan 21-Jun-12 3:03am    
:thumbsup:
In addition to the solutions provided by Richard and Espen, you can achieve this using a call to DrawText, using DT_CALCRECT as the text-drawing flag.

For single line text, just set the rect members to 0 before use.
For multi-line text, set the 'right' member to the desired width.

C++
HDC hDC;
TEXTMETRIC textMetric;
HFONT   hFont, hOldFont;
int sizeInPpoints, lineHeight;
RECT textRect;
char *buffer = "Comprimento em pixels desta string!";

hDC = GetDC(hwnd);
sizeInPpoints = MulDiv(10, GetDeviceCaps(hDC, LOGPIXELSY), 72);
hFont = CreateFont(-sizeInPpoints, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, "Courier New");
hOldFont = (HFONT)SelectObject(hDC, hFont);

GetTextMetrics(hDC, &textMetric);

lineHeight = textMetric.tmHeight; // character height in current font

textRect.left = textRect.right = textRect.top = textRect.bottom = 0;
DrawText(hDC, buffer, strlen(buffer), &textRect, DT_CALCRECT);

printf("Size of text calculated by DrawText: [%d x %d]\n", textRect.right, textRect.bottom);
printf("Height of text calculated by GetTextMetrics: %d\n", lineHeight);


Output:
Size of text calculated by DrawText: [280 x 16]
Height of text calculated by GetTextMetrics: 16
 
Share this answer
 
Comments
Alexandre Bencz 16-Jun-12 8:03am    
Thanks (:
Muito obrigado (:
VJ Reddy 20-Jun-12 22:55pm    
Good answer. 5!
I know this question was posted 10 years ago, but I had the same problem when I came across this page and I didn't feel the other answers fully solved it. I eventually figured it out so I'll post here.

The OP asks for the "size in pixel of one character".

GetCharWidth32 takes as input a sequence of characters and returns their individual widths in a buffer. These widths are in logical coordinates, which are pixels by default, unless your program set a different mapping mode for the device context you passed to GetCharWidth32. If so, call SetMapMode(hdc, MM_TEXT) before calling GetCharWidth32 as MM_TEXT is the only mapping mode that returns pixel size. The Microsoft docs say GetCharWidth32 can't be used with TrueType fonts.

GetCharWidthFloat is the same as GetCharWidth32 except it returns floats instead of ints, for fractional widths. I'm not sure if it would return fractions of pixels, or if this is only useful for different mapping modes. The docs don't mention whether it works or not with TrueType fonts, I haven't tested it.

GetCharABCWidths only works with TrueType fonts and provides more info.
A: Width of white space required to the left of the character.
B: The actual width of the character from the pixel furthest to the left to the pixel furthest to the right.
C: Width of white space required to the right.
This is where the 'ABC' in GetCharABCWidths comes from.
If a character is designed to 'overhang' into an adjacent character (like a cursive 'f'), A and C will be negative values.
The width of the character (taking into account white space and possible overhang) is calculated by adding A, B and C. This is the value I was after to calculate the position of the blinking caret (or cursor) in a text editing program when dealing with variable-width fonts.

GetCharABCWidthsFloat is the same as GetCharABCWidthFloat except it returns floats instead of ints, for fractional widths.

It looks like there aren't equivalent functions that return character height on a per-character basis, but rather on a per-string basis (GetTextExtentPoint32 and GetTabbedTextExtent) or overall font basis (GetTextMetrics and GetOutlineTextMetrics).
You could pass individual characters to GetTextExtentPoint32 and GetTabbedTextExtent if need be.

Hope this is helpful to someone!
 
Share this answer
 
Comments
CPallini 22-Feb-22 2:48am    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900