如何在 Combobox 中显示 FontFamily?

发布于 2024-12-16 19:38:28 字数 2626 浏览 2 评论 0原文

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

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

我希望组合框如下图所示:

别人的组合框

不像下面我的:

带有组合框的 winform 的实际图像

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:

someone else's combobox

Not like mine below:

actual Image of the winform with combobox

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱果果 2024-12-23 19:38:28

这是我的答案和工作代码。

  • 创建一个新项目并打开主 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;
  • 为您的组合框创建 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;

当您完成所有操作并运行后,您应该有一个显示字体的组合框1,如下所示:

ComboBox 显示字体文本

Here is my answer with working code.

  • create a new project and open your main winform. Open your ToolBox
    and place a combobox on your mainform.
  • Open the property window for the combobox that you just placed on
    the winform.
  • Set the following properties for your combobox as follows: DrawMode = OwnerDrawFixed, DropDownStyle = DropDownList, FormattingEnabled = true, GenerateMemeber = true, IntegralHeight = false and ItemHeight = 25.
    • Create Mainform_Load method by double clicking on the main winform
      and copy the following code accordingly to your load method.

,

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;
  • Create DrawItem event for the your comboBox and copy the following
    code accordingly into your drawitem event.

'

    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;

When you have it all done and running you should have a combobox1 displaying fonts as follows:

ComboBox displaying Font Text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文