2009-04-07 8 views

Répondre

6

Eh bien, vous devrez hériter d'un nouveau contrôle de ListBox. Pour cela, créez un nouveau projet dans votre solution, du type « bibliothèque de contrôles Windows », et utiliser le code ci-dessous dans le fichier de code source du contrôle du fichier:

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); 
    } 
} 

J'omis tout le code de concepteur et comme pour brièveté, mais vous devrez vous rappeler à Dispose de l'image et les pinceaux dans la méthode Dispose du contrôle.

+0

Merci Scraimer, c'est bon j'ai changé quelques lignes mais à la fin c'est du travail. Amuse-toi bien – JayJay

Questions connexes