如何在C#中制作Font Combobox?

时间:2017-09-04 12:34:15

标签: c# .net winforms fonts combobox

我想在ac#.NET 4.5 Windows窗体应用程序(注意:不是WPF)中制作一个组合框,它显示系统上所有的truetype安装字体,并且每个字体都使用它所代表的字体格式化(“Times”格式化与Times,“Arial”格式化arial等等。)

怎么做?

3 个答案:

答案 0 :(得分:2)

使用ComboBox.DrawItem eventhandler。

public YourForm()
{
    InitializeComponent();

    ComboBoxFonts.DrawItem += ComboBoxFonts_DrawItem;           
    ComboBoxFonts.DataSource = System.Drawing.FontFamily.Families.ToList();
}

private void ComboBoxFonts_DrawItem(object sender, DrawItemEventArgs e)
{
    var comboBox = (ComboBox)sender;
    var fontFamily = (FontFamily)comboBox.Items[e.Index];
    var font = new Font(fontFamily, comboBox.Font.SizeInPoints);

    e.DrawBackground();
    e.Graphics.DrawString(font.Name, font, Brushes.Black, e.Bounds.X, e.Bounds.Y);
}

要使用ComboBox.DrawItem,您需要设置ComboBox.DrawMode = DrawMode.OwnerDrawFixed

答案 1 :(得分:0)

您可以使用System.Drawing.FontFamily.Families,类似这样的

List<string> fonts = new List<string>();

foreach (FontFamily font in System.Drawing.FontFamily.Families)
{
    fonts.Add(font.Name);
}

.................. your logic

答案 2 :(得分:0)

您可以按照几年前发布的项目https://www.codeproject.com/Articles/318174/Creating-a-WYSIWYG-font-ComboBox-using-Csharp

来实现这一目标

但是,您可以按照Narek Noreyan的步骤添加更多内容,使用富文本框,在从组合框中选择字体系列后,将更改其文本字体。

此处有一些代码段,

using System.Drawing.Text;
void mainformload(object sender, EventArgs s)
{
    InstalledFontCollection inf=new      InstalledFontCollection();
    foreach(Font family font in inf.Families)
        combobox.Items.Add(font.Name); //filling the font name
    //get the font name of the rich text box text
    combobox.Text=this.richtextbox.Font.Name.ToString();
    }
void comboboxselectionchanged(object sender, EventArgs e)
{
    richtextbox.Font=new Font(combobox.text, richtextbox.Font.Size);
}