Length of Bitmap Font Strings

I’m using the WGL routines to create a bitmap font in OpenGL. I’m trying to find a way to calculate the width in pixels of a string in a bitmap font. I’ve tried GetCharWidth32() and GetCharABCWidths(). Unless I’m using them incorrectly, they both consider “lllll” and “WWWWW” to be the same width even though I’m using a variable-width font. If someone could point me to a method by which I could accomplish this, I would be very grateful.

Try this guy:

BOOL GetTextExtentPoint32(
HDC hdc, // handle to device context
LPCTSTR lpString, // pointer to text string
int cbString, // number of characters in string
LPSIZE lpSize // pointer to structure for string size

But remember that any scaling you do on the gl matrix stack will affect the rendered text.

I tried that as well. Same problem. Ideally, I’d like to be able to get the widths of all 256 characters and put them in an integer array, and use that array to calculate widths of strings. I’ve been able to do that with all three of these functions, but it’s always used the same value for every entry.

You need to make sure you have your font selected in th dc:

HFONT oldFont = (HFONT)SelectObject( hDC, myFont );

const char* text = “my dog has fleas”;
SIZE extent;
GetTextExtentPoint32( hDC, text, strlen(text), &extent );

// now extent contains the width and height

SelectObject( hDC, oldFont );

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.