我这里有一段代码。
加两个ListBox1、ComboBox1
//.cpp
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
// Get the location of the Images\Buttons directory
TRegistry* reg = new TRegistry;
reg->RootKey = HKEY_LOCAL_MACHINE;
bool res = reg->OpenKey("SOFTWARE\\Borland\\Borland Shared\\Image Files", false);
if (!res) {
ShowMessage("Registry key not found");
delete reg;
return;
}
String imageDir = reg->ReadString("RootDir") + "\\buttons";
String fileSpec = imageDir + "\\*.bmp";
delete reg;
// Find the files in the Images\Buttons directory.
WIN32_FIND_DATA fd;
HANDLE h = FindFirstFile(fileSpec.c_str(), &fd);
if (h == INVALID_HANDLE_VALUE) {
ShowMessage("No files found");
return;
}
String S = String(fd.cFileName)
+ "=" + imageDir + "\\" + String(fd.cFileName);
ListBox1->Items->Add(S);
ComboBox1->Items->Add(S);
bool done = false;
do {
bool res = FindNextFile(h, &fd);
if (res) {
// Build a string using the TStringList NAME=VALUE mechanism
S = String(fd.cFileName)
+ "=" + imageDir + "\\" + String(fd.cFileName);
ListBox1->Items->Add(S);
ComboBox1->Items->Add(S);
}
done = !res;
} while (!done);
// Close the find handle.
FindClose(h);
// Fill the variable-height list box with values
ListBox2->Items->Add("Height: 20");
ListBox2->Items->Add("Height: 40");
ListBox2->Items->Add("Height: 15");
ListBox2->Items->Add("Height: 30");
ListBox2->Items->Add("Height: 25");
}
void __fastcall TMainForm::ListBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
// Get the name of the file. This will be used in the list box display.
String name = ListBox1->Items->Names[Index];
String filename = ListBox1->Items->Values[name];
TColor backColor;
TColor textColor;
// Set up the colors used to draw the background and the text.
// If the item is selected then use the Windows selection colors.
if (State.Contains(odSelected)) {
backColor = (TColor)GetSysColor(COLOR_HIGHLIGHT);
textColor = (TColor)GetSysColor(COLOR_HIGHLIGHTTEXT);
}
else {
backColor = (TColor)GetSysColor(COLOR_WINDOW);
textColor = (TColor)GetSysColor(COLOR_WINDOWTEXT);
}
// Bold the odd-numbered items just to illustrate.
if (Index % 2 == 1)
ListBox1->Canvas->Font->Style = TFontStyles() << fsBold;
else
ListBox1->Canvas->Font->Style = TFontStyles();
// Fill the drawing rect with the background color.
ListBox1->Canvas->Brush->Color = backColor;
ListBox1->Canvas->FillRect(Rect);
// Get the image.
Graphics::TBitmap* bm = new Graphics::TBitmap;
bm->LoadFromFile(filename);