下面是我的代码:Unit1.h//---------------------------------------------------------------------------#ifndef Unit1H#define Unit1H//---------------------------------------------------------------------------#include <Classes.hpp>#include <Controls.hpp>#include <StdCtrls.hpp>#include <Forms.hpp>#include <Dialogs.hpp>#include <CheckLst.hpp>//---------------------------------------------------------------------------class TForm1 : public TForm{__published: // IDE-managed Components TListBox *ListBox1;
TButton *Button1;
TOpenDialog *OpenDialog1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall ListBox1MeasureItem(TWinControl *Control, int Index, int &Height);
void __fastcall ListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, TOwnerDrawState State);private: // User declarationspublic: // User declarations __fastcall TForm1(TComponent* Owner);};//---------------------------------------------------------------------------extern PACKAGE TForm1 *Form1;//---------------------------------------------------------------------------#endifunit1.cpp//---------------------------------------------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit1.h"//---------------------------------------------------------------------------#pragma package(smart_init)#pragma link "cxContainer"#pragma link "cxControls"#pragma link "cxListBox"#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){ if( OpenDialog1->Execute() ) { ListBox1->Items->LoadFromFile( OpenDialog1->FileName );
}}//---------------------------------------------------------------------------void __fastcall TForm1::ListBox1MeasureItem(TWinControl *Control, int Index, int &Height){ Height = ListBox1->ItemHeight + Index%10;}//---------------------------------------------------------------------------void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, TOwnerDrawState State){ if(State.Contains(odSelected)) { ListBox1->Canvas->Brush->Color = clNavy;
} else
{ if( Index%2 == 0 ) ListBox1->Canvas->Brush->Color = clSilver;
else
ListBox1->Canvas->Brush->Color = clWindow;
} ListBox1->Canvas->FillRect(Rect);
ListBox1->Canvas->TextOut(Rect.Left+2, Rect.Top+1, ListBox1->Items->Strings[Index]);}//---------------------------------------------------------------------------