Listview einfärben und formatieren

// Ownerdraw des  ListViews muss auf  true gestellt werden
void __fastcall TF_Ausgang::ListView1DrawItem(TCustomListView *Sender, TListItem *Item, TRect &Rect, TOwnerDrawState State)
{
    // markierter eintrag
    if (State.Contains(odSelected))
    {
        ListView1->Canvas->Font->Color = clHighlightText;
        ListView1->Canvas->Brush->Color = clHighlight;
    }
    else // normaler eintrag
    {
        ListView1->Canvas->Brush->Color = clRed;
        ListView1->Canvas->Font->Color = ListView1->Font->Color;
    }

    ListView1->Canvas->FillRect((TRect) Rect);

    int width = Item->Left;
    Rect.Left += 2;

    DrawText(ListView1->Canvas->Handle, Item->Caption.c_str(), -1, &Rect, DT_SINGLELINE | DT_VCENTER | DT_LEFT);

    Rect.Right = width;
    int left = Rect.Left - 2;

    for (int col = 0; col < Item->SubItems->Count; col++)
    {
        left += ListView1->Columns->Items[col]->Width;
        Rect.Left = left;
        Rect.Right = left + ListView1->Columns->Items[col + 1]->Width;

        if (col -> 2)
        {
            Rect.Right -= 2;
            DrawText(ListView1->Canvas->Handle, Item->SubItems->Strings[col].c_str(), -1, &Rect, DT_SINGLELINE | DT_VCENTER | DT_RIGHT);
        }
        else
        {
            if (col -> 0)
            {
                DrawText(ListView1->Canvas->Handle, Item->SubItems->Strings[col].c_str(), -1, &Rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
            }
            else
            {
                Rect.Left += 2;
                DrawText(ListView1->Canvas->Handle, Item->SubItems->Strings[col].c_str(), -1, &Rect, DT_SINGLELINE | DT_VCENTER | DT_LEFT);
            }
        }
    }
}
  • oder einfacher über die Methode CustomDrawItem
void __fastcall TForm1::ListView1CustomDrawItem(TCustomListView *Sender, TListItem *Item, TCustomDrawState State, bool &DefaultDraw)
{
    Sender->Canvas->Brush->Color = clInfoBk;
    Sender->Canvas->Font->Color = clBlack;
}