C++ Builder

La Isla Bonita



ListBox With Coloured Lines
ListBox With Coloured Lines


Description

This example shows how to draw color lines (or items) in a TListBox component. The program will draw the first 20 items with 20 different colors, then the default color will be used. The Style of the List Box component (ListBox1) has to be set to either lbOwnerDrawFixed or lbOwnerDrawVariable for the event OnDrawItem to be executed. The default colors are contained in an array. All the components on Form1 are created dynamically.

ListBoxColors.cpp

#include <vcl.h>
#pragma hdrstop

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

enum DefaultColors
{
    BACKGROUND_COLOR = 0,
    FONT_COLOR
};

static int line_count         = 1;
const int  dX                 = 5;
const int  DIM_DEFAULT_COLORS = 2;
const int  DIM_COLORS         = 20;

TColor ColorsDefault[DIM_DEFAULT_COLORS] = { clBlack, clWhite };

TColor Colors[DIM_COLORS] = { clYellow,         clRed,
                              clBlue,           clGreen,
                              clSilver,         clTeal,
                              clMaroon,         clOlive,
                              (TColor)0xabffed, clFuchsia,
                              (TColor)0xefac,   (TColor)0xaabbcc,
                              (TColor)0xffadeb, (TColor)0x1155aa,
                              (TColor)0x33bb22, (TColor)0x6611f0,
                              (TColor)0xf3f25a, (TColor)0x347890,
                              (TColor)0xe356ff, (TColor)0x45fdab  };

String SText1 = " This color is nice";
String SText2 = " This color is the default";
TListBox* ListBox1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    //SET THE FORM DIMENSIONS
    Width  = 400;
    Height = 500;

    //CREATE A LISTBOX: ListBox
    ListBox1 = new TListBox( this );
    ListBox1->Parent = this;
    ListBox1->Top    = 10;
    ListBox1->Left   = 30;
    ListBox1->Width  = 200;
    ListBox1->Height = Height - (5*ListBox1->Top);
    ListBox1->Color       = ColorsDefault[BACKGROUND_COLOR];
    ListBox1->Font->Color = ColorsDefault[FONT_COLOR];
    ListBox1->Style       = lbOwnerDrawVariable;
    ListBox1->OnDrawItem  = ListBox1DrawItem;

    //CREATE A BUTTON TO ADD LINES TO THE LISTBOX: Button1
    TButton* Button1 = new TButton( this );
    Button1->Parent  = this;
    Button1->Top     = ListBox1->Top;
    Button1->Left    = ListBox1->Left + ListBox1->Width + 30;
    Button1->Width   = 100;
    Button1->Height  = 50;
    Button1->Caption = "Add Item";
    Button1->OnClick = Button1Click;

    //CREATE A BUTTON TO CLEAR THE LISTBOX: Button2
    TButton* Button2 = new TButton( this );
    Button2->Parent  = this;
    Button2->Top     = Button1->Top + Button1->Height + 20;
    Button2->Left    = Button1->Left;
    Button2->Width   = Button1->Width;
    Button2->Height  = Button1->Height;
    Button2->Caption = "Clear";
    Button2->OnClick = Button2Click;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//
    if ( line_count <= DIM_COLORS )
        ListBox1->Items->Add( String( line_count++ ) + SText1 );
    else
        ListBox1->Items->Add( String( line_count++ ) + SText2 );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//
    ListBox1->Clear();
    line_count = 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DrawItem( TWinControl *Control,
                                          int Index,
                                          const TRect& Rect,
                                          TOwnerDrawState State)
{
    //ShowMessage("ListBox1DrawItem");
    //I added const to the third argument to make it to compile
    ListBox1->Canvas->FillRect( Rect );

    for ( int i=0; i < DIM_COLORS; i++ )
    {
        if (Index == i)
        {
           ListBox1->Canvas->Font->Color = Colors[i];
        }
    }

    ListBox1->Canvas->TextOut( Rect.Left + dX, Rect.Top,
                               ListBox1->Items->Strings[Index] );
}
//---------------------------------------------------------------------------

ListBoxColors.h

#ifndef ListBoxColorsH
#define ListBoxColorsH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall ListBox1DrawItem(TWinControl *Control,
                        int Index,
                        const TRect& Rect,
                        TOwnerDrawState State);
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
                        

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

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

Homepage

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