I. De la couleur dans une ListBox▲
Exemple : sur une Form (Form1) vous posez une ListBox (ListBox1) avec sa propriété Style à : lbOwnerDrawFixed. Vous y rajoutez quelques Items.
Puis le code suivant sur l'événement OnDrawItem de la ListBox :
Sélectionnez
void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control,
int Index, TRect &Rect, TOwnerDrawState State)
{
TCanvas *pCanvas = ((TListBox *)Control)->Canvas;
if ((Index%2)==0) pCanvas->Brush->Color = clYellow;
// met 1 ligne sur 2 en jaune
if(State.Contains(odSelected)) //ligne selectionnée
{
pCanvas->Brush->Color = clRed;
pCanvas->FillRect(Rect);
pCanvas->Font->Color = clYellow;
}
else pCanvas->FillRect(Rect); //Les autres lignes
//écriture les Items.
pCanvas->TextOut(Rect.Left+5,Rect.Top+2,
((TListBox *)Control)->Items->Strings[Index]);
}Avec l'utilisation de ((TListBox *)Control) au lieu de ListBox1 cette méthode est valable pour n'importe quel ListBox.


