属性编辑器
我在学编写组件,在做到属性编辑器的时候遇到了问题
#ifndef MyComponentH
#define MyComponentH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
#include <DesignEditors.hpp>
#include <DesignIntf.hpp>
typedef void __fastcall(__closure*TCountChangedEvent)(TObject * Sender,int & Count);
//---------------------------------------------------------------------------
class TMyGraphic : public TPersistent
{
private:
int FTest;
__published:
__property int Test={read=FTest,write=FTest};
};
class PACKAGE TMyComponent : public TWinControl
{
private:
int FCount;
int __fastcall GetCount();
void __fastcall SetCount(int ACount);
int FPointArray[5];
int __fastcall GetPointArray(int Index);
void __fastcall SetPointArray(int Index,int Value);
TMyGraphic *FGraphics;
TCountChangedEvent FCountChanged;
protected:
public:
__property int PointArray[int Index]={read=GetPointArray,write=SetPointArray};
__fastcall TMyComponent(TComponent* Owner);
__fastcall ~TMyComponent();
__published:
__property int Count={read=GetCount,write=SetCount,default=5};
__property TMyGraphic *Graphics={read=FGraphics,write=FGraphics};
__property TCountChangedEvent OnCountChanged={read=FCountChanged,write=FCountChanged,nodefault};
};
//以上都很顺利,下面是要做属性编辑器时加上的代码,我在想下面这段代码要不要另外写道一个新的单元里
class PACKAGE TMyPropertyEditor : public TPropertyEditor
{
private:
void __fastcall SetValue(String Value);
String __fastcall GetValue();
TPropertyAttributes __fastcall GetAttributes();
void __fastcall Edit();
public:
__fastcall TMyPropertyEditor(const _di_IDesigner ADesigner,int APropCount);
__fastcall ~TMyPropertyEditor();
};
//---------------------------------------------------------------------------
#endif
//cpp文件
#include <vcl.h>
#pragma hdrstop
#include "MyComponent.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(TMyComponent *)
{
new TMyComponent(NULL);
}
//---------------------------------------------------------------------------
__fastcall TMyComponent::TMyComponent(TComponent* Owner)
: TWinControl(Owner)
{
Count=5;
Height=100;
Width=120;
FGraphics=new TMyGraphic;
}
//---------------------------------------------------------------------------
int __fastcall TMyComponent::GetCount()
{
return FCount;
}
//---------------------------------------------------------------------------
void __fastcall TMyComponent::SetCount(int ACount)
{
if(FCount!=ACount)
{
FCount=ACount;
if(FCountChanged!=NULL)
FCountChanged(this,ACount);
}
}
//---------------------------------------------------------------------------
int __fastcall TMyComponent::GetPointArray(int Index)
{
if(Index>=0&&Index<5)
return FPointArray[Index];
else
return -1;
}
//---------------------------------------------------------------------------
void __fastcall TMyComponent::SetPointArray(int Index,int Value)
{
if(Index>=0&&Index<5)
FPointArray[Index]=Value;
}
//---------------------------------------------------------------------------
__fastcall TMyComponent::~TMyComponent()
{
delete FGraphics;
}
//-------------------------------------------------------------
TPropertyAttributes __fastcall TMyPropertyEditor::GetAttributes()
{
return TPropertyAttributes()<<paDialog;
}
//---------------------------------------------------------------------------
void __fastcall TMyPropertyEditor::SetValue(String Value)
{
((TMyComponent *)GetComponent(0))->Graphics->Test=StrToInt(Value);
}
//---------------------------------------------------------------------------
String __fastcall TMyPropertyEditor::GetValue()
{
String tmpS=IntToStr(((TMyComponent *)GetComponent(0))->Graphics->Test);
return tmpS;
}
//---------------------------------------------------------------------------
namespace Mycomponent
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TMyComponent)};
RegisterComponents("Samples", classes, 0);
RegisterPropertyEditor(__typeinfo(TMyGraphic),__classid(TMyComponent),
"Graphics",__classid(TMyPropertyEditor));
}
}
//-------------------------------------------------------------------------
哪位高手把代码考过去给我调一调,谢谢啦!