tec*_*hno 8 .net c# fonts dialog winforms
由于内置字体对话框在选择非真实类型字体时返回"非真实类型字体"异常,我正在尝试使用字体系列创建自定义字体对话框,该字体系列会过滤掉非真实类型字体.
控件工作正常但我需要这个对话框的大小和样式选择器.我发布了当前的代码.请帮我添加尺寸和样式选择器.它也可能对你有用.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我在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);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它将在字体列表框旁边创建一个列表框,其中包含可用字体样式的列表。至于尺寸选择,您只需添加一个包含硬编码尺寸列表的列表框:8、9、10、11、12、14、16、18、20、22、24、26、28、36、48 和 72 ,就像标准 FontDialog 一样,因为我们处理的是 true type 字体。
| 归档时间: |
|
| 查看次数: |
3266 次 |
| 最近记录: |