2012-04-23 2 views
0

TCustomControl 클래스에서 파생 된 사용자 지정 구성 요소를 개발 중입니다. 나는 TLabel 구성 요소에서와 같이 디자인 타임에 편집 할 수있는 새로운 TFont 기반 속성을 추가하고 싶습니다. 기본적으로 내가 원하는 것은 별도의 속성으로 이러한 각 속성을 추가하지 않고 글꼴 (이름, 크기, 스타일, 색상 등)의 다양한 속성을 변경하는 옵션을 사용자에게 추가하는 것입니다.C++ Builder XE - TFont 속성을 구현하는 방법

내 첫 번째 시도 :

class PACKAGE MyControl : public TCustomControl 
{ 
... 
__published: 
    __property TFont LegendFont = {read=GetLegendFont,write=SetLegendFont}; 

protected: 
    TFont __fastcall GetLegendFont(); 
    void __fastcall SetLegendFont(TFont value); 
... 
} 

컴파일러 오류 "E2459 델파이 스타일 클래스는 new 연산자를 사용하여 구성되어야한다"를 반환합니다. 또한 TFont 또는 TFont * 데이터 형식을 사용해야하는지 여부도 알 수 없습니다. 사용자가 단일 속성을 변경할 때마다 새로운 객체 인스턴스를 만드는 것은 비효율적 인 것처럼 보입니다. 이 코드 샘플을 어떻게 완성 할 수 있을지 고맙게 생각합니다.

답변

3

TObject에서 파생 된 클래스는 new 연산자를 사용하여 힙에 할당해야합니다. 어떤 포인터도 사용하지 않고 TFont을 사용하려고합니다. 작동하지 않습니다. 다음과 같이 속성을 구현해야합니다.

class PACKAGE MyControl : public TCustomControl 
{ 
... 
__published: 
    __property TFont* LegendFont = {read=FLegendFont,write=SetLegendFont}; 

public: 
    __fastcall MyControl(TComponent *Owner); 
    __fastcall ~MyControl(); 

protected: 
    TFont* FLegendFont; 
    void __fastcall SetLegendFont(TFont* value); 
    void __fastcall LegendFontChanged(TObject* Sender); 
... 
} 

__fastcall MyControl::MyControl(TComponent *Owner) 
    : TCustomControl(Owner) 
{ 
    FLegendFont = new TFont; 
    FLegendFont->OnChange = LegendFontChanged; 
} 

__fastcall MyControl::~MyControl() 
{ 
    delete FLegendFont; 
} 

void __fastcall MyControl::SetLegendFont(TFont* value) 
{ 
    FLegendFont->Assign(value); 
} 

void __fastcall MyControl::LegendFontChanged(TObject* Sender); 
{ 
    Invalidate(); 
} 
관련 문제