我有一個組合框,我需要與系統中的所有可用字體填充它 - 他們的實際名稱,風格等等如何在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事件,但仍然不以他們自己的樣式和大小繪製字體。
我想組合框看起來像下面的圖片:
不喜歡下面礦:
什麼是e.Index價值?確保檢查它不是-1。 ComboBox列表中實際有多少種字體? – LarsTech
@ LarsTech,我想這個系統中有很多。 :)無論FontFamily擁有多少種字體。 e.Index是存儲在項目objectCollection列表中的每個字體對象的位置。 – ThN
我只問,因爲我不是一個delphi-prism程序員,但它「看起來」像只添加一種字體,因爲它不在開始結束塊內。 – LarsTech