为C#设计自定义字体对话框/选择器,以过滤掉非真实类型的字体 [英] Designing a Custom Font Dialog/Selector for C# that filters out non true type fonts

查看:222
本文介绍了为C#设计自定义字体对话框/选择器,以过滤掉非真实类型的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于内置字体对话框在选择非真实类型字体时返回非真实类型字体异常,因此我尝试使用Font-family创建自定义字体对话框,该字体过滤掉非真实类型的字体。



该控件工作正常,但我需要一个大小和样式选择器为此对话框。我正在发布当前的代码。请帮助我添加一个大小和一个样式选择器。它也可能对你有用。

  public class FontListBox:ListBox 
{
private List< Font& ; _fonts = new List< Font>();
私人刷_foreBrush;

public FontListBox()
{
DrawMode = DrawMode.OwnerDrawFixed;
ItemHeight = 20;
foreach(FontFamily.Families中的FontFamily ff)
{
//确定第一个可用样式,因为所有字体都不支持所有样式
FontStyle? availableStyle = null;
foreach(Enum.GetValues(typeof(FontStyle)中的FontStyle样式))
{
if(ff.IsStyleAvailable(style))
{
availableStyle = style;
break;
}
}

if(availableStyle.HasValue)
{
Font font = null;
尝试
{
//在这里执行您自己的字体初始化
//丢弃不喜欢的字体:-)
font = new Font(ff, 12,availableStyle.Value);
}
catch
{
}
if(font!= null)
{
_fonts.Add(font);
Items.Add(font);
}
}
}
}

protected override void Dispose(bool disposal)
{
base.Dispose );
if(_fonts!= null)
{
foreach(_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();
矩形边界= 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为Font;
if(font!= null)
{
foreach(Enum.GetValues(typeof(FontStyle))中的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,因为我们正在处理真正的类型字体。


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/

解决方案

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.

这篇关于为C#设计自定义字体对话框/选择器,以过滤掉非真实类型的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆