创建字体对话框。如何显示每种字体及其设计?

发布于 2024-10-10 04:52:39 字数 398 浏览 2 评论 0 原文

这是我的代码:

private void DialogFont_Load(object sender, EventArgs e)
{
    LoadInstalledFonts();
}

private void LoadInstalledFonts()
{
    var fontCollection = new System.Drawing.Text.InstalledFontCollection();
    foreach (var font in fontCollection.Families)
    {
        lstFonts.Items.Add(font.Name);
    }
}

如何使用自己的设计显示每种字体,有点像字体的预览?我正在使用 ListBox 控件列出字体。

Here's my code:

private void DialogFont_Load(object sender, EventArgs e)
{
    LoadInstalledFonts();
}

private void LoadInstalledFonts()
{
    var fontCollection = new System.Drawing.Text.InstalledFontCollection();
    foreach (var font in fontCollection.Families)
    {
        lstFonts.Items.Add(font.Name);
    }
}

How can I display each font using its own design, sort of like a preview of the fonts? I'm using the ListBox control to list the fonts.

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

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

发布评论

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

评论(4

瞳孔里扚悲伤 2024-10-17 04:52:39

您可以在 WPF 中轻松完成此操作。

XAML 看起来像:

<ComboBox Width="100" Height="30" x:Name="FontSelector">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" FontFamily="{Binding}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

和 C# 代码隐藏:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    FontSelector.ItemsSource = Fonts.SystemFontFamilies;
}

您还可以在 CodeProject 上查看这篇文章,其中演示了(在 Winforms 示例中)如何使用您想要的字体填充列表框: http://www.codeproject.com/KB/selection/FontListBoxAndCombo.aspx

You can do it easily in WPF.

The XAML would look like:

<ComboBox Width="100" Height="30" x:Name="FontSelector">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" FontFamily="{Binding}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

And C# codebehind:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    FontSelector.ItemsSource = Fonts.SystemFontFamilies;
}

You could also check this article over on CodeProject which walks through (in a Winforms example) how to populate a list box with fonts like you want: http://www.codeproject.com/KB/selection/FontListBoxAndCombo.aspx

暖阳 2024-10-17 04:52:39

也许使用 ListView 而不是 ListBoxListViewItem type 有一个可以使用的 Font 属性。我不知道 ListBox 有任何特殊的每项格式化功能。


更新:如果您仍在处理此问题,这是对我有用的一些代码片段(这不会按原样编译,因为它只是来自较大用户控件的剪辑;我相信您可以弄清楚但

private void PopulateListView(IEnumerable<FontFamily> fontFamilies)
{
    try
    {
        m_listView.BeginUpdate();

        float fontSize = m_listView.Font.Size;
        Color foreColor = m_listView.ForeColor;
        Color backColor = m_listView.BackColor;
        string sampleText = m_sampleText;

        foreach (FontFamily fontFamily in fontFamilies)
        {
            var listViewItem = new ListViewItem(fontFamily.Name)
            {
                UseItemStyleForSubItems = false
            };

            var sampleSubItem = new ListViewItem.ListViewSubItem(listViewItem, sampleText, foreColor, backColor, new Font(fontFamily, fontSize));

            listViewItem.SubItems.Add(sampleSubItem);

            m_listView.Items.Add(listViewItem);
        }
    }
    finally
    {
        m_listView.EndUpdate();
    }
}

结果是这样的:

FontListView control

Maybe go with a ListView instead of a ListBox? The ListViewItem type has a Font property you could use. I'm not aware of any special per-item formatting capabilities of ListBox.


Update: In case you're still working on this, here's a snippet of some code that worked for me (this won't compile as-is as it's just a clip from a larger user control; I'm sure you can figure out what goes where though):

private void PopulateListView(IEnumerable<FontFamily> fontFamilies)
{
    try
    {
        m_listView.BeginUpdate();

        float fontSize = m_listView.Font.Size;
        Color foreColor = m_listView.ForeColor;
        Color backColor = m_listView.BackColor;
        string sampleText = m_sampleText;

        foreach (FontFamily fontFamily in fontFamilies)
        {
            var listViewItem = new ListViewItem(fontFamily.Name)
            {
                UseItemStyleForSubItems = false
            };

            var sampleSubItem = new ListViewItem.ListViewSubItem(listViewItem, sampleText, foreColor, backColor, new Font(fontFamily, fontSize));

            listViewItem.SubItems.Add(sampleSubItem);

            m_listView.Items.Add(listViewItem);
        }
    }
    finally
    {
        m_listView.EndUpdate();
    }
}

Here's what the result looks like:

FontListView control

八巷 2024-10-17 04:52:39

首先,我想确保您了解 FontDialog 控件,并且您有意要创建自定义字体对话框。如果您不知道它,那么也许您可以看一下它并确保它不符合您的需求,然后再尝试创建您自己的。以下堆栈溢出问题向您展示了如何确保它填充了所有内容设备字体而不仅仅是 TrueType 字体。

现在,如果您仍想创建自己的字体,则以下是一个简化的解决方案:

  1. 将标签添加到字体对话框中,并将其文本设置为您希望用户看到的字体示例。类似 AabBcC 的内容,甚至可以是随机句子。

  2. 您可以在列表框的 SelectedIndexChanged 事件中设置标签的字体。这实际上会更改示例文本以匹配您指定的字体。下面是一个简单的示例:

请注意,如果您希望用户指定他们希望以其他字体查看的文本,您也可以使用文本框。此外,某些字体(例如 Andy 和 Aharomi)会抛出 ArgumentException,表明该字体不支持常规样式,因此捕获此异常类型是明智的。

private void lstFonts_SelectedIndexChanged(object sender, EventArgs e)
{
    lblSample.Font = new Font(lstFonts.SelectedItem.ToString(), 12);
}

First thing, I want to make sure that you are aware of the FontDialog control, and you are purposely wanting to create a custom Font Dialog. If you weren't aware of it, then maybe you can take a look at it and make sure that it doesn't fit your needs before trying to create your own. And the following Stack Overflow question shows you how to make sure it is populated with all the device fonts and not just TrueType fonts.

Now, if you still want to create your own, then the following is a simplified solution:

  1. Add a Label to your Font Dialog and set its text to whatever you want the user to see as a sample of the font. Something like AabBcC, or it could even be a random sentence.

  2. You can set the Font of the label in the SelectedIndexChanged event of your ListBox. This in effect changes the sample text to match the font you specify. The following is a simple example:

Note that you can also use a Textbox if you want your user to specify the text that they want to see in another font. Also, some fonts like Andy and Aharomi throw an ArgumentException stating that the Font doesn't support a regular style, so it would be wise to catch this exception type.

private void lstFonts_SelectedIndexChanged(object sender, EventArgs e)
{
    lblSample.Font = new Font(lstFonts.SelectedItem.ToString(), 12);
}
梦境 2024-10-17 04:52:39

使用列表框,我认为你需要进行所有者绘制。绘制每个列表项时,您需要选择该项目的字体。

Using a ListBox, I would think you'd need to do owner-draw. When drawing each list item, you'd need to select the font for that item.

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