如何在delphi中获取字体大小

Bha*_*rat 2 delphi fonts font-size delphi-2007

我正在寻找一个函数,它应该将参数作为字体名称,样本字符,宽度,样本字符的高度,并应返回字体大小.

它必须如下所示:

    GetFontSize(<Font Name>, <Sample Character>, <Sample Character Width>,
                <Sample Character Height>)
Run Code Online (Sandbox Code Playgroud)

必须返回字体大小,

在delphi中这可能吗?

Mar*_*der 5

You may want to take a look at this page that discusses font size and points.

It uses the following to convert between the points and pixel size:

Arial 12pt at 96 dpi:

                      font size in points             12
font size in pixels = ------------------- × 96 dpi = ---- × 96 = 16 pixels
                      72 points per inch              72

You could use that for your conversion. You just need to be aware of possible differences in your screen dpi as Blorgbeard stated. Points are usually used to describe the size on paper. You need to decide how you want to map that to the screen. For example many programs will allow you to set a zoom level. That does not change the printed size on paper but does affect the pixel height on the screen.

Depending on what you are trying to accomplish you may need to get the actual sizes of the font. If you are trying to find out exactly how the font is put together take a look at Obtaining Font Metrics The code on that page uses the .Net libraries but it has a good explanation. If you want to get the Text Metrics in Delphi you would use the GetTextMetrics function.

正如Rob在评论中所述,您可以通过将Font.Size属性设置为负高度(以像素为单位)来绘制特定高度的文本.然后,Windows将在该高度绘制文本.在这种情况下,您不需要原型函数中的特定字母或宽度.对于给定的字体大小,每个字母将具有不同的高度和宽度.IE大写字母将比小写字母高,而像"W"的字母将比像"i"这样的字母更宽.