2

これは非常に単純なはずですが、私が望む正確な答えが見つかりません。TSpeedButton に基づくカスタム Delphi コントロールがあります。SpeedButton の Caption プロパティを常に 'Comments' にしたいが、実行時に設定したくない コンポーネント自体に設定して、フォームに配置したときに既にこれが取り込まれているようにしたい文章。ボタンの高さと幅も設定したいのですが、これを行う方法はキャプションの設定と同じになると思います。

完全を期すために、コンポーネント コードを次に示します。

unit CustomSpeedButton;

interface

uses
  SysUtils, Classes, Controls, Buttons;

type
  TCustomSpeedButton = class(TSpeedButton)
  private
    FCommentText: string;
    FCommentTitle: string;

    procedure SetCommentText(const Value: string);
    procedure SetCommentTitle(const Value: string);

    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }

  published
    { Published declarations }
    property CommentTitle: string read FCommentTitle write SetCommentTitle;
    property CommentText: string read FCommentText write SetCommentText;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Standard', [TCustomSpeedButton]);
end;

{ TCustomSpeedButton }

procedure TCustomSpeedButton.SetCommentText(const Value: string);
begin
  FCommentText := Value;
end;

procedure TCustomSpeedButton.SetCommentTitle(const Value: string);
begin
  FCommentTitle := Value;
end;

end.
4

3 に答える 3

3

Caption プロパティを適切に処理したかったので、メイソンの答えは「csSetCaption」を見逃したため機能しません。キャプションとプロパティの両方が文字列型であるため、「デフォルト」に関する彼の提案も機能しません。

以下は、必要な単位です。

動作は次のとおりです。

  1. 最初に Caption プロパティの値は 'Comments' になります
  2. ユーザーは、新しい値を設定することにより、設計時にそれをオーバーライドできます

(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.
于 2009-08-28T09:23:55.863 に答える
2

コンポーネントのコンストラクターで元の値を設定する必要があります。

編集:ControlStyle := ControlStyle - [csSetCaption];コンストラクターに add を追加することもできます。

于 2009-08-27T20:00:30.377 に答える
1

@Etherman: デフォルト値の文字列プロパティが必要で、それを DFM に保存したい場合 (空白であっても)、自分で行う必要があります。幸いなことに、Delphi にはそれを行う方法が用意されています。このコードを見てください:

type
  TMyComp = class(TControl)
  private
    FMyStringProperty: string;
    procedure WriteMyStringProperty(Writer: TWriter);
    procedure DefineProperties(Filer: TFiler);  override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property MyStringProperty: string read FMyStringProperty write FMyStringProperty stored False;
  end;


implementation


constructor TMyComp.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FMyStringProperty := 'my default value';
end;

procedure TMyComp.WriteMyStringProperty(Writer: TWriter);
begin
  Writer.WriteString(FMyStringProperty);
end;

procedure TMyComp.DefineProperties(Filer: TFiler);
begin
  inherited DefineProperties(Filer);
  Filer.DefineProperty('MyStringProperty', nil, WriteMyStringProperty, True);
end;
于 2014-09-05T19:23:46.470 に答える