如何获得指定字体大小的固定宽度字体的宽度? [自己弄清楚了,最终] [英] How to get the width of a fixed-width font, at a specified font size? [Figured it out myself, eventually]
问题描述
与通常的字体(所有字符的宽度可能有所不同)不同,我们在编程时使用的固定宽度字体的所有字符都具有相同的宽度(当然,这取决于字体大小,或者取决于某些dpi)设置).
我尝试以编程方式或以其他方式获得此宽度,但无济于事.
Graphics.MeasureString
给出完全不可靠且几乎随机的结果,Font.ToLogFont
方法返回没有字段且被卡住的对象.
有人可以帮我吗?
谢谢
程序员Toli.
Unlike an usual font, in which all characters may have a different width, the fixed-width fonts we all use while programming have all character of the same width (depending of the font size, of course and probably of some dpi whatever settings).
I tried to programatically or otherwise get this width, but to no avail.
Graphics.MeasureString
gives completely unreliable and almost random results, Font.ToLogFont
method returns an object with no fields and I''m stuck.
Can someone help me?
Thank you,
Toli, fellow programmer.
推荐答案
在C ++/Win32中,您将在设备上下文上使用GetTextMetrics()
调用.我认为C#/.NET中必须有一些等效项.尝试在MSDN上进行搜索.
In C++/Win32 you would use the GetTextMetrics()
call on the device context. I assume there must be some equivalent in C#/.NET. Try a search on MSDN.
有一个名为AverageCharacterWidth
的属性.实例化字体并将字符串分配给使用该字体的资源后,您才能获取文本指标.完成后,您应该能够从图形上下文中获取各种整洁的字体信息.
There is a property called AverageCharacterWidth
. You can only get text metrics after you''ve instantiated the font and assigned a string to a resource that uses that font. Once that''s done, you should be able to get all kinds of neat font info from the graphics context.
public enum Unit : int
{
Pixels = 0,
Inches = 100, // 1 inch
Centimeters = 254, // 2.54 cm
Millimeters = 2540, // 25.4 mm
Points = 7200, // 72 points
HalfPoints = 14400, // used in rtf
QuarterPoints = 28800, // used in rtf
DocumentUnits = 30000, // 300 document units
Twips = 144000, // used in rtf (20 twips = 1 point)
}
public static float GetCharWidth(string MonospacedFontName,
float RequestedFontSizeInPoints, Unit CharWidthUnit)
{
int PointsPerInch = 72;
int UnitFactor = 100;
Bitmap B = new Bitmap(16, 16); // Whatever, we only need a Graphics
Graphics G = Graphics.FromImage(B);
float PixelsPerInch = G.DpiX;
// Why are we doing this?
// Well, when we want to create a font with a certain size,
// the system finds the closest possible value.
// That's why in the Visual Studio designer, for example,
// if you set Font.Size = 10, it actually becomes 9.75
float GeneratedFontSizeInPoints = (int)(RequestedFontSizeInPoints *
PixelsPerInch) / PointsPerInch * PointsPerInch / PixelsPerInch;
Font F = new Font(MonospacedFontName, GeneratedFontSizeInPoints,
FontStyle.Regular, GraphicsUnit.Point);
// NoPadding is mandatory
int WidthInPixels = TextRenderer.MeasureText(G, " ", F,
Size.Empty, TextFormatFlags.NoPadding).Width;
F.Dispose();
G.Dispose();
B.Dispose(); // Clean up everything...
// And finally, convert to the desired unit
return (CharWidthUnit == Unit.Pixels) ? WidthInPixels :
WidthInPixels / PixelsPerInch * (int)CharWidthUnit / UnitFactor;
}
这篇关于如何获得指定字体大小的固定宽度字体的宽度? [自己弄清楚了,最终]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!