Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在组合框中显示FontFamily?_.net_Fonts_Combobox_Delphi Prism_Ondrawitem - Fatal编程技术网

.net 如何在组合框中显示FontFamily?

.net 如何在组合框中显示FontFamily?,.net,fonts,combobox,delphi-prism,ondrawitem,.net,Fonts,Combobox,Delphi Prism,Ondrawitem,我有一个组合框,我需要用系统中所有可用的字体填充它-它们的实际名称、样式等 从我能在网上找到的所有信息中,我能够将DrawItem事件组合在一起,但我一直遇到以下错误,“无法调用非委托类型'System.Drawing.Font'”事实上,我从其他网站借用了每行一行,并做了一些更改。所以,我认为它应该起作用 以下是我在组合框中填充项目列表的方式: method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);

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

从我能在网上找到的所有信息中,我能够将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;
更新:我忘了将DrawMode设置为OwnerDrawFixed。现在它正在调用my DrawItem事件,但仍然没有以自己的样式和大小绘制字体

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

不像下面我的:


以下是最有可能帮助您的:


不过,也要确保实现本文中所需的基础结构:

以下是我的答案和工作代码

  • 创建新项目并打开主winform。打开你的工具箱 然后在主窗体上放置一个组合框
  • 打开刚放置的组合框的属性窗口 winform
  • 按如下方式设置组合框的以下属性:DrawMode=OwnerDrawFixed、DropDownStyle=DropDownList、FormattingEnabled=true、GenerateMeber=true、IntegralHeight=false和ItemHeight=25。
    • 双击主winform创建Mainform\u加载方法 并将以下代码相应地复制到加载方法
,

方法MainForm.MainForm\u加载(发送方:System.Object;e:System.EvenArgs);
变量
字体:字体;
开始
如果(ComboBox1.Items.Count>0),则
ComboBox1.Items.Clear;
对于FontFamily中的每个oneFontFamily。家庭会
开始
如果(oneFontFamily.IsStyleAvailable(FontStyle.Regular)),则
字体:=新字体(oneFontFamily.Name,12)
否则如果(oneFontFamily.IsStyleAvailable(FontStyle.Bold))则
字体:=新字体(oneFontFamily.Name,12,FontStyle.Bold)
否则如果(oneFontFamily.IsStyleAvailable(FontStyle.Italic))则
字体:=新字体(oneFontFamily.Name,12,FontStyle.Italic)
否则如果(oneFontFamily.IsStyleAvailable(FontStyle.Streekeout))则
thefont:=新字体(oneFontFamily.Name,12,FontStyle.删除线)
否则如果(oneFontFamily.isStyleAvailable(FontStyle.Underline))则
字体:=新字体(oneFontFamily.Name,12,FontStyle.Underline);
如果(前零)那么
ComboBox1.Items.Add(字体);
结束;
结束;
  • 为您的组合框创建DrawItem事件并复制以下内容 相应地编码到drawitem事件中
"

方法MainForm.ComboBox1\u DrawItem(发送方:System.Object;e:System.Windows.Forms.DrawItemEventArgs);
变量对象:字体;
开始
如果e.Index=-1,则退出;
//绘制项目的背景
e、 牵引杆接地();
//我们应该画焦点矩形吗
如果((e.State和DrawItemState.Focus)DrawItemState.Checked),则
e、 DrawFocusRectangle();
//创建一个新的背景笔刷。
变量b:=新的SolidBrush(例如前景色);
对象:=(ComboBox1.Items[e.Index]作为字体);
//画这个项目。
e、 图形.字符串(theobject.Name,theobject,b,e.Bounds);
结束;
完成并运行后,您应该有一个combobox1,显示如下字体:


e.指数值是多少?确保检查它不是-1。您的组合框列表中实际有多少种字体?@LarsTech,我猜是系统中的字体数。:)FontFamily有多少种字体。e、 Index是存储在items objectCollection列表中的每个字体对象的位置。我之所以问这个问题,是因为我不是delphi prism程序员,但“看起来”您只添加了一种字体,因为它不在begin-end块中。:)我刚刚意识到这一点并进行了更改。DrawString方法是想要string,font,x,y作为参数,我传递字符串,不知道是什么,x,y。这就是它在绘制字体时遇到问题的原因。那么,如何从font的对象中键入字体?
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);
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;