Reputation: 1880
I'm trying to measure the size of a character and I can't get the right result out. We use DrawText with the DT_CALCRECT flag to measure strings, and that seems to work well enough (to be fair, we haven't actually tested it for pixel perfect measurements).
However I'm now trying to measure a single character, the 'x' symbol in Marlett to draw a close button, but it really doesn't want to tell me the actual size of the character. It tells me the character is 8x8 when it really is a 6x5, and the character then is drawn slightly displaced from where I ask it to be.
Height wise, I'm guessing it's returning the character height for that font (being the same for all characters, same for all fonts). For the width though, it seems to be adding character spacing into it, which it doesn't seem to do with other fonts (tried a couple of characters in Arial). In Marlett all tested characters seem to return the same width.
Is it possible to find out what the extra spacing around a character is going to be, so I can center the 'x' in a box?
Upvotes: 3
Views: 809
Reputation: 47962
Most fonts have a little bit of spacing on either side of the glyphs so that they don't touch when you line them up. You might not expect this in a specialty symbol font like Marlett, but it's there.
You can use GetCharABCWidths
to get an approximate measurement of these spaces (sometimes called "advance widths"). The A
value is typically the gap before the glyph; the B
value is the width of the glyph itself, and the C
value is the space after the glyph. These may not be precise measurements, though, as they're more about making things look right. A round glyph, like O
might spill into the surrounding space, and the A
and C
value can actually be negative.
Well made fonts also contain kerning adjustments so that the spaces between certain pairs of characters look balanced, but I wouldn't expect a specialty symbol font like Marlett to have these.
For your specific need, you could test to see if the B
value is the precise value of the glyph and whether the A
value is the appropriate offset.
Marlett was a cool hack for dealing with rendering of nice controls at a variety of resolutions; it certainly worked better than the old OEM images you can still get from LoadImage
. But modern UI generally relies on code (like Themes as IIinspectable suggested) and/or custom bitmaps (as Hans Passant suggested). Your best bet is probably to pursue one of those techniques for your stated purpose. But I felt the question you actually asked deserved an answer.
Upvotes: 2