自然是采用类的聚合
比如:
//---------------------------------------------------------------------------
#ifndef PictureSelectorH
#define PictureSelectorH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
const String Strings[] = {
"handshak.bmp",
"factory.bmp",
"chemical.bmp",
"shipping.bmp",
"finance.bmp"
};
class PACKAGE TUnlPictureSelector : public TWinControl
{
private:
TComboBox* FComboBox;
TImage* FImage;
protected:
void __fastcall ComboBoxClick(TObject* Sender);
virtual void __fastcall CreateWnd();
public:
__fastcall TUnlPictureSelector(TComponent* Owner);
};
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "PictureSelector.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TUnlPictureSelector *)
{
new TUnlPictureSelector(NULL);
}
//---------------------------------------------------------------------------
__fastcall TUnlPictureSelector::TUnlPictureSelector(TComponent* Owner)
: TWinControl(Owner)
{
Width = 240;
Height = 205;
FComboBox = new TComboBox(this);
FComboBox->Left = 0;
FComboBox->Top = 0;
FComboBox->Width = 150;
FComboBox->Style = csDropDownList;
FComboBox->OnClick = ComboBoxClick;
FComboBox->Parent = this;
FImage = new TImage(this);
FImage->Left = 0;
FImage->Top = FComboBox->Height;
FImage->Width = Width;
FImage->Height = Height - FComboBox->Height;
FImage->Parent = this;
}
//---------------------------------------------------------------------------
void __fastcall TUnlPictureSelector::CreateWnd()
{
TWinControl::CreateWnd();
if (!ComponentState.Contains(csDesigning)) {
for (int i=0;i<6;i++)
FComboBox->Items->Add(Strings[i]);
FComboBox->ItemIndex = 0;
ComboBoxClick(0);
}
}
void __fastcall TUnlPictureSelector::ComboBoxClick(TObject* Sender)
{
int index = FComboBox->Items->IndexOf(FComboBox->Text);
FImage->Picture->LoadFromFile(Strings[index]);
}
//---------------------------------------------------------------------------