Combo Box With Bitmaps

Introduction

 This exercise shows how to retrieve the color of a pixel and create a TColor object:

Header File

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TComboBox *ComboBox1;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
    void __fastcall ComboBox1DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State);
    void __fastcall ComboBox1MeasureItem(TWinControl *Control, int Index,
          int &Height);
private:
    Graphics::TBitmap *bmpTitles,   *bmpRecords, *bmpOptions,
                      *bmpKeyboard, *bmpInsert,  *bmpDans,
                      *bmpWorld,    *bmpBand,    *bmpCategory;	// User declarations
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

 

Source File

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    bmpTitles   = new Graphics::TBitmap;
    bmpTitles->LoadFromFile("C:\\BcbFoundResources\\Bitmaps\\Titles.bmp");
    bmpRecords  = new Graphics::TBitmap;
    bmpRecords->LoadFromFile("C:\\BcbFoundResources\\Bitmaps\\Records.bmp");
    bmpOptions  = new Graphics::TBitmap;
    bmpOptions->LoadFromFile("C:\\BcbFoundResources\\Bitmaps\\Options.bmp");
    bmpKeyboard = new Graphics::TBitmap;
    bmpKeyboard->LoadFromFile("C:\\BcbFoundResources\\Bitmaps\\Keyboard.bmp");
    bmpInsert   = new Graphics::TBitmap;
    bmpInsert->LoadFromFile("C:\\BcbFoundResources\\Bitmaps\\Insert.bmp");
    bmpDans     = new Graphics::TBitmap;
    bmpDans->LoadFromFile("C:\\BcbFoundResources\\Bitmaps\\Dans.bmp");
    bmpWorld    = new Graphics::TBitmap;
    bmpWorld->LoadFromFile("C:\\BcbFoundResources\\Bitmaps\\World.bmp");
    bmpBand     = new Graphics::TBitmap;
    bmpBand->LoadFromFile("C:\\BcbFoundResources\\Bitmaps\\Band.bmp");
    bmpCategory = new Graphics::TBitmap;
    bmpCategory->LoadFromFile("C:\\BcbFoundResources\\Bitmaps\\Category.bmp");

    ComboBox1->Items->AddObject("Plate", bmpTitles);
    ComboBox1->Items->AddObject("Books", bmpRecords);
    ComboBox1->Items->AddObject("Device Options", bmpOptions);
    ComboBox1->Items->AddObject("Trial by Error", bmpKeyboard);
    ComboBox1->Items->AddObject("Inertion", bmpInsert);
    ComboBox1->Items->AddObject("Interior", bmpDans);
    ComboBox1->Items->AddObject("Universal", bmpWorld);
    ComboBox1->Items->AddObject("Nkute Mendjang", bmpBand);
    ComboBox1->Items->AddObject("Categories", bmpCategory);
    ComboBox1->ItemIndex = 2;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    delete bmpTitles;
    delete bmpRecords;
    delete bmpOptions;
    delete bmpKeyboard;
    delete bmpInsert;
    delete bmpDans;
    delete bmpWorld;
    delete bmpBand;
    delete bmpCategory;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control, int Index,
      TRect &Rect, TOwnerDrawState State)
{
    Graphics::TBitmap *Bitmap = new Graphics::TBitmap;
    Integer Offset;

    ComboBox1->Canvas->FillRect(Rect);
    Bitmap = reinterpret_cast<Graphics::TBitmap*>(ComboBox1->Items->Objects[Index]);

    if( Bitmap != NULL )
    {
        ComboBox1->Canvas->BrushCopy(Bounds(Rect.Left+2, Rect.Top, Bitmap->Width, Bitmap->Height),
                                                  Bitmap,
                                                  Bounds(0, 0, Bitmap->Width, Bitmap->Height),
                                                  clRed);
        Offset = Bitmap->Width + 8;
    }

    ComboBox1->Canvas->TextOut(Rect.Left + Offset, Rect.Top+2, ComboBox1->Items->Strings[Index]);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1MeasureItem(TWinControl *Control,
      int Index, int &Height)
{
    Height = 20;
}
//---------------------------------------------------------------------------