delphi中如何获取字体大小
我正在寻找一个函数,该函数应采用字体名称、样本字符、样本字符的宽度、高度等参数,并应返回字体大小。
它必须看起来像这样:
GetFontSize(<Font Name>, <Sample Character>, <Sample Character Width>,
<Sample Character Height>)
必须返回字体大小,
这在delphi中可能吗?
I am looking for a function which should take parameters as Font name, sample character, width, height of the sample character and should return Font Size.
It must look like this:
GetFontSize(<Font Name>, <Sample Character>, <Sample Character Width>,
<Sample Character Height>)
which must return the font size,
Is this possible in delphi?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想查看此页面,其中讨论了 字体大小和点。
它使用以下内容在点和像素大小之间进行转换:
您可以使用它进行转换。正如Blorgbeard 所说,您只需要注意屏幕 dpi 可能存在的差异。点通常用来描述纸上的大小。您需要决定如何将其映射到屏幕。例如,许多程序将允许您设置缩放级别。这不会改变纸张上的打印尺寸,但会影响屏幕上的像素高度。
根据您想要完成的任务,您可能需要获取字体的实际大小。如果您想确切地了解字体是如何组合在一起的,请查看 获取字体规格 该页面上的代码使用 .Net 库,但它有一个很好的解释。如果您想在 Delphi 中获取文本指标,您可以使用 GetTextMetrics 函数。
正如 Rob 在他的评论中所述,您可以通过将 Font.Size 属性设置为负高度(以像素为单位)来绘制特定高度的文本。然后 Windows 将在该高度绘制文本。在这种情况下,您不需要原型函数中那样的特定字母或宽度。对于给定的字体大小,每个字母将具有不同的高度和宽度。 IE 大写字母将比小写字母高,“W”等字母将比“i”等字母宽。
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:
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.
As Rob stated in his comment you can draw text at a specific height by setting the Font.Size property to the negative height in pixels. Windows will then draw the text out at that height. In that case you don't need a specific letter or the width as you have in your prototype function. For a given font size each letter will have a different height and width. I.E. capital letters will be taller than lower case letters and letters like "W" will be wider than letters like "i".
我只知道相反的方法 - 即从点大小获取像素大小。
我能想到的唯一方法是设置一个 TCanvas,并在其上重复调用 GetTextExtent,每次更改字体大小,直到获得可接受的接近大小到你想要的。
根据您需要的精确程度,您可以将所需的像素高度转换为点 - 点是 1/72 英寸,您的屏幕可能是每英寸 96 像素(但请检查
GetDeviceCaps
以获得真实的数字)。请参阅此问题。这将使您的字体大小非常接近您想要的像素大小。
I'm only aware of methods to do the opposite - that is, to get pixel sizes from point sizes.
The only way I can think of is to set up a
TCanvas
, and repeatedly callGetTextExtent
on it, changing the font size each time, until you get a size that's acceptably close to what you want.Depending on how accurate you need this, you could just convert your desired height in pixels to points - a point is 1/72 of an inch, and your screen is probably 96 pixels per inch (but check
GetDeviceCaps
to get the real figure). See this question.That would get you a font size pretty close to the pixel size you want.