将所有窗口字体读入ComboBox的最佳方法是什么? 基本上,我试过这样做:
等于微软的Word
我可以:
string[] fonts = Directory.GetFiles(@"C:windowsfonts");
并显示每个文件到comboBox ,但这是正确的? 没有这样做的组件?
.NET中的窗口句柄可以改变它的值吗?
如何发现哪个图像文件是Windows上的当前桌面背景?
Environment.FailFast在C#应用程序
提前致谢。
CredUIPromptForCredentials强制手动select用户名
如何从C#closures,Process.Start(“关机”)不能在Windows XP中工作
在MonoDevelop和Visual Studio上使用生成后脚本生成项目的最佳方法是什么?
使用Windows C#应用程序将应用程序限制为32位体系结构
在windows应用程序中以编程方式在treeview中select一个节点
尝试这个:
using System.Drawing.Text; InstalledFontCollection myFonts = new InstalledFontCollection(); foreach (FontFamily ff in myFonts.Families) comboBox1.Items.Add(ff.Name); }
看看EnumFontFamiliesEx和EnumFonts 。
更好的是InstalledFontCollection 。 我没有意识到它存在。
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); } } }
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,10)).Width; if(both) { int h1 = (int)g.MeasureString(samplestr,10)).Height; int h2 = (int)g.MeasureString(Items[e.Index].ToString(),new Font("Arial",10)).Height; int w1 = (int)g.MeasureString(samplestr,10)).Width; int w2 = (int)g.MeasureString(Items[e.Index].ToString(),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,e.Bounds.X+w+ttimg.Width*2,e.Bounds.Y); } else { e.Graphics.FillRectangle(new SolidBrush(SystemColors.Highlight),new SolidBrush(SystemColors.HighlightText),e.Bounds.Y); } } else { if((e.State & DrawItemState.Focus)==0) { e.Graphics.FillRectangle(new SolidBrush(SystemColors.Window),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; } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。