且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将LogFont高度转换为字体大小

更新时间:2023-11-29 15:03:46

当映射模式是 mm_Text (通常是这样),当 lfHeight 字段为正时,它已经给出了高度。当它是负数时,单位是像素。 用于LogFont的MSDN 为您提供了在它们之间转换的公式:


  lfHeight = -MulDiv(PointSize,GetDeviceCaps(hDC,LOGPIXELSY),72); 


每英寸有72点。 GetDeviceCaps 告诉您给定设备每英寸的像素数。反转公式从点获取像素:

  PointSize:= MulDiv(-lfHeight,72,GetDeviceCaps(hDC,LogPixelsY); 

这里要重要的一点是需要一个设备上下文,字体大小不会独立存在他们出现的媒体的像素高度在屏幕上的像素高度将不同于打印机上字体的像素高度,使用任何画布的句柄属性你正在计划绘图。


I have a LOGFONT structure. Now all i'd like to do is get the associated font size in points from the LOGFONT height.

When the mapping mode is mm_Text (which it usually is), and when the lfHeight field is positive, it already gives the height in points. When it's negative, the units are pixels. MSDN for LogFont gives you the formula to convert between them:

lfHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);

There are 72 points per inch. GetDeviceCaps tells you the number of pixels per inch on a given device. Invert the formula to get pixels from points:

PointSize := MulDiv(-lfHeight, 72, GetDeviceCaps(hDC, LogPixelsY);

The important thing to realize here is that you need a device context. Font sizes don't exist independently of the media they appear on. The pixel height of a font on the screen will be different from the pixel height of a font on a printer. Use the Handle property of whatever canvas you're planning on drawing to.