使用Delphi 6教授
我为分离创建了一个斜角组件.
因为我使用了8像素(宽度x高度)的垫片,我认为我创建了这个组件,当我把它放在Form上时,我只需要设置Align – 这就是全部.
type
TSSpacer = class(TBevel)
public
constructor Create(aOwner: TComponent); override;
published
//property Width default 8;
//property Height default 8;
property Shape default bsSpacer;
end;
constructor TSSpacer.Create(aOwner : TComponent);
begin
inherited Create(aOwner);
Shape := bsSpacer;
Width := 8;
Height := 8;
end;
但是当我使用这段代码(有或没有“默认值”)时,IDE中的结果是140 x 41像素.
那么为什么尺寸不能达到8 x 8呢?而且有趣的是:默认的TBevel是50 x 50.
是什么导致这个大小调整?
最佳答案 TLama在评论中指出:设计师不知何时会阻止组件变得太小.奇怪的是,设计师没有设置这个最小尺寸(10 x 10),而是似乎随机地将尺寸设置为任意值:如D6所述在D6中为140 x 41,在D7中为100 x 41.
好吧,因为TBevel确实使用也没有发布AutoSize属性,并且该属性名称与所希望的行为有关,所以我选择扩展它的用途:
type
TSSPacer = class(TBevel)
protected
procedure SetParent(AParent: TWinControl); override;
public
constructor Create(AOwner: TComponent); override;
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
published
property Shape default bsSpacer;
end;
constructor TSSPacer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Shape := bsSpacer;
end;
procedure TSSPacer.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
if AutoSize then
inherited SetBounds(ALeft, ATop, 8, 8)
else
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
end;
procedure TSSPacer.SetParent(AParent: TWinControl);
begin
AutoSize := (csDesigning in ComponentState) and (Parent = nil) and
(AParent <> nil);
inherited SetParent(AParent);
end;
这在D7中可以使用,但更可靠的实现可能是:
private
FFixDesignSize: Boolean;
procedure TSSPacer.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
if FFixDesignSize then
begin
inherited SetBounds(ALeft, ATop, 8, 8);
FFixDesignSize := False;
end
else
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
end;
procedure TSSPacer.SetParent(AParent: TWinControl);
begin
FFixDesignSize := (csDesigning in ComponentState) and (Parent = nil) and
(AParent <> nil);
inherited SetParent(AParent);
end;
并通过调用堆栈来完成此答案,该控件堆栈在设计器中将此控件放在表单上:
- Before SetBounds
- After SetBounds
- Before SetBounds
- After SetBounds
- Before SetParent
- Before SetBounds
- After SetBounds
- After SetParent
- Before SetBounds
- After SetBounds
- Before SetParent
- After SetParent
但我认为你不应该依赖这个特定的顺序或调用次数:我怀疑它可能在Delphi版本之间有所不同.