我有一个组合框,我需要用系统中所有可用的字体填充它 - 它们的实际名称,样式等......
从我在网上找到的所有信息中,我能够将DrawItem事件放在一起,但我一直遇到以下错误,“无法调用非委托类型'System.Drawing.Font'”< / strong>事实上,我从其他网站借用了每行的行并进行了一些更改。所以,我认为它应该有效。
以下是我在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;
这是combobox4 drawitem事件:
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事件,但仍然没有以自己的样式和大小绘制字体。
我希望组合框看起来像下图:
不像我的下面:
答案 0 :(得分:1)
以下内容最有可能对您有所帮助: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
答案 1 :(得分:1)
以下是我对工作代码的回答。
,
Method MainForm.MainForm_Load(sender: System.Object; e:System.EvenArgs);
var
thefont:Font;
begin
if (ComboBox1.Items.Count>0) then
ComboBox1.Items.Clear;
for each oneFontFamily in FontFamily.Families do
begin
if (oneFontFamily.IsStyleAvailable(FontStyle.Regular)) then
thefont := new Font(oneFontFamily.Name, 12)
else if (oneFontFamily.IsStyleAvailable(FontStyle.Bold)) then
thefont := new Font(oneFontFamily.Name, 12,FontStyle.Bold)
else if (oneFontFamily.IsStyleAvailable(FontStyle.Italic)) then
thefont := new Font(oneFontFamily.Name, 12,FontStyle.Italic)
else if (oneFontFamily.IsStyleAvailable(FontStyle.Strikeout)) then
thefont := new Font(oneFontFamily.Name, 12, FontStyle.Strikeout)
else if (oneFontFamily.isStyleAvailable(FontStyle.Underline)) then
thefont := new Font(oneFontFamily.Name, 12, FontStyle.Underline);
if (thefont <> nil) then
ComboBox1.Items.Add(theFont);
end;
end;
'
Method MainForm.ComboBox1_DrawItem(sender:System.Object; e: System.Windows.Forms.DrawItemEventArgs);
var theobject:Font;
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);
theobject := (ComboBox1.Items[e.Index] as font);
// Draw the item.
e.Graphics.DrawString(theobject.Name, theObject, b,e.Bounds);
end;
当你完成所有操作后,你应该有一个显示字体的combobox1,如下所示: