Caption プロパティを適切に処理したかったので、メイソンの答えは「csSetCaption」を見逃したため機能しません。キャプションとプロパティの両方が文字列型であるため、「デフォルト」に関する彼の提案も機能しません。
以下は、必要な単位です。
動作は次のとおりです。
- 最初に Caption プロパティの値は 'Comments' になります
- ユーザーは、新しい値を設定することにより、設計時にそれをオーバーライドできます
(2.が必要ない場合は、Kenが言及したようにオーバーライドされたLoadedメソッドでCaptionプロパティを割り当てる必要がありますが、それが必要かどうかは質問から明確ではありませんでした. )
これがコードの仕組みです。
文字列プロパティの場合、ストリーミング システムにデフォルトのヒントを与えることはできません。ただし、コンストラクターで設計時の初期値を設定できます。Caption := DefaultCustomSpeedButtonCaption;
Caption プロパティについては、Caption プロパティのデフォルトの割り当ても無効にする必要があります (そうしないと、コンポーネントは「CustomSpeedButton1」のようなキャプションを自動的に取得します)。この行はそれを行います: ControlStyle := ControlStyle - [csSetCaption];
最後に、コンポーネントの登録を別のユニットに分割することをお勧めします。これにより、コンポーネントを IDE に登録する設計時パッケージと、アプリケーションでコンポーネントを使用するための実行時パッケージ (またはパッケージなし) を用意できます。
コンポーネント アイコンがある場合は、それも登録ユニットにロードします (これは設計時にのみ必要になるため)。
Ray Konopka はコンポーネントの作成に関する優れた本を書きましたが、これは今でも非常に有効です:カスタム Delphi 3 コンポーネントの開発
多くの優れた Delphi 本と同様に絶版ですが、彼のサイト で PDF コピーを注文できます。
あなたの CommentTitle と CommentText プロパティが何のためにあるのかわからないので、それらを以下のソースに残しました。
リスト 1: 実際のコンポーネント
unit CustomSpeedButtonUnit;
interface
uses
SysUtils, Classes, Controls, Buttons;
const
DefaultCustomSpeedButtonCaption = 'Comments';
type
TCustomCustomSpeedButton = class(TSpeedButton)
strict private
FCommentText: string;
FCommentTitle: string;
strict protected
procedure SetCommentText(const Value: string); virtual;
procedure SetCommentTitle(const Value: string); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property CommentTitle: string read FCommentTitle write SetCommentTitle;
property CommentText: string read FCommentText write SetCommentText;
end;
TCustomSpeedButton = class(TCustomCustomSpeedButton)
published
// note you cannot use 'default' for string types; 'default' is only valid for ordinal ordinal, pointer or small set type
// [DCC Error] CustomSpeedButtonUnit.pas(29): E2146 Default values must be of ordinal, pointer or small set type
// property Caption default DefaultCustomSpeedButtonCaption;
property CommentTitle;
property CommentText;
end;
implementation
constructor TCustomCustomSpeedButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Caption := DefaultCustomSpeedButtonCaption;
ControlStyle := ControlStyle - [csSetCaption];
end;
destructor TCustomCustomSpeedButton.Destroy;
begin
inherited Destroy;
end;
procedure TCustomCustomSpeedButton.SetCommentText(const Value: string);
begin
FCommentText := Value;
end;
procedure TCustomCustomSpeedButton.SetCommentTitle(const Value: string);
begin
FCommentTitle := Value;
end;
end.
リスト 2: コンポーネントの登録
unit CustomSpeedButtonRegistrationUnit;
interface
procedure Register;
implementation
uses
CustomSpeedButtonUnit;
procedure Register;
begin
RegisterComponents('Standard', [TCustomSpeedButton]);
end;
end.