【问题标题】:reading windows font阅读窗口字体
【发布时间】:2011-10-26 01:14:23
【问题描述】:

将所有 Windows 字体读入 ComboBox 的最佳方法是什么? 基本上,我试过这样做:

等于 Microsoft Word

我能做到:

 string[] fonts = Directory.GetFiles(@"C:\windows\fonts");

并将每个文件显示到 ComboBox 中,但这是正确的吗?没有一个组件可以完成这项工作吗?

提前致谢。

【问题讨论】:

    标签: c# .net windows fonts filesystems


    【解决方案1】:

    看看EnumFontFamiliesExEnumFonts

    InstalledFontCollection 更好。我没有意识到它的存在。

    【讨论】:

      【解决方案2】:

      试试这个:

      using System.Drawing.Text;
      
      InstalledFontCollection myFonts = new InstalledFontCollection();
      foreach (FontFamily ff in myFonts.Families)
        comboBox1.Items.Add(ff.Name);
      }
      

      【讨论】:

      • InstalledFontCollectionIDisposable。在 using 语句中包含用法。
      【解决方案3】:

      Font 文件夹中有一些 .FON 字体。 C# 不能使用那些 BITMAP 字体——你不能使用 drawstring 函数显示这些字体。要获得所有可以显示的字体,只需使用我从这里找到的代码: http://www.java2s.com/Code/CSharp/GUI-Windows-Form/Getallsysteminstalledfont.htm

         using System;
         using System.Drawing;
         using System.Drawing.Text;
         using System.Collections;
         using System.ComponentModel;
         using System.Windows.Forms;
         using System.Data;        
      
      
         public class Test{
              static void Main() 
              {
                InstalledFontCollection fonts = new InstalledFontCollection();
                for(int i = 0; i < fonts.Families.Length; i++)
                {
                  Console.WriteLine(fonts.Families[i].Name);
                }
              }
            }
      

      【讨论】:

        【解决方案4】:

        您可以创建一个自定义控件来自动加载 Windows 安装的字体:

        namespace MyProject
        {   
            public class FontComboBox : ComboBox
            {
                public FontComboBox()
                {               
                    MaxDropDownItems = 20;
                    IntegralHeight = false;
                    Sorted = false;
                    DropDownStyle = ComboBoxStyle.DropDownList;
                    DrawMode = DrawMode.OwnerDrawVariable;                          
                }
        
                public void Populate(bool b)
                {
                    both = b;
                    foreach (FontFamily ff in FontFamily.Families)
                    {
                        if(ff.IsStyleAvailable(FontStyle.Regular))
                            Items.Add(ff.Name);                                             
                    }           
        
                    if(Items.Count > 0)
                        SelectedIndex=0;
                    //ttimg = new Bitmap(GetType(),"ttfbmp.bmp");
                    ttimg = new Bitmap(Resources.ttfbmp);
                }
        
                protected override void OnMeasureItem(System.Windows.Forms.MeasureItemEventArgs e)
                {   
                    if(e.Index > -1)
                    {
                        int w = 0;
                        string fontstring = Items[e.Index].ToString();                      
                        Graphics g = CreateGraphics();
                        e.ItemHeight = (int)g.MeasureString(fontstring, new Font(fontstring,10)).Height;
                        w = (int)g.MeasureString(fontstring, new Font(fontstring,10)).Width;
                        if(both)
                        {
                            int h1 = (int)g.MeasureString(samplestr, new Font(fontstring,10)).Height;
                            int h2 = (int)g.MeasureString(Items[e.Index].ToString(), new Font("Arial",10)).Height;
                            int w1 = (int)g.MeasureString(samplestr, new Font(fontstring,10)).Width;
                            int w2 = (int)g.MeasureString(Items[e.Index].ToString(), new Font("Arial",10)).Width;
                            if(h1 > h2 )
                                h2 = h1;
                            e.ItemHeight = h2;
                            w = w1 + w2;
                        }
                        w += ttimg.Width*2;
                        if(w > maxwid)
                            maxwid=w;
                        if(e.ItemHeight > 20)
                            e.ItemHeight = 20;
                    }
        
        
        
                    base.OnMeasureItem(e);
                }
        
                protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
                {   
                    if(e.Index > -1)
                    {
                        string fontstring = Items[e.Index].ToString();
                        nfont = new Font(fontstring,10);
                        Font afont = new Font("Arial",10);
        
                        if(both)
                        {
                            Graphics g = CreateGraphics();
                            int w = (int)g.MeasureString(fontstring, afont).Width;
        
                            if((e.State & DrawItemState.Focus)==0)
                            {
                                e.Graphics.FillRectangle(new SolidBrush(SystemColors.Window),
                                    e.Bounds.X+ttimg.Width,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height);
                                e.Graphics.DrawString(fontstring,afont,new SolidBrush(SystemColors.WindowText),
                                    e.Bounds.X+ttimg.Width*2,e.Bounds.Y);   
                                e.Graphics.DrawString(samplestr,nfont,new SolidBrush(SystemColors.WindowText),
                                    e.Bounds.X+w+ttimg.Width*2,e.Bounds.Y); 
                            }
                            else
                            {
                                e.Graphics.FillRectangle(new SolidBrush(SystemColors.Highlight),
                                    e.Bounds.X+ttimg.Width,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height);
                                e.Graphics.DrawString(fontstring,afont,new SolidBrush(SystemColors.HighlightText),
                                    e.Bounds.X+ttimg.Width*2,e.Bounds.Y);
                                e.Graphics.DrawString(samplestr,nfont,new SolidBrush(SystemColors.HighlightText),
                                    e.Bounds.X+w+ttimg.Width*2,e.Bounds.Y);
                            }   
                        }
                        else
                        {
        
                            if((e.State & DrawItemState.Focus)==0)
                            {
                                e.Graphics.FillRectangle(new SolidBrush(SystemColors.Window),
                                    e.Bounds.X+ttimg.Width,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height);
                                e.Graphics.DrawString(fontstring,nfont,new SolidBrush(SystemColors.WindowText),
                                    e.Bounds.X+ttimg.Width*2,e.Bounds.Y);
        
                            }
                            else
                            {
                                e.Graphics.FillRectangle(new SolidBrush(SystemColors.Highlight),
                                    e.Bounds.X+ttimg.Width,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height);
                                e.Graphics.DrawString(fontstring,nfont,new SolidBrush(SystemColors.HighlightText),
                                    e.Bounds.X+ttimg.Width*2,e.Bounds.Y);
                            }           
        
                        }
        
                        e.Graphics.DrawImage(ttimg, new Point(e.Bounds.X, e.Bounds.Y)); 
                    }
                    base.OnDrawItem(e);
                }
        
                Font nfont;
                bool both = false;
                int maxwid = 0;
                string samplestr = " - " + Resources.AppTitle;
                Image ttimg;
        
                protected override void OnDropDown(System.EventArgs e)
                {
                    this.DropDownWidth = maxwid+30;
                }       
        
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-14
          • 1970-01-01
          • 1970-01-01
          • 2012-05-31
          • 2022-07-15
          • 1970-01-01
          • 2017-12-05
          • 2022-01-10
          相关资源
          最近更新 更多