Designing a custom dialog / font selector for C # filtering out the wrong type of fonts

Since the built-in Font Dialog returns a โ€œNot True Type Fontโ€ exception when selecting a Non True Type font, I'm trying to create a custom font dialog using font families that filter out non-true type fonts.

The control works fine, but for this dialog I need size and style selectors. I am posting the current code. Please help me add to this size and style. It may 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 posted the project on sourceforge https://sourceforge.net/p/newfontpicker/

+8
c # fonts winforms dialog
source share
3 answers

You can change MyFontDialog as follows:

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

It will create a list box aside from the font list with a list of available font styles. Regarding the choice of size, you can just add a list box with a hard-coded list of sizes: 8,9,10,11,12, 14,16,18,20,22,24,26,28,36,48 and 72, so same as the standard FontDialog, since we are dealing with true type fonts.

+1
source share

http://www.developerfusion.com/code/254/determine-if-a-font-is-truetype/ contains some VB code to determine if the font is a TT font. All he really does is call some Win32 API functions and analyze the results.

There may be some fonts that seem to be TT, but this is not true even if they are verified using the Win32 API (which is probably what the FontDialog does). If Win32 does not fix your problem, perhaps the only way to find out if the font is invalid is to check for an exception.

0
source share

OK, Umar, you should try:

1) Using "FontFamily.IsStyleAvailable" to avoid / minimize the need to catch exceptions - and thus skip some available fonts. 2) Play a little with Graphics.MeasureString to set the size for each individual font that looks best And you get columns with the same height ...

Happy try :)

Jens, Denmark.

0
source share

All Articles