alex555
alex555

Reputation: 1786

Calculate the font height from its size in C++?

I am trying to verify the dependency between the CFont height and size on an example:

int main(int argc, char* argv[])
{
  int myVariableFontHeight = 90;

  CFont * font = new CFont();

  LOGFONT lf;
  memset(&lf,0,sizeof(LOGFONT));
  lf.lfHeight = myVariableFontHeight;
  lf.lfWeight =FW_BOLD;
  lf.lfCharSet = 1;
  _tcscpy_s(lf.lfFaceName , "Arial Unicode MS");    
  font->CreatePointFontIndirect(&lf);

  font->GetLogFont(&lf);
  int fontHeight = lf.lfHeight;

  HWND console = GetConsoleWindow();
  HDC dc = GetDC(console);

  int nFontSize = -::MulDiv( lf.lfHeight, 72, ::GetDeviceCaps( dc, LOGPIXELSY ) );

  delete font;

  return 0;
}

And the result is always nFontSize = myVariableFontHeight/10. What is this factor 10? Where comes it from? Can I calculate the font height from a given size?

Thanks

Upvotes: 0

Views: 1646

Answers (1)

Joseph Willcoxson
Joseph Willcoxson

Reputation: 6040

It's in the MFC souce code. It's in the documentation. The very first line of the online documentation for CFont::CreatePointFontIndirect states:

This function is the same as CreateFontIndirect except that the lfHeight member of the LOGFONT is interpreted in tenths of a point rather than device units.

So, if you want to create a 10 pt font, you set lf.lfHeight to 100.

Upvotes: 1

Related Questions