2011-11-18 23 views
1

我有一個組合框,我需要與系統中的所有可用字體填充它 - 他們的實際名稱,風格等等如何在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; 

這裏是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

+0

什麼是e.Index價值?確保檢查它不是-1。 ComboBox列表中實際有多少種字體? – LarsTech

+0

@ LarsTech,我想這個系統中有很多。 :)無論FontFamily擁有多少種字體。 e.Index是存儲在項目objectCollection列表中的每個字體對象的位置。 – ThN

+0

我只問,因爲我不是一個delphi-prism程序員,但它「看起來」像只添加一種字體,因爲它不在開始結束塊內。 – LarsTech

回答

1

這是我的工作代碼的答案。

  • 創建一個新項目並打開您的主winform。打開您的工具箱 並在您的mainform上放置一個組合框。
  • 打開剛剛放置在 winform上的組合框的屬性窗口。
  • 設置您的組合框下面的屬性如下:DrawMode = OwnerDrawFixed,DropDownStyle = DropDownList的,FormattingEnabled = TRUE,GenerateMemeber = TRUE,IntegralHeight =虛假和ItemHeight = 25
    • 創建雙Mainform_Load方法點擊主要的Winform 並相應地將下面的代碼複製到您的加載方法中。

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; 

當你擁有這一切完成並運行你應該有一個combobox1顯示字體如下:

ComboBox displaying Font Text