0

TCustomControl クラスから派生したカスタム コンポーネントを開発しています。たとえば、TLabel コンポーネントのように、設計時に編集できる新しい TFont ベースのプロパティを追加したいと考えています。基本的に私が望むのは、フォントのさまざまな属性 (名前、サイズ、スタイル、色など) を変更するオプションをユーザーに追加することです。これらの属性を個別のプロパティとして追加する必要はありません。

私の最初の試み:

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* のどちらを使用する必要があるかもわかりません。ユーザーが単一の属性を変更するたびに新しいオブジェクト インスタンスを作成するのは効率が悪いように思えます。これをどのように達成できるか、コードサンプルをいただければ幸いです。

4

1 に答える 1

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();
}
于 2012-04-24T22:56:48.247 に答える