コンボボックスがあり、システムで使用可能なすべてのフォント (実際の名前、スタイルなど) を入力する必要があります...
オンラインで見つけることができるすべての情報から、 DrawItemイベントをまとめることができますが、「非デリゲート型 'System.Drawing.Font' を呼び出せません」というエラーが発生し続けます。他のウェブサイトからの行といくつかの変更を加えました。だから、私はそれがうまくいくはずだと思った。
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 イベントを呼び出していますが、独自のスタイルとサイズでフォントを描画していません。
コンボボックスを次の画像のようにしたい:
以下の私のようではありません: