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

后端 未结 3 1572
暗喜
暗喜 2020-12-07 02:00

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

3条回答
  •  抹茶落季
    2020-12-07 02:03

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

    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.

提交回复
热议问题