如何在C#中制作字体组合框? [英] How to make a Font Combobox in C#?

查看:108
本文介绍了如何在C#中制作字体组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make a combobox in a c# .NET 4.5 Windows Forms application (note: not WPF) that displays all the truetype installed font on the system and that every font is formatted with the font it's representing ("Times" formatted with Times, "Arial" formatted with arial and so on).

该怎么做?

推荐答案

使用ComboBox.DrawItem事件处理程序.

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

这篇关于如何在C#中制作字体组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆