从C 中的大小计算字体高度

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

本文关键字:计算 字体 高度      更新时间:2023-10-16

我试图在示例上验证CFont高度和大小之间的依赖性:

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;
}

,结果始终是nFontSize = myVariableFontHeight/10。这是什么因素10?从哪里来?我可以从给定尺寸计算字体高度吗?

谢谢

它在MFC Souce代码中。它在文档中。CFONT :: CreatePointFontirect状态的在线文档的第一行:

此功能与CreateFontinirect相同 LogFont的LFHEIGHT成员在十分之一的解释 而不是设备单位。

所以,如果要创建10 pt字体,则将lf.lfheight设置为100。