【发布时间】:2011-09-02 14:45:48
【问题描述】:
如果您输入多行TextBox,我该怎么做:
abcde
ABCDE
所以大的E直接低于小e。如果它们在同一行,我希望它们垂直排列。
【问题讨论】:
如果您输入多行TextBox,我该怎么做:
abcde
ABCDE
所以大的E直接低于小e。如果它们在同一行,我希望它们垂直排列。
【问题讨论】:
你能把文本框上的字体设置为monospaced吗?
在代码中,保持与默认字体相同的大小:
textBox.Font = new Font(FontFamily.GenericMonospace, textBox.Font.Size);
或者只是在设计器中更改Font 属性。
【讨论】:
您可以使用固定宽度的字体来做到这一点。 Courier 系列字体通常是固定宽度的。
您可以在属性编辑器中为文本框控件设置字体。例如,您可以将 Font 属性设置为 Courier New, 8.25pt。
【讨论】:
有些字体对不同的字符使用不同的字符宽度。在这样的字体中,“m”将比“i”具有更大的宽度。它们被称为proportional字体。这些字体更好看,更容易阅读。
所有字符都具有相同宽度的字体称为等宽字体。它们通常用于源代码,因为它们允许将行 cmets 等功能与代码右侧对齐。
使用等宽字体!
这是我用来获取所有已安装等宽字体列表的代码:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace PE.Rendering {
static class FontHelper {
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
class LOGFONT {
public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string lfFaceName;
}
static bool IsMonospaced(Graphics g, Font f)
{
float w1, w2;
w1 = g.MeasureString("i", f).Width;
w2 = g.MeasureString("W", f).Width;
return w1 == w2;
}
static bool IsSymbolFont(Font font)
{
const byte SYMBOL_FONT = 2;
LOGFONT logicalFont = new LOGFONT();
font.ToLogFont(logicalFont);
return logicalFont.lfCharSet == SYMBOL_FONT;
}
/// <summary>
/// Tells us, if a font is suitable for displaying document.
/// </summary>
/// <remarks>Some symbol fonts do not identify themselves as such.</remarks>
/// <param name="fontName"></param>
/// <returns></returns>
static bool IsSuitableFont(string fontName)
{
return !fontName.StartsWith("ESRI") && !fontName.StartsWith("Oc_");
}
public static List<string> GetMonospacedFontNames()
{
List<string> fontList = new List<string>();
InstalledFontCollection ifc;
ifc = new InstalledFontCollection();
using (Bitmap bmp = new Bitmap(1, 1)) {
using (Graphics g = Graphics.FromImage(bmp)) {
foreach (FontFamily ff in ifc.Families) {
if (ff.IsStyleAvailable(FontStyle.Regular) && ff.IsStyleAvailable(FontStyle.Bold)
&& ff.IsStyleAvailable(FontStyle.Italic) && IsSuitableFont( ff.Name)) {
using (Font f = new Font(ff, 10)) {
if (IsMonospaced(g,f) && !IsSymbolFont(f)) {
fontList.Add(ff.Name);
}
}
}
}
}
}
return fontList;
}
}
}
【讨论】:
尝试使用等宽或固定宽度的字体。
【讨论】: