I would like to determine real font line height based on font taken from system. The font I use is system icon font.
Here is my code so far.
LOGFONTW lf;
ZeroMemory(&lf, sizeof(lf));
// Get icon font size from the system
if (SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0))
{
int H = 7;
{
// Create TBitmap and TFont
boost::scoped_ptr<Graphics::TBitmap> bmp(new Graphics::TBitmap);
boost::scoped_ptr<TFont> fnt(new TFont);
// Assign font from the system
fnt->Name = lf.lfFaceName;
fnt->Height = lf.lfHeight;
bmp->Canvas->Font->Assign(fnt.get());
// Calc height (returns 13 for default font size, 96 DPI but should be more like 18)
H = bmp->Canvas->TextHeight("Wq");
}
VST->DefaultNodeHeight = H;
VST->Font->Name = lf.lfFaceName;
VST->Font->Height = lf.lfHeight;
}
Now here is the problem. The above calculates text height which is 13 pixels for default font size at 96 DPI. But it should be 18 actually for nice pitch and line spacing. The difference increases as DPI is enlarged or font size is enlarged. If VirtualTreeView DefaultNoteHeight is set to 13 pixels it all looks very tight and lines are too close to each other.
What I need to know is actually line spacing as explained here: http://msdn.microsoft.com/en-us/library/xwf9s90b%28v=VS.71%29.aspx
Please explain how do I extract line spacing from the given font from the system.
I believe GetTextMetrics
holds the key but I just miss small piece of the puzzle to put it all together how to use it together with the above.
Examples in Delphi also welcome doesn't have to be in C++ Builder.
Update:
I've revised the formula a bit by adding:
H = bmp->Canvas->TextHeight("Wq");
// Take care of smaller heights to make them minimum 18 pixels
H = (H < 18)? 18 : H;
Seems to work OK for all font sizes I've tested with (for larger fonts it is a bit more tight but that's OK).