Farbige einträge in ListBox



  • Hallo !

    Ich würde gerne in meiner Listbox "TitleList" zur besseren übersicht jeden 2ten eintrag fablich unterlegt haben. Mit dem Code unten klappt das soweit auch aber sobald man die Listbox schnell hoch/runter scrollt oder aber reinklickt und per Pfeiltasten die Einnträge durchläuft verändern sich die Farben. Was kann ich machen ? Danke !

    col1 & col2 sind Boolsche Werte beide mit true init.

    void __fastcall TForm1::TitleListDrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State)
    {
    
      if ( col1 && col2 )
      {
      TitleList->Canvas->Brush->Color=clSkyBlue;
      TitleList -> Canvas -> FillRect(Rect);
      }
      col2=!col2;
    
     TitleList -> Canvas -> Font -> Color = clBlack;
    
      TitleList -> Canvas -> TextOut(0, Rect.Top,TitleList -> Items -> Strings[Index]);
    }
    


  • Was genau bezweckst du mit der if-Abfrage? Warum nicht einfach modulo 2 (Index % 2) auf Null prüfen?



  • 🙂 Ok richtig ist wohl nicht allzu schick aber das ist nicht das eigendliche Problem oder ?



  • void __fastcall TfMain::lbCheatListDrawItem(TWinControl *Control,
          int Index, TRect &Rect, TOwnerDrawState State)
    {
        lbCheatList->Canvas->Font->Assign(fMain->Font);
        lbCheatList->Canvas->Font->Size=8;
        lbCheatList->Canvas->Font->Color=0xa000+(Index&1)*0x5000;
        lbCheatList->Canvas->Brush->Color=(Index&1)*0x5000;
        lbCheatList->Canvas->Pen->Style=psSolid;
        if(State.Contains(odSelected))
        {
            lbCheatList->Canvas->Font->Color=(Index&1)*0x5000;
            lbCheatList->Canvas->Brush->Color=0xa000+(Index&1)*0x2800;
        }
        lbCheatList->Canvas->Pen->Color=lbCheatList->Canvas->Brush->Color;
        lbCheatList->Canvas->Rectangle(Rect);
        lbCheatList->Canvas->Brush->Style=bsClear;
        lbCheatList->Canvas->TextOut(Rect.Left+4,
            Rect.Top+Rect.Height()*0.5-lbCheatList->Canvas->TextHeight("T")*0.5,
            IntToStr(Index+1));
        lbCheatList->Canvas->Pen->Color=lbCheatList->Canvas->Font->Color;
        lbCheatList->Canvas->MoveTo(Rect.Left+47,Rect.Top);
        lbCheatList->Canvas->LineTo(Rect.Left+47,Rect.Bottom);
        lbCheatList->Canvas->TextOut(Rect.Left+50,
            Rect.Top+Rect.Height()*0.5-lbCheatList->Canvas->TextHeight("T")*0.5,
                lbCheatList->Items->Strings[Index]);
    }
    

    Zum Verständnis:
    Übersicht
    Source

    Und so sieht's dann aus:
    Mittleres Bild



  • Karlo234 schrieb:

    🙂 Ok richtig ist wohl nicht allzu schick aber das ist nicht das eigendliche Problem oder ?

    Ich denke schon. Da du col2 immer wieder negierst, machst du die Ausgabe vom vorherigen Zustand abhängig. Allerdings kannst du nicht wisssen, wie der war und somit verunstaltet es deine Anzeige. Deswegen über den mitgelieferten Index gehen, da der immer konstant (für denselben Eintrag) bleibt.


Anmelden zum Antworten