如何在 Combobox 中显示 FontFamily?
我有一个组合框,我需要用系统中所有可用的字体填充它 - 它们的实际名称、样式等...
根据我可以在网上找到的所有信息,我能够将 DrawItem 事件放在一起,但是我继续遇到以下错误,“无法调用非委托类型'System.Drawing.Font'”事实上,我从其他网站借用了一行一行并做了一些更改。所以,我认为它应该有效。
以下是我在组合框中填充项目列表的方法:
method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
var thefont:Font;
begin
if (ComboBox4.Items.Count>0) then
ComboBox4.Items.Clear;
for each oneFontFamily in FontFamily.Families do
begin
if (oneFontFamily.IsStyleAvailable(FontStyle.Regular)) then
thefont := new Font(oneFontFamily.Name, 15)
else if (oneFontFamily.IsStyleAvailable(FontStyle.Bold)) then
thefont := new Font(oneFontFamily.Name, 15,FontStyle.Bold)
else if (oneFontFamily.IsStyleAvailable(FontStyle.Italic)) then
thefont := new Font(oneFontFamily.Name, 15,FontStyle.Italic)
else if (oneFontFamily.IsStyleAvailable(FontStyle.Strikeout)) then
thefont := new Font(oneFontFamily.Name, 15, FontStyle.Strikeout)
else if (oneFontFamily.isStyleAvailable(FontStyle.Underline)) then
thefont := new Font(oneFontFamily.Name, 15, FontStyle.Underline);
if (thefont <> nil) then
ComboBox4.Items.Add(theFont);
end;
end;
这是组合框4绘制项目事件:
method MainForm.comboBox4_DrawItem(sender: System.Object; e: System.Windows.Forms.DrawItemEventArgs);
begin
if e.index = -1 then exit;
// Draw the background of the item
e.DrawBackground();
// Should we draw the focus rectangle
if ((e.State and DrawItemState.Focus) <> DrawItemState.Checked) then
e.DrawFocusRectangle();
// Create a new background brush.
var b := new SolidBrush(e.ForeColor);
// Draw the item.
// This line raises the above mentioned error.
e.Graphics.DrawString(FontFamily(comboBox4.Items[e.Index]).Name, font(comboBox4.Items[e.Index]), b, e.Bounds.x,e.Bounds.y); <<===== Here is where the error is raised
end;
更新: 我修改了导致错误的行,现在编译时没有错误,但正如我在评论中所述,它没有以自己的样式和大小绘制字体。
e.Graphics.DrawString(FontFamily(comboBox4.Items[e.Index]).Name, new font((comboBox4.Items[e.Index] as Font), (comboBox4.Items[e.Index] as Font).Style), b, e.Bounds.x,e.Bounds.y);
更新:我忘记将 DrawMode 设置为 OwnerDrawFixed。现在它正在调用我的 DrawItem 事件,但仍然没有按照自己的样式和大小绘制字体。
我希望组合框如下图所示:
不像下面我的:
I have a combobox and I need to populate it with all the available fonts in the system - their actual name, style, etc...
From all the information I can find online, I am able to put together the DrawItem event, but I keep running into the following error, "Cannot invoke non-delegate type 'System.Drawing.Font'" In fact, I borrowed line per line from other websites and made some changes. So, I thought it should have worked.
Here is how I populate the items list in the ComboBox:
method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
var thefont:Font;
begin
if (ComboBox4.Items.Count>0) then
ComboBox4.Items.Clear;
for each oneFontFamily in FontFamily.Families do
begin
if (oneFontFamily.IsStyleAvailable(FontStyle.Regular)) then
thefont := new Font(oneFontFamily.Name, 15)
else if (oneFontFamily.IsStyleAvailable(FontStyle.Bold)) then
thefont := new Font(oneFontFamily.Name, 15,FontStyle.Bold)
else if (oneFontFamily.IsStyleAvailable(FontStyle.Italic)) then
thefont := new Font(oneFontFamily.Name, 15,FontStyle.Italic)
else if (oneFontFamily.IsStyleAvailable(FontStyle.Strikeout)) then
thefont := new Font(oneFontFamily.Name, 15, FontStyle.Strikeout)
else if (oneFontFamily.isStyleAvailable(FontStyle.Underline)) then
thefont := new Font(oneFontFamily.Name, 15, FontStyle.Underline);
if (thefont <> nil) then
ComboBox4.Items.Add(theFont);
end;
end;
Here is the combobox4 drawitem event:
method MainForm.comboBox4_DrawItem(sender: System.Object; e: System.Windows.Forms.DrawItemEventArgs);
begin
if e.index = -1 then exit;
// Draw the background of the item
e.DrawBackground();
// Should we draw the focus rectangle
if ((e.State and DrawItemState.Focus) <> DrawItemState.Checked) then
e.DrawFocusRectangle();
// Create a new background brush.
var b := new SolidBrush(e.ForeColor);
// Draw the item.
// This line raises the above mentioned error.
e.Graphics.DrawString(FontFamily(comboBox4.Items[e.Index]).Name, font(comboBox4.Items[e.Index]), b, e.Bounds.x,e.Bounds.y); <<===== Here is where the error is raised
end;
UPDATED:
I modified the line that was causing the error and it compiles without error now but as I stated in my comment it is not drawing the fonts in their own style and size.
e.Graphics.DrawString(FontFamily(comboBox4.Items[e.Index]).Name, new font((comboBox4.Items[e.Index] as Font), (comboBox4.Items[e.Index] as Font).Style), b, e.Bounds.x,e.Bounds.y);
UPDATE: I forgot to set the DrawMode to OwnerDrawFixed. Now it is calling my DrawItem Event but still not drawing the Fonts in their own style and sizes.
I want the combobox to look like the following image:
Not like mine below:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容很可能对您有帮助: http:// www.vbaccelerator.com/home/net/code/controls/ListBox_and_ComboBox/Font_Picker/article.asp
请务必实现本文中所需的基础结构,不过: http://www.vbaccelerator.com/home /NET/Code/Controls/ListBox_and_ComboBox/Icon_ComboBox/article.asp
Here is something that most probably helps you: http://www.vbaccelerator.com/home/net/code/controls/ListBox_and_ComboBox/Font_Picker/article.asp
Be sure to implement the required infrastructure from this article too, though: http://www.vbaccelerator.com/home/NET/Code/Controls/ListBox_and_ComboBox/Icon_ComboBox/article.asp
这是我的答案和工作代码。
并在主窗体上放置一个组合框。
的winform。
并将以下代码相应地复制到您的加载方法。
,
相应地编码到您的drawitem事件中。
'
当您完成所有操作并运行后,您应该有一个显示字体的组合框1,如下所示:
Here is my answer with working code.
and place a combobox on your mainform.
the winform.
and copy the following code accordingly to your load method.
,
code accordingly into your drawitem event.
'
When you have it all done and running you should have a combobox1 displaying fonts as follows: