【问题标题】:C# .NET multiline TextBox with same-width characters具有相同宽度字符的 C# .NET 多行文本框
【发布时间】:2011-09-02 14:45:48
【问题描述】:

如果您输入多行TextBox,我该怎么做:

abcde

ABCDE

所以大的E直接低于小e。如果它们在同一行,我希望它们垂直排列。

【问题讨论】:

    标签: c# .net winforms textbox


    【解决方案1】:

    你能把文本框上的字体设置为monospaced吗?

    在代码中,保持与默认字体相同的大小:

    textBox.Font = new Font(FontFamily.GenericMonospace, textBox.Font.Size);
    

    或者只是在设计器中更改Font 属性。

    【讨论】:

    • 学了一个新词,有哪些等宽字体?
    • 使用像 consolas 这样的字体是否安全,因为我认为是我自己安装的,还是所有字体都是 VS 附带的 .net 框架?
    • @Mark:我似乎记得 Consolas 是随 Office 提供的……我不记得如果您指定的字体在部署的系统上不存在会发生什么。你的用户是谁?
    • 为吉他和弦和标签制作应用程序,通用的似乎很好,但有没有办法将字体与应用程序打包
    • @Mark:我怀疑这将是很多工作 - 你需要调查许可等。如果我是你,我会坚持使用 Courier New :)
    【解决方案2】:

    您可以使用固定宽度的字体来做到这一点。 Courier 系列字体通常是固定宽度的。

    您可以在属性编辑器中为文本框控件设置字体。例如,您可以将 Font 属性设置为 Courier New, 8.25pt

    【讨论】:

      【解决方案3】:

      有些字体对不同的字符使用不同的字符宽度。在这样的字体中,“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;
              }
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        尝试使用等宽或固定宽度的字体。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多