CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2011
    Posts
    1

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

    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.

    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);
    }
    }

    I have hosted the project at sourceforge https://sourceforge.net/p/newfontpicker/

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Designing a Custom Font Dialog/Selector for C# that filters out non true type fon

    Please use CODE tags when posting code!

    Perhaps this article might help :

    Extending the ComboBox with C#
    http://www.codeguru.com/csharp/cshar...cle.php/c15261

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured