1

コンボボックスがあり、システムで使用可能なすべてのフォント (実際の名前、スタイルなど) を入力する必要があります...

オンラインで見つけることができるすべての情報から、 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 イベントを呼び出していますが、独自のスタイルとサイズでフォントを描画していません。

コンボボックスを次の画像のようにしたい:

他人のコンボボックス

以下の私のようではありません:

コンボボックス付きのwinformの実際の画像

4

2 に答える 2

1

これはおそらくあなたを助けるものです:http://www.vbaccelerator.com/home/net/code/controls/ListBox_and_ComboBox/Font_Picker/article.asp

ただし、この記事から必要なインフラストラクチャも必ず実装してください: http://www.vbaccelerator.com/home/NET/Code/Controls/ListBox_and_ComboBox/Icon_ComboBox/article.asp

于 2011-11-18T17:53:33.610 に答える
1

これが作業コードでの私の答えです。

  • 新しいプロジェクトを作成し、メインの 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 が表示されます。

フォント テキストを表示するコンボ ボックス

于 2011-11-18T20:55:39.543 に答える