2009-04-07 12 views

答えて

6

さて、ListBoxから新しいコントロールを継承する必要があります。

public partial class ListBoxWithBg : ListBox 
{ 
    Image image; 
    Brush brush, selectedBrush; 

    public ListBoxWithBg() 
    { 
     InitializeComponent(); 

     this.DrawMode = DrawMode.OwnerDrawVariable; 
     this.DrawItem += new DrawItemEventHandler(ListBoxWithBg_DrawItem); 
     this.image = Image.FromFile("C:\\some-image.bmp"); 
     this.brush = new SolidBrush(Color.Black); 
     this.selectedBrush = new SolidBrush(Color.White); 
    } 

    void ListBoxWithBg_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     e.DrawBackground(); 
     e.DrawFocusRectangle(); 
     /* HACK WARNING: draw the last item with the entire image at (0,0) 
     * to fill the whole ListBox. Really, there's many better ways to do this, 
     * just none quite so brief */ 
     if (e.Index == this.Items.Count - 1) 
     { 
      e.Graphics.DrawImage(this.image, new Point(0, 0)); 
     } 
     else 
     { 
      e.Graphics.DrawImage(this.image, e.Bounds, e.Bounds, GraphicsUnit.Pixel); 
     } 
     Brush drawBrush = 
      ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
      ? this.selectedBrush : this.brush; 
     e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, drawBrush, e.Bounds); 
    } 
} 

は、私はすべてのデザイナーのコードと、このような省略:それまでに、タイプ「Windowsコントロールライブラリ」で、あなたのソリューションに新しいプロジェクトを作成し、ファイル制御のソースコードファイルに以下のコードを使用します簡潔ですが、Disposeのコントロールの画像とブラシのDisposeを覚えておく必要があります。

+0

ありがとうScraimer、 私はいくつかの行を変更しましたが、最後は作業です。 楽しい時間を過ごしましょう – JayJay

関連する問題