In a Win32 GUI application I need to determine the width of area occupied by a string on a toolbar button so that I adjust the button width accordingly. The toolbar is plain old ToolbarWindow32
windows class. I use the following code:
HDC dc = GetDC( toolbarWindowHandle );
SIZE size;
GetTextExtentPoint32( dc, stringToMeasure, tcslen(stringToMeasure), &size );
For some fixed string (say "Hello") size.cx
is filled with say 72 but when I make a screenshot of the toolbar with the very same string displayed on a button I see that the string occupies say 56 pixels.
The difference clearly depends on system fonts settings. I use "large fonts" and the value obtained by code is bigger than what is occupied on screen. On machines with "small fonts" the value obtained is smaller.
I thought if I use GetTextExtentPoint32() on a window device context it will return exactly the right size. What am I doing wrong?