设计自定义字体对话框/选择器对C#,过滤掉非True Type字体 [英] Designing a Custom Font Dialog/Selector for C# that filters out non true type fonts

查看:611
本文介绍了设计自定义字体对话框/选择器对C#,过滤掉非True Type字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于内置字体对话框返回一个不是真正的Type字体异常上选择非True Type字体,我试图用字体家庭其过滤掉非True Type字体创建一个自定义字体对话框。

Since the inbuilt Font Dialog returns a 'Not a True Type Font' Exception on selecting a Non True Type Font, I'm trying to create a Custom Font Dialog using Font-families which filter out non true type fonts.

控件是可以正常使用,但我需要一个大小和样式选择此对话框。我张贴当前code。请帮我补充一个大小和样式选择了这一点。它也可能是对你有用。

The Control is working perfectly but I need a size and style selectors for this dialog. I'm posting the current code. Please help me add a size and a style selector to this. It could also be useful to you.

public class FontListBox : ListBox
{
    private List<Font> _fonts = new List<Font>();
    private Brush _foreBrush;

    public FontListBox()
    {
        DrawMode = DrawMode.OwnerDrawFixed;
        ItemHeight = 20;
        foreach (FontFamily ff in FontFamily.Families)
        {
            // determine the first available style, as all fonts don't support all styles
            FontStyle? availableStyle = null;
            foreach (FontStyle style in Enum.GetValues(typeof(FontStyle)))
            {
                if (ff.IsStyleAvailable(style))
                {
                    availableStyle = style;
                    break;
                }
            }

            if (availableStyle.HasValue)
            {
                Font font = null;
                try
                {
                    // do your own Font initialization here
                    // discard the one you don't like :-)
                    font = new Font(ff, 12, availableStyle.Value);
                }
                catch
                {
                }
                if (font != null)
                {
                    _fonts.Add(font);
                    Items.Add(font);
                }
            }
        }
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        if (_fonts != null)
        {
            foreach (Font font in _fonts)
            {
                font.Dispose();
            }
            _fonts = null;
        }
        if (_foreBrush != null)
        {
            _foreBrush.Dispose();
            _foreBrush = null;
        }
    }

    public override Color ForeColor
    {
        get
        {
            return base.ForeColor;
        }
        set
        {
            base.ForeColor = value;
            if (_foreBrush != null)
            {
                _foreBrush.Dispose();
            }
            _foreBrush = null;
        }
    }

    private Brush ForeBrush
    {
        get
        {
            if (_foreBrush == null)
            {
                _foreBrush = new SolidBrush(ForeColor);
            }
            return _foreBrush;
        }
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        base.OnDrawItem(e);
        if (e.Index < 0)
            return;

        e.DrawBackground();
        e.DrawFocusRectangle();
        Rectangle bounds = e.Bounds;
        Font font = (Font)Items[e.Index];
        e.Graphics.DrawString(font.Name, font, ForeBrush, bounds.Left, bounds.Top);
    }
}

public partial class MyFontDialog : Form
{
    private FontListBox _fontListBox;

    public MyFontDialog()
    {
        InitializeComponent();

        _fontListBox = new FontListBox();
        _fontListBox.Dock = DockStyle.Fill;
        Controls.Add(_fontListBox);
    }
}

我已经主持了项目在SourceForge https://sourceforge.net/p/newfontpicker/

推荐答案

您可以修改MyFontDialog是这样的:

You could modify the MyFontDialog like this:

public partial class MyFontDialog : Form
{
    private FontListBox _fontListBox;
    private ListBox _fontSizeListBox;

    public MyFontDialog()
    {
        //InitializeComponent();

        _fontListBox = new FontListBox();
        _fontListBox.SelectedIndexChanged += OnfontListBoxSelectedIndexChanged;
        _fontListBox.Size = new Size(200, Height);
        Controls.Add(_fontListBox);

        _fontSizeListBox = new ListBox();
        _fontSizeListBox.Location = new Point(_fontListBox.Width, 0);

        Controls.Add(_fontSizeListBox);
    }

    private void OnfontListBoxSelectedIndexChanged(object sender, EventArgs e)
    {
        _fontSizeListBox.Items.Clear();
        Font font = _fontListBox.SelectedItem as Font;
        if (font != null)
        {
            foreach (FontStyle style in Enum.GetValues(typeof(FontStyle)))
            {
                if (font.FontFamily.IsStyleAvailable(style))
                {
                    _fontSizeListBox.Items.Add(style);
                }
            }
        }
    }
}

这将预留创建一个列表框可用字体样式列表中的字体列表框。至于大小的选择,你可以简单地添加一个列表框硬codeD大小的列表:8,9,10,11,12,14,16,18,20,22,24,26,28,36 ,48和72,就像标准FontDialog类,因为我们正在处理的True Type字体。

It will create a list box aside the font list box with the list of available font styles. As for the size choice, you can simply add a list box with hardcoded list of size: 8,9,10,11,12, 14,16,18,20,22,24,26,28,36,48 and 72, just like the standard FontDialog, since we're dealing with true type fonts.

这篇关于设计自定义字体对话框/选择器对C#,过滤掉非True Type字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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