C++ Builder

La Isla Bonita



ComboBox with Coloured Items
ComboBox with Coloured Items


Description

This program creates a combo box with items of different colors. On the left of each item a coloured box is drawn. The combo box's style is set to csOwnerDrawVariable so that we can draw items in different colours and shapes such as rectangles.
We use the Canvas method TextOut() to draw the items on the Canvas and the Canvas method Rectangle() to draw the corresponding coloured boxes.

ComboBoxColors.cpp

#include <vcl.h>
#pragma hdrstop

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

const int        dX = 20;
const int        dY = 15;
const int        dX_SEP = 5;
//---------------------------------------------------------------------------


String MyItems[] = { "Gladiators", "The Lord of the Rings",
                     "The Others", "Titanic", "Pretty Woman",
                     "The Third Element", "Total Recall",
                     "The Ten Commandments", "Pearl Habour",
                     "Notting Hill", "Deep Impact",
                     "Diamonds are Forever", "You Only Live Twice",
                     "The Jewel of the Nile", "Castaway",
                     "For Your Eyes Only"
                   };

namespace MyColors
{
    const TColor clBackground = clBlack;
}

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    Width  = 500;
    Height = 400;
    Position = poScreenCenter;
    Color  = (TColor)0x12ddf;
    Caption = "Combo Box with Coloured Items";

    TLabel* pLabel = new TLabel( Form1 );
    pLabel->Parent = Form1;
    pLabel->Top    =  25;
    pLabel->Height =  24;
    pLabel->Width  = 300;
    pLabel->Left   = (Width - pLabel->Width)/2;
    pLabel->Font->Style = pLabel->Font->Style << fsBold;
    pLabel->Font->Size  = 10;
    pLabel->Font->Name  = "Arial";
    pLabel->Font->Color = clWhite;
    pLabel->Caption     = "Select a Movie";

    const int DIM = sizeof(MyItems)/sizeof(MyItems[0]);

    pCOMB = new TComboBox( Form1 );
    pCOMB->Parent  = Form1;
    pCOMB->Top     =  50;
    pCOMB->Height  =  24;
    pCOMB->Width   = 300;
    pCOMB->Left    = (Width - pCOMB->Width)/2;
    pCOMB->Visible = true;   //Default. Added for completeness
    pCOMB->Enabled = true;   //Default. Added for completeness
    pCOMB->Color   = MyColors::clBackground;
    pCOMB->Font->Size  = 10;
	//Set Style to be able to Draw
    pCOMB->Style      = csOwnerDrawVariable;
	//Event and Event Handler
    pCOMB->OnDrawItem = COMB_DrawItem;
    //-------------------------------------

    for ( int index=0; index < DIM; index++ )
    {
        pCOMB->Items->Add( MyItems[index] );
    }
    //TEST
    //ShowMessage( pCOMB->Items->Strings[0] );
}
//--------------------------------------------------------------------

void __fastcall TForm1::COMB_DrawItem( TWinControl *Control,
                                       int Index,
                                       const TRect &Rect,
                                       TOwnerDrawState State )
{
   //Added const to the third agument (const TRect &Rect) to be able
   //to compile

   //Colors array
   TColor Colors[] = { (TColor)0x88ded,   clRed,
                       (TColor)0xcef1,    clGreen,
                        clSilver,         clTeal,
                       (TColor)0xeac11ff, clOlive,
                       (TColor)0xabffed,  clFuchsia,
                       (TColor)0xefac,    (TColor)0xaabbcc,
                       (TColor)0xffadeb,  (TColor)0x1155aa,
                       (TColor)0x33bb22,  (TColor)0x6611f0,
                       (TColor)0xf3f25a,  (TColor)0x347890,
                       (TColor)0xe356ff,  (TColor)0x45fdab  };

    //Get the Dimension of the Colors array:                   
    const int DIM_COLORS = sizeof(Colors)/sizeof(Colors[0]);

    pCOMB->Canvas->FillRect( Rect );

    for ( int i=0; i < DIM_COLORS; i++ )
    {
        if ( Index == i )
        {
           //Set the font color
           pCOMB->Canvas->Font->Color  = Colors[i];

           //Use a brush with the same color as the background
           pCOMB->Canvas->Brush->Color = MyColors::clBackground;
        }
    }

    //Draw the text (String) for each item:
    pCOMB->Canvas->TextOut( Rect.Left + dX + dX_SEP, Rect.Top,
                            pCOMB->Items->Strings[Index] );

    for ( int i=0; i < DIM_COLORS; i++ )
    {
        if ( Index == i )
        {
            //Draw each Box with a different colors
            pCOMB->Canvas->Brush->Color = Colors[i];

            //Draw a Red Box for the item that meets the condition.
            //And draw all other Boxes with a different color
            if ( pCOMB->Items->Strings[i] == pCOMB->Items->Strings[4] )
            {
                pCOMB->Canvas->Brush->Color = clRed;
            }
            else
            {
                pCOMB->Canvas->Brush->Color = (TColor)0x88ded;

            }
            //Draw a Box on the left of each item:
            pCOMB->Canvas->Rectangle( Rect.Left     , Rect.Top,
                                      Rect.Left + dX, Rect.Top + dY);
        }
    }
}
//--------------------------------------------------------------------

ComboBoxColors.h

#ifndef ComboBoxColorsH
#define ComboBoxColorsH
//----------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//----------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall COMB_DrawItem( TWinControl *Control,
                                       int Index,
                                       const TRect &Rect,
                                       TOwnerDrawState State);
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.