Properties C++;生成器XE-如何实现TFont属性

Properties C++;生成器XE-如何实现TFont属性,properties,fonts,custom-component,c++builder-xe,Properties,Fonts,Custom Component,C++builder Xe,我正在开发从TCustomControl类派生的自定义组件。我想添加新的基于TFont的属性,这些属性可以在设计时进行编辑,例如在TLabel组件中。基本上,我想要的是添加用户选项来更改字体的各种属性(名称、大小、样式、颜色等),而无需将这些属性作为单独的属性添加 我的第一次尝试: class PACKAGE MyControl : public TCustomControl { ... __published: __property TFont LegendFont = {read=G

我正在开发从TCustomControl类派生的自定义组件。我想添加新的基于TFont的属性,这些属性可以在设计时进行编辑,例如在TLabel组件中。基本上,我想要的是添加用户选项来更改字体的各种属性(名称、大小、样式、颜色等),而无需将这些属性作为单独的属性添加

我的第一次尝试:

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

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

编译器返回错误“E2459 Delphi样式的类必须使用运算符new构造”。我也不知道应该使用数据类型TFont还是TFont*。在我看来,每次用户更改单个属性时创建新的对象实例效率很低。我希望代码示例能够实现这一点。

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();
}

必须使用
new
操作符在堆上分配从
TObject
派生的类。您试图在不使用任何指针的情况下使用
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();
}