c# - 为 C# 设计自定义字体对话框/选择器,过滤掉非真实字体

标签 c# .net winforms fonts dialog

由于内置字体对话框在选择非 True Type 字体时返回“不是 True Type 字体”异常,我正在尝试使用过滤掉非 True Type 字体的字体系列创建自定义字体对话框。

控件运行良好,但我需要此对话框的大小和样式选择器。我正在发布当前代码。请帮我添加尺寸和样式选择器。它也可能对您有用。

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:

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

它将在字体列表框旁边创建一个列表框,其中包含可用字体样式的列表。至于大小选择,您可以简单地添加一个列表框,其中包含硬编码的大小列表:8、9、10、11、12、14、16、18、20、22、24、26、28、36、48 和 72 ,就像标准的 FontDialog 一样,因为我们处理的是真正的字体。

关于c# - 为 C# 设计自定义字体对话框/选择器,过滤掉非真实字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8644007/

相关文章:

c# - SQL Server 数据库密码

c# - 使用 C# 访问网页的内容

.net - pdb 文件包含多少信息? (C#/.NET)

c# - 解决构造函数中的虚拟成员调用

c# - 调用 OpenFileDialog.ShowDialog() 时 Windows 窗体 GUI 挂起

c# - 用户控件中的 HandleDestroyed 事件

c# - 保存包含换行符的 richtextbox 数据,将其保存到文件并使用记事本打开它不会在 c# 中显示换行符

c# - ListView 项目不清除 (C#/WPF)

c# - 在c#中将多个参数化变量添加到数据库

c# - 是否可以在 Windows CE 平台上运行我的 Windows 窗体应用程序?