Custom Font Dialog/Selector for C# that filters out non true type fonts

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Since the inbuilt Font Dialog returns a Not a True Type Font Exception on selecting a Non True Type Font, Im trying to create a Custom Font Dialog using Font-families which filter out non true type fonts.
The Control is working perfectly but I need a size and style selectors for this dialog. Im posting the current code. Please help me add a size and a style selector to this. It could also be useful to you.
Ive hosted this on source forge see https://sourceforge.net/projects/newfontpicker/" target="_blank
https://sourceforge.net/projects/newfontpicker/
<pre>
Code:
<span>pub<pre>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 dont 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 dont 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);
    }
}
<br/>lic<span> <span>class<span> <span>FontListBox<span> <span>:<span> <span>ListBox<span><br/><span>{<span><br/> <span>private<span> <span>List<span><<span>Font<span>><span> _fonts <span>=<span> <span>new<span> <span>List<span><<span>Font<span>>();<span><br/> <span>private<span> <span>Brush<span> _foreBrush<span>;<span><br/><br/> <span>public<span> <span>FontListBox<span>()<span><br/> <span>{<span><br/> <span>DrawMode<span> <span>=<span> <span>DrawMode<span>.<span>OwnerDrawFixed<span>;<span><br/> <span>ItemHeight<span> <span>=<span> <span>20<span>;<span><br/> <span>foreach<span> <span>(<span>FontFamily<span> ff <span>in<span> <span>FontFamily<span>.<span>Families<span>)<span><br/> <span>{<span><br/> <span>// determine the first available style, as all fonts dont support all styles<span><br/> <span>FontStyle<span>?<span> availableStyle <span>=<span> <span>null<span>;<span><br/> <span>foreach<span> <span>(<span>FontStyle<span> style <span>in<span> <span>Enum<span>.<span>GetValues<span>(<span>typeof<span>(<span>FontStyle<span>)))<span><br/> <span>{<span><br/> <span>if<span> <span>(<span>ff<span>.<span>IsStyleAvailable<span>(<span>style<span>))<span><br/> <span>{<span><br/> availableStyle <span>=<span> style<span>;<span><br/> <span>break<span>;<span><br/> <span>}<span><br/> <span>}<span><br/><br/> <span>if<span> <span>(<span>availableStyle<span>.<span>HasValue<span>)<span><br/> <span>{<span><br/> <span>Font<span> font <span>=<span> <span>null<span>;<span><br/> <span>try<span><br/> <span>{<span><br/> <span>// do your own Font initialization here<span><br/> <span>// discard the one you dont like :-)<span><br/> font <span>=<span> <span>new<span> <span>Font<span>(<span>ff<span>,<span> <span>12<span>,<span> availableStyle<span>.<span>Value<span>);<span><br/> <span>}<span><br/> <span>catch<span><br/> <span>{<span><br/> <span>}<span><br/> <span>if<span> <span>(<span>font <span>!=<span> <span>null<span>)<span><br/> <span>{<span><br/> _fonts<span>.<span>Add<span>(<span>font<span>);<span><br/> <span>Items<span>.<span>Add<span>(<span>font<span>);<span><br/> <span>}<span><br/> <span>}<span><br/> <span>}<span><br/> <span>}<span><br/><br/> <span>protected<span> <span>override<span> <span>void<span> <span>Dispose<span>(<span>bool disposing<span>)<span><br/> <span>{<span><br/> <span>base<span>.<span>Dispose<span>(<span>disposing<span>);<span><br/> <span>if<span> <span>(<span>_fonts <span>!=<span> <span>null<span>)<span><br/> <span>{<span><br/> <span>foreach<span> <span>(<span>Font<span> font <span>in<span> _fonts<span>)<span><br/> <span>{<span><br/> font<span>.<span>Dispose<span>();<span><br/> <span>}<span><br/> _fonts <span>=<span> <span>null<span>;<span><br/> <span>}<span><br/> <span>if<span> <span>(<span>_foreBrush <span>!=<span> <span>null<span>)<span><br/> <span>{<span><br/> _foreBrush<span>.<span>Dispose<span>();<span><br/> _foreBrush <span>=<span> <span>null<span>;<span><br/> <span>}<span><br/> <span>}<span><br/><br/> <span>public<span> <span>override<span> <span>Color<span> <span>ForeColor<span><br/> <span>{<span><br/> get<br/> <span>{<span><br/> <span>return<span> <span>base<span>.<span>ForeColor<span>;<span><br/> <span>}<span><br/> set<br/> <span>{<span><br/> <span>base<span>.<span>ForeColor<span> <span>=<span> value<span>;<span><br/> <span>if<span> <span>(<span>_foreBrush <span>!=<span> <span>null<span>)<span><br/> <span>{<span><br/> _foreBrush<span>.<span>Dispose<span>();<span><br/> <span>}<span><br/> _foreBrush <span>=<span> <span>null<span>;<span><br/> <span>}<span><br/> <span>}<span><br/><br/> <span>private<span> <span>Brush<span> <span>ForeBrush<span><br/> <span>{<span><br/> get<br/> <span>{<span><br/> <span>if<span> <span>(<span>_foreBrush <span>==<span> <span>null<span>)<span><br/> <span>{<span><br/> _foreBrush <span>=<span> <span>new<span> <span>SolidBrush<span>(<span>ForeColor<span>);<span><br/> <span>}<span><br/> <span>return<span> _foreBrush<span>;<span><br/> <span>}<span><br/> <span>}<span><br/><br/> <span>protected<span> <span>override<span> <span>void<span> <span>OnDrawItem<span>(<span>DrawItemEventArgs<span> e<span>)<span><br/> <span>{<span><br/> <span>base<span>.<span>OnDrawItem<span>(<span>e<span>);<span><br/> <span>if<span> <span>(<span>e<span>.<span>Index<span> <span><<span> <span>0<span>)<span><br/> <span>return<span>;<span><br/><br/> e<span>.<span>DrawBackground<span>();<span><br/> e<span>.<span>DrawFocusRectangle<span>();<span><br/> <span>Rectangle<span> bounds <span>=<span> e<span>.<span>Bounds<span>;<span><br/> <span>Font<span> font <span>=<span> <span>(<span>Font<span>)<span>Items<span>[<span>e<span>.<span>Index<span>];<span><br/> e<span>.<span>Graphics<span>.<span>DrawString<span>(<span>font<span>.<span>Name<span>,<span> font<span>,<span> <span>ForeBrush<span>,<span> bounds<span>.<span>Left<span>,<span> bounds<span>.<span>Top<span>);<span><br/> <span>}<span><br/><span>}<span><br/><br/><span>public<span> <span>partial<span> <span>class<span> <span>MyFontDialog<span> <span>:<span> <span>Form<span><br/><span>{<span><br/> <span>private<span> <span>FontListBox<span> _fontListBox<span>;<span><br/><br/> <span>public<span> <span>MyFontDialog<span>()<span><br/> <span>{<span><br/> <span>InitializeComponent<span>();<span><br/><br/> _fontListBox <span>=<span> <span>new<span> <span>FontListBox<span>();<span><br/> _fontListBox<span>.<span>Dock<span> <span>=<span> <span>DockStyle<span>.<span>Fill<span>;<span><br/> <span>Controls<span>.<span>Add<span>(<span>_fontListBox<span>);<span><br/> <span>}<span><br/><span>}<span><br/>[/code][/code]
<br/>

View the full article
 
Back
Top