C++ Builder

La Isla Bonita



ListView with Color Items
ListView with Color Items


Description

In this article I will show how to create a TListView control, in Report Mode (Report ViewStyle), with color items. I will use the OnDrawItem event to render the ListView items in color. In order to use this event we must set the OwnerDraw property to true. So we write these two lines:

    ListView->OwnerDraw  = true;
    ListView->OnDrawItem = ListViewDrawItem;
Colors are selected randomly from the Colors[] array by means of the rand() function:

       ListView->Canvas->Font->Color = Colors[rand()%11];
All components on the form are created at runtime.

Code

ListViewColors.cpp

#include <vcl.h>
#pragma hdrstop

#include "ListViewColors.h"
//---------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

const int dX = 10;
const int NUMBER_LINES = 10;

TListColumn *Column1;
TListColumn *Column2;
TListColumn *Column3;

TListItem   *ListItem;
TListView   *ListView;

String S[] = {"I see trees of green, red roses too",
              "I see them bloom for me and you",
              "And I think to myself, what a wonderful world",
              "I see skies of blue and clouds of white",
              " The bright blessed day, the dark sacred night",
              " And I think to myself, what a wonderful world",
              "The colours of the rainbow, so pretty in the sky",
              "Are also on the faces of people going by",
              "I see friends shakin' hands, sayin 'How do you do?'",
              "They're really saying 'I love you'"};

TColor Colors[] = { clYellow, clWhite,           (TColor)0xefabc1,
                    clAqua,   (TColor)0x676feda,  clBlue,
                    clRed,    (TColor)0x654321,  (TColor)0xcceabc,
                    clBlack };

                    
//-----------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    randomize();
    Form1->Color = clPurple;
    Position = poScreenCenter;

    //Create a TListView Component
    ListView = new TListView( Form1 );
    ListView->Parent = Form1;
    ListView->Top    = 10;
    ListView->Height = 500;
    ListView->Width  = 600;
    ListView->Left   = 10;
    ListView->Align  = alNone;
    ListView->Color = clGray;
    ListView->Cursor      = crHandPoint;
    ListView->MultiSelect = true;
    ListView->Checkboxes  = false;
    ListView->SortType    = stNone; 
    ListView->GridLines   = false;

    // Select the Report View Style
    ListView->ViewStyle   = vsReport;

    //------------------------------------------------
    // Write code in an OnDrawItem handler to manually
    // draw items in the list view. This event occurs
    // only if OwnerDraw is true.
    ListView->OwnerDraw  = true;
    ListView->OnDrawItem = ListViewDrawItem;
    //------------------------------------------------

    if (ListView->ViewStyle == vsReport)
    {
        // Add Column
        Column1 = ListView->Columns->Add();
        Column1->Caption = "Column 1";
        Column1->Width   = 350;

        // Add Column
        Column2 = ListView->Columns->Add();
        Column2->Caption = "Column 2";
        Column2->Width   = 100;

        // Add Column
        Column3 = ListView->Columns->Add();
        Column3->Caption = "Column 3";
        Column3->Width   = 100;
    }

    // Add Items:
    for (int i = 0; i < NUMBER_LINES; i++)
    {
        ListItem = ListView->Items->Add();
        ListItem->Caption = S[i];
    }
}
//------------------------------------------------------

void __fastcall TForm1::ListViewDrawItem(TCustomListView *Sender,
                                         TListItem *Item,
                                         const TRect &Rect,
                                         TOwnerDrawState State)
{
    //  The OwnerDraw property must be set to true

    ListView->Canvas->FillRect(Rect);

    for (int i=0; i < NUMBER_LINES; i++)
    {
        if (Item->Index == i)
        {
            ListView->Canvas->Font->Color = Colors[rand()%11];
            ListView->Canvas->TextOut(Rect.Left + dX, Rect.Top, S[i]);

        }
    }
}

ListViewColors.h

#ifndef ListViewColorsH
#define ListViewColorsH
//----------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <Menus.hpp>
//----------------------------------------------------------------------

class TForm1 : public TForm
{
__published:	// IDE-managed Components

        void __fastcall ListViewDrawItem( TCustomListView *Sender,
                                          TListItem *Item,
                                          const TRect &Rect,
                                          TOwnerDrawState State );

private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);

};
//----------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//----------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2003 Rodolfo A. Frino. All rights reserved.