我创建了一个组件 TGridPaintBox,基于 TPaintBox。它基本上是一个具有“网格功能”的画布框架。它不是数据表格,更像是一个象棋盘组件。
在对象资源管理器中,我可以设置某些属性。最重要的是我可以设置网格尺寸(横向/纵向有多少个单元格),还可以设置跟绘制相关的选项,比如单元格是否应该为正方形,奇偶单元格的颜色等。
我的第一个版本将属性直接放在类上,当我更改属性时,设计时绘图会立即更新。由于组件越来越复杂,我想把我的属性整理得更好一些,引入了一些“选项属性”,如绘图选项、行为选项等。引入这些选项后,设计时绘图不再像以前那样即时更新。每次更改属性后,我都必须单击组件才能进行更新。有人能告诉我这是为什么吗?
下面是代码的简化版本。希望能够解释这种行为。 (PS:虽然我使用 Delphi 自 1997 年以来,但这是我第一个组件,如果有人能发现我做得有什么愚蠢的地方,请随便告诉我)
在对象资源管理器中,我可以设置某些属性。最重要的是我可以设置网格尺寸(横向/纵向有多少个单元格),还可以设置跟绘制相关的选项,比如单元格是否应该为正方形,奇偶单元格的颜色等。
我的第一个版本将属性直接放在类上,当我更改属性时,设计时绘图会立即更新。由于组件越来越复杂,我想把我的属性整理得更好一些,引入了一些“选项属性”,如绘图选项、行为选项等。引入这些选项后,设计时绘图不再像以前那样即时更新。每次更改属性后,我都必须单击组件才能进行更新。有人能告诉我这是为什么吗?
下面是代码的简化版本。希望能够解释这种行为。 (PS:虽然我使用 Delphi 自 1997 年以来,但这是我第一个组件,如果有人能发现我做得有什么愚蠢的地方,请随便告诉我)
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.