创建字体对话框。如何显示每种字体及其设计?
这是我的代码:
private void DialogFont_Load(object sender, EventArgs e)
{
LoadInstalledFonts();
}
private void LoadInstalledFonts()
{
var fontCollection = new System.Drawing.Text.InstalledFontCollection();
foreach (var font in fontCollection.Families)
{
lstFonts.Items.Add(font.Name);
}
}
如何使用自己的设计显示每种字体,有点像字体的预览?我正在使用 ListBox 控件列出字体。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 WPF 中轻松完成此操作。
XAML 看起来像:
和 C# 代码隐藏:
您还可以在 CodeProject 上查看这篇文章,其中演示了(在 Winforms 示例中)如何使用您想要的字体填充列表框: http://www.codeproject.com/KB/selection/FontListBoxAndCombo.aspx
You can do it easily in WPF.
The XAML would look like:
And C# codebehind:
You could also check this article over on CodeProject which walks through (in a Winforms example) how to populate a list box with fonts like you want: http://www.codeproject.com/KB/selection/FontListBoxAndCombo.aspx
也许使用
ListView
而不是ListBox
?ListViewItem
type 有一个可以使用的Font
属性。我不知道ListBox
有任何特殊的每项格式化功能。更新:如果您仍在处理此问题,这是对我有用的一些代码片段(这不会按原样编译,因为它只是来自较大用户控件的剪辑;我相信您可以弄清楚但
结果是这样的:
Maybe go with a
ListView
instead of aListBox
? TheListViewItem
type has aFont
property you could use. I'm not aware of any special per-item formatting capabilities ofListBox
.Update: In case you're still working on this, here's a snippet of some code that worked for me (this won't compile as-is as it's just a clip from a larger user control; I'm sure you can figure out what goes where though):
Here's what the result looks like:
首先,我想确保您了解 FontDialog 控件,并且您有意要创建自定义字体对话框。如果您不知道它,那么也许您可以看一下它并确保它不符合您的需求,然后再尝试创建您自己的。以下堆栈溢出问题向您展示了如何确保它填充了所有内容设备字体而不仅仅是 TrueType 字体。
现在,如果您仍想创建自己的字体,则以下是一个简化的解决方案:
将标签添加到字体对话框中,并将其文本设置为您希望用户看到的字体示例。类似
AabBcC
的内容,甚至可以是随机句子。您可以在列表框的 SelectedIndexChanged 事件中设置标签的字体。这实际上会更改示例文本以匹配您指定的字体。下面是一个简单的示例:
请注意,如果您希望用户指定他们希望以其他字体查看的文本,您也可以使用文本框。此外,某些字体(例如 Andy 和 Aharomi)会抛出 ArgumentException,表明该字体不支持常规样式,因此捕获此异常类型是明智的。
First thing, I want to make sure that you are aware of the FontDialog control, and you are purposely wanting to create a custom Font Dialog. If you weren't aware of it, then maybe you can take a look at it and make sure that it doesn't fit your needs before trying to create your own. And the following Stack Overflow question shows you how to make sure it is populated with all the device fonts and not just TrueType fonts.
Now, if you still want to create your own, then the following is a simplified solution:
Add a Label to your Font Dialog and set its text to whatever you want the user to see as a sample of the font. Something like
AabBcC
, or it could even be a random sentence.You can set the Font of the label in the SelectedIndexChanged event of your ListBox. This in effect changes the sample text to match the font you specify. The following is a simple example:
Note that you can also use a Textbox if you want your user to specify the text that they want to see in another font. Also, some fonts like Andy and Aharomi throw an ArgumentException stating that the Font doesn't support a regular style, so it would be wise to catch this exception type.
使用列表框,我认为你需要进行所有者绘制。绘制每个列表项时,您需要选择该项目的字体。
Using a ListBox, I would think you'd need to do owner-draw. When drawing each list item, you'd need to select the font for that item.