12

TPaintBox に基づいて、コンポーネント TGridPaintBox を作成しました。基本的には、「グリッド機能」が追加されたペイントボックスです。これはデータ グリッドではありません。チェス盤のコンポーネントに似ています。

オブジェクト エクスプローラーでは、特定のプロパティを設定できます。最も重要なのは、グリッドの寸法 (縦横のセルの数) を設定できるだけでなく、描画に関するオプションも設定できることです。セルを正方形にするかどうか、奇数/偶数セルの色など。

このコンポーネントの最初のバージョンでは、クラスに直接プロパティがあり、プロパティを変更すると、設計時の図面がすぐに更新されました。コンポーネントが大きくなるにつれて、プロパティをもう少し整理したいと思い、描画オプション、動作オプションなどの「オプション プロパティ」をいくつか導入しました。これを導入した後、設計時の描画は以前のように更新されなくなりました。プロパティを変更した後、コンポーネントをクリックして更新する必要があります。なぜこれが起こるのか誰か教えてもらえますか?

これは、コードの簡略化されたバージョンです。私はそれが動作を説明することを願っています:

(PS: 1997 年から Delphi を使用していますが、これは私の最初のコンポーネントです。そのため、私のやり方で愚かなことを誰かが見つけたら、遠慮なく教えてください)

unit GridPaintBox;

interface

type
  TGridDrawOption = (gdoSquareCells,gdoCenterCells,gdoDrawCellEdges,gdoDrawFocus);
  TGridDrawOptions = set of TGridDrawOption;

  TGridOptions = class(TPersistent)
  private
    FCellsX : integer;
    FCellsY : integer;
    FDrawOptions : TGridDrawOptions;
  public
    constructor Create(aGridPaintBox : TGridPaintBox);
    procedure Assign(Source : TPersistent); override;
  published
    property CellsX : integer read FCellsX write FCellsX;
    property CellsY : integer read FCellsY write FCellsY;
    property DrawOptions : TGridDrawOptions read FDrawOptions write FDrawOptions;
  end;

  TGridPaintBox = class(TPaintBox)
  private
    FGridOptions : TGridOptions;
    FFocusedX,
    FFocusedY : integer;
    FOnFocusChanged: TNotifyEvent; 
    procedure CalculateSizeAndPosition; 
    procedure DrawCell(X,Y : integer);
    procedure DrawCells;
    procedure SetGridOptions(const Value: TGridOptions);
  protected
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  public
    constructor Create(aOwner : TComponent); override;
    destructor Destroy; override;
    procedure Paint; override;
    procedure SetFocus(X,Y : integer);
  published
    property OnFocusChanged : TNotifyEvent read FOnFocusChanged write FOnFocusChanged;
    property Options : TGridOptions read FGridOptions write SetGridOptions;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TGridPaintBox]);
end;

procedure TGridPaintBox.CalculateSizeAndPosition;
begin
  <...>
end;

constructor TGridPaintBox.Create(aOwner: TComponent);
begin
  inherited;
  FGridOptions := TGridOptions.Create(self);
end;

procedure TGridPaintBox.DrawCell(X, Y: integer);
begin
  <...>
end;

procedure TGridPaintBox.DrawCells;
var
  X,Y : integer;
begin
  CalculateSizeAndPosition;

  for Y := 0 to FGridOptions.CellsY-1 do
    for X := 0 to FGridOptions.CellsX-1 do
      DrawCell(X,Y);
end;

procedure TGridPaintBox.Paint;
begin
  Canvas.Font := Font;
  Canvas.Brush.Color := Color;
  Canvas.Brush.Style := bsSolid;
  Canvas.FillRect(Rect(0,0,Width,Height));
  DrawCells;
  if Assigned(OnPaint) then
    OnPaint(Self); 
end;

procedure TGridPaintBox.SetGridOptions(const Value: TGridOptions);
begin
  FGridOptions.Assign(Value);
  invalidate;
end;

procedure TGridPaintBox.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  SetFocus(PixelToCellX(X),PixelToCellY(Y));
  inherited;
end;

procedure TGridPaintBox.SetFocus(X, Y: integer);
begin
  if (FocusedX=X) and (FocusedY=Y) then 
    exit;

  FFocusedX := X;
  FFocusedY := Y;

  if assigned(OnFocusChanged) then
    OnFocusChanged(self);

  invalidate;
end;

constructor TGridOptions.Create(aGridPaintBox : TGridPaintBox);
begin
  FCellsX := 20;
  FCellsY := 8;
  FDrawOptions := [gdoSquareCells,gdoCenterCells,gdoDrawCellEdges];
end;

procedure TGridOptions.Assign(Source : TPersistent);
begin
  if Source is TGridOptions then
  begin
    FCellsX := TGridOptions(Source).CellsX;
    FCellsY := TGridOptions(Source).CellsY;
    FDrawOptions := TGridOptions(Source).DrawOptions;
  end
  else
    inherited;
end;

end.
4

1 に答える 1

13

これは、属するコントロールを無効にするオプション セットのセッターがないために発生します。フォーム デザイナでクリックすると無効化するコントロールが呼び出されますが、オプション セッターなどで独自に処理する必要があります。したがって、直接所有者クラスのインスタンスにアクセスしやすくするためにオプション所有者を保存し、オプション セッターでこの所有者を強制的に再描画させます。

type
  TGridPaintBox = class;
  TGridDrawOption = (gdoSquareCells, gdoCenterCells, gdoDrawCellEdges, gdoDrawFocus);
  TGridDrawOptions = set of TGridDrawOption;
  TGridOptions = class(TPersistent)
  private
    FOwner: TGridPaintBox;
    FCellsX: Integer;
    FCellsY: Integer;
    FDrawOptions: TGridDrawOptions;
    procedure SetCellsX(AValue: Integer);
    procedure SetCellsY(AValue: Integer);
    procedure SetDrawOptions(const AValue: TGridDrawOptions);
  public
    constructor Create(AOwner: TGridPaintBox);
    procedure Assign(ASource: TPersistent); override;
  published
    property CellsX: Integer read FCellsX write SetCellsX;
    property CellsY: Integer read FCellsY write SetCellsY;
    property DrawOptions: TGridDrawOptions read FDrawOptions write SetDrawOptions;
  end;

implementation

constructor TGridOptions.Create(AOwner: TGridPaintBox);
begin
  FOwner := AOwner;
  FCellsX := 20;
  FCellsY := 8;
  FDrawOptions := [gdoSquareCells, gdoCenterCells, gdoDrawCellEdges];
end;

procedure TGridOptions.SetCellsX(AValue: Integer);
begin
  if FCellsX <> AValue then
  begin
    FCellsX := AValue;
    FOwner.Invalidate;
  end;
end;

procedure TGridOptions.SetCellsY(AValue: Integer);
begin
  if FCellsY <> AValue then
  begin
    FCellsY := AValue;
    FOwner.Invalidate;
  end;
end;

procedure TGridOptions.SetDrawOptions(const AValue: TGridDrawOptions);
begin
  if FDrawOptions <> AValue then
  begin
    FDrawOptions := AValue;
    FOwner.Invalidate;
  end;
end;

その他の注意事項:

Colorペイント ボックスのパブリッシュされたプロパティと、インスタンスやイベントFontなどのイベントを明示的に必要としない場合は、コントロールをではなくOnPaintから派生させます。どの物件やイベントを公開するかは、ご自身でお選びいただけます。TGraphicControlTPaintBox

于 2012-06-10T19:49:52.117 に答える