如何在Combobox中显示FontFamily?

时间:2011-11-18 16:23:27

标签: .net fonts combobox delphi-prism ondrawitem

我有一个组合框,我需要用系统中所有可用的字体填充它 - 它们的实际名称,样式等......

从我在网上找到的所有信息中,我能够将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事件,但仍然没有以自己的样式和大小绘制字体。

我希望组合框看起来像下图:

someone else's combobox

不像我的下面:

actual Image of the winform with combobox

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

以下是我对工作代码的回答。

  • 创建一个新项目并打开您的主要winform。打开你的工具箱 并在你的主体上放置一个组合框。
  • 打开刚刚放置的组合框的属性窗口 winform。
  • 为您的组合框设置以下属性,如下所示: DrawMode = OwnerDrawFixed,DropDownStyle = DropDownList,FormattingEnabled = true,GenerateMemeber = true,IntegralHeight = false,ItemHeight = 25.
    • 双击主winform创建Mainform_Load方法 并相应地将以下代码复制到您的加载方法。

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;
  • 为您的comboBox创建DrawItem事件并复制以下内容 相应地编写您的drawitem事件。

'

    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,如下所示:

ComboBox displaying Font Text