|
This is a simple aggregate that recreates its subcomponents each time it is
constructed.
.h
//---------------------------------------------------------------------------
#ifndef AggregatePanelH
#define AggregatePanelH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class PACKAGE TAggregatePanel : public TPanel
{
/*
This component class implements a panel
with children. The children are
recreated each time, and their characteristics
are not streamed.
This is a conventional aggregate component
*/
private:
protected:
TButton *myChildButton;
TButton *__fastcall CreateChildButton(TComponent
*theOwner);
public:
__fastcall TAggregatePanel(TComponent* Owner);
void __fastcall ClickChildButton(TObject
*Sender);
__published:
};
//---------------------------------------------------------------------------
#endif
.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "AggregatePanel.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(TAggregatePanel *)
{
new TAggregatePanel(NULL);
}
//---------------------------------------------------------------------------
__fastcall TAggregatePanel::TAggregatePanel(TComponent* Owner)
: TPanel(Owner)
{
Parent = dynamic_cast<TWinControl *>(Owner);
Top = 0;
Left = 0;
Width = 200;
Height = 50;
myChildButton = CreateChildButton(this);
}
//---------------------------------------------------------------------------
TButton *__fastcall TAggregatePanel::CreateChildButton(TComponent *theOwner)
{
// This function is shared with the descendants, to make it
easier to
// alter the owner when creating.
TButton *ChildButton = new TButton(theOwner);
ChildButton->Parent = this;
ChildButton->Caption = "Child Button";
ChildButton->OnClick = ClickChildButton;
return ChildButton;
}
//---------------------------------------------------------------------------
void __fastcall TAggregatePanel::ClickChildButton(TObject *Sender)
{
myChildButton->Caption = FormatDateTime("hh:mm:ss",TDateTime::CurrentDateTime());
}
//---------------------------------------------------------------------------
namespace Aggregatepanel
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TAggregatePanel)};
RegisterComponents("MSC Test", classes,
0);
}
}
//---------------------------------------------------------------------------
|
|