使用可用字体列表填充组合框

发布于 2024-09-13 09:16:23 字数 29 浏览 5 评论 0原文

如何使用系统中所有可用字体的列表填充组合框?

How can I fill a combo-box with a list of all the available fonts in the system?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

流星番茄 2024-09-20 09:16:24

您可以使用 System.Drawing.FontFamily.Families 来获取可用的字体。

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

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

// add the fonts to your ComboBox here

You can use System.Drawing.FontFamily.Families to get the available fonts.

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

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

// add the fonts to your ComboBox here
秋叶绚丽 2024-09-20 09:16:24

不知道为什么我们需要在这里foreach

IList<string> fontNames = FontFamily.Families.Select(f => f.Name).ToList();

Not sure why we need to foreach here.

IList<string> fontNames = FontFamily.Families.Select(f => f.Name).ToList();
晚风撩人 2024-09-20 09:16:24

使用已安装的字体集合类:

http://msdn。 microsoft.com/en-us/library/system.drawing.text.installedfontcollection.aspx

这是扎克·约翰逊回答的替代且等效的方法。

List<string> fonts = new List<string>();
InstalledFontCollection installedFonts = new InstalledFontCollection();          
foreach (FontFamily font in installedFonts.Families)
{               
    fonts.Add(font.Name);
}

Use Installed Font Collection class:

http://msdn.microsoft.com/en-us/library/system.drawing.text.installedfontcollection.aspx

This is alternative and equivalent approach to answer from Zach Johnson.

List<string> fonts = new List<string>();
InstalledFontCollection installedFonts = new InstalledFontCollection();          
foreach (FontFamily font in installedFonts.Families)
{               
    fonts.Add(font.Name);
}
兔姬 2024-09-20 09:16:24

这是最简单的方法。它包括两个组合框 1 用于字体名称,一个用于字体大小

 public FontFamily[] Families { get; }


 private void Form1_Load(object sender, EventArgs e)
    {

        foreach (FontFamily oneFontFamily in FontFamily.Families)
        {
            comboBox1.Items.Add(oneFontFamily.Name);
        }

        comboBox1.Text = this.richTextBox1.Font.Name.ToString();
        comboBox2.Text = this.richTextBox1.Font.Size.ToString();

        richTextBox1.Focus();

    }

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {

         float size = Convert.ToSingle(((ComboBox)sender).Text);

        richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, size);
    }

This is the easy way to do it. It includes two comboboxes 1 for the font name and one for the font size

 public FontFamily[] Families { get; }


 private void Form1_Load(object sender, EventArgs e)
    {

        foreach (FontFamily oneFontFamily in FontFamily.Families)
        {
            comboBox1.Items.Add(oneFontFamily.Name);
        }

        comboBox1.Text = this.richTextBox1.Font.Name.ToString();
        comboBox2.Text = this.richTextBox1.Font.Size.ToString();

        richTextBox1.Focus();

    }

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {

         float size = Convert.ToSingle(((ComboBox)sender).Text);

        richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, size);
    }
我做我的改变 2024-09-20 09:16:24

请记住所有内容都来自“System.Drawing”

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

Please keep in mind all will come from "System.Drawing"

foreach (System.Drawing.FontFamily font in System.Drawing.FontFamily.Families)
{
    comboBox1.Items.Add(font.Name);
}
吃颗糖壮壮胆 2024-09-20 09:16:24
ComboBox1.ItemsSource = new InstalledFontCollection().Families;

以及第一次选择的项目:

private void Combo1_Loaded(object sender, RoutedEventArgs e)
{
    ComboBox1.Text = "Tahoma";
}
ComboBox1.ItemsSource = new InstalledFontCollection().Families;

and for the first time selected item:

private void Combo1_Loaded(object sender, RoutedEventArgs e)
{
    ComboBox1.Text = "Tahoma";
}
梦途 2024-09-20 09:16:24

你可以像这样绑定:

ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"

You can just bind like this:

ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文