Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以所有可用字体绘制文本_C#_Fonts - Fatal编程技术网

C# 以所有可用字体绘制文本

C# 以所有可用字体绘制文本,c#,fonts,C#,Fonts,我有简单的列表框,我想用所有可用字体绘制简单的文本,以便选择我喜欢的字体: ListBox lb; foreach (FontFamily font in System.Drawing.FontFamily.Families) { Font f = new Font(font, 15); lbFonts.Items.Add(f.Name + ": " + "11"); } 目前我的列表框都是相同的字体。我相信你真正的问题是: 如何枚举.NET中所有已安装的字体 对它。它的核心用

我有简单的
列表框
,我想用所有可用字体绘制简单的
文本
,以便选择我喜欢的字体:

ListBox lb;
foreach (FontFamily font in System.Drawing.FontFamily.Families)
{
    Font f = new Font(font, 15);
    lbFonts.Items.Add(f.Name + ": " + "11");
}

目前我的
列表框
都是相同的字体。

我相信你真正的问题是:

如何枚举.NET中所有已安装的字体

对它。它的核心用于获取所有字体系列:

InstalledFontCollection installedFontCollection = new InstalledFontCollection();

// Get the array of FontFamily objects.
FontFamily[] fontFamilies = installedFontCollection.Families;

重复此操作以填充列表框。请注意,从UI的角度来看,这是一种可怕的方法,没有某种类型。此外,许多用户喜欢看到该字体的示例,但使用该字体的名称作为示例会损害用户界面,因为有许多象形字体(Zapf Dingbats、Wingdings等)无法以有用的方式呈现自己的名称。

您只需将字体本身添加到列表中即可(或者创建一个类,为字体和大小添加一个属性,并覆盖ToString()-方法以获取示例中的文本)

这样您就可以使用该方法

为初始化添加以下行:

this.listBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(listBox1_DrawItem);

listBox1.ItemHeight = 20;
InstalledFontCollection installedFontCollection = new InstalledFontCollection();

foreach (FontFamily fontFamily in installedFontCollection.Families)
{
    FontListItem item = new FontListItem(fontFamily, 10);
    listBox1.Items.Add(item);
}
以及DrawItem方法:

void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();

    FontListItem item = (FontListItem)listBox1.Items[e.Index];

    e.Graphics.DrawString(item.ToString(), item.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
}
用于将字体添加到列表框的示例类:

private class FontListItem
{
    public int Size { get; private set; }
    public Font Font { get; private set; }

    public FontListItem(FontFamily family, int size)
    {
        Font = new Font(family, size);
        Size = size;
    }

    public override string ToString()
    {
        if(Font != null)
            return Font.Name + ": " + Size;
        return "[none]: " + Size;
    }
}
您应该得到类似以下内容的输出:


这是因为你没有使用字体本身,只有它们的名称……我可以举个例子来说明如何修复它吗?可能是重复的,如果我只想为每种字体插入我自己的文本?@roz我更新了答案,它现在是使用FontListItem类实现的。