ListBox - farbige Einträge



  • Hallo alle zusammen!
    Meine Frage: Wie kann ich die Texte verschiedener Einträge einer TListBox in unterschiedlichen (Text-)Farben 'erstrahlen' lassen?
    MfG Matze

    [ Dieser Beitrag wurde am 10.01.2002 um 22:48 Uhr von Jansen editiert. ]



  • Das geht wie üblich im OnDraw-Ereignis, hier OnDrawItem. Der Style muss dabei natürlich auf lbOwnerDrawFixed bzw. -Variable stehen.

    TColor LBColors[3] = {clBlue, clGreen, clRed};
    ListBox1->Items->CommaText = "Eins,Zwei,Drei";
    
    void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index,
    TRect &Rect, TOwnerDrawState State)
    {
      if(State.Contains(odSelected))
      {
        ListBox1->Canvas->Brush->Color = LBColors[Index];   // Hintergrundfarbe
        ListBox1->Canvas->Font->Color = LBColors[3-Index];  // Textfarbe
      }
      else
      {
        ListBox1->Canvas->Brush->Color = LBColors[3-Index]; // dito
        ListBox1->Canvas->Font->Color = LBColors[Index];
      }
    
      // Hintergrund zeichnen
      ListBox1->Canvas->FillRect(Rect);
      // Text zeichnen
      ListBox1->Canvas->TextRect(Rect, Rect.Left, Rect.Top,
                                 ListBox1->Items->Strings[Index]);
    
      if(State.Contains(odFocused))
        ListBox1->Canvas->DrawFocusRect(Rect);
    }
    

Anmelden zum Antworten