Home > Net >  Win32api how to get pixel width of char
Win32api how to get pixel width of char

Time:09-19

I know GetTextExtentPoint function can get width of string. However, I want to get width of char. Do you know how to get pixel width of char?

I haven't good at english.

CodePudding user response:

GetTextExtentPoint() is a deprecated 16bit function. Use the 32bit GetTextExtentPoint32() function instead.

Either way, both functions allow you to pass in a pointer to a single character without needing to include a null-terminator, eg:

int getCharWidth(HDC hdc, char ch)
{
    SIZE size;
    //GetTextExtentPointA(hdc, &ch, 1, &size);
    GetTextExtentPoint32A(hdc, &ch, 1, &size);
    return size.cx;
}
HDC hdc = ...;
HFONT hOldFont = (HFONT) SelectObject(hdc, desired font...);
...
int width = getCharWidth(hdc, 'A');
// use width as needed...
...
SelectObject(hdc, hOldFont);
  • Related