Why did I get a different component size in the IDE?

Using Delphi 6 Prof.

I created a cone component for separation.

Since I used 8-pixel dividers (width x height), I thought I was creating this component, and when I put it on the form, I only need to install Align - and that’s it.

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; 

But when I use this code (with or without "default"), the result is 140 x 41 pixels in the IDE.

So why is it not a size up to 8 x 8? And also interestingly: the default TBevel value is 50 x 50.

What causes resizing?

+4
source share
2 answers

TLama hits the nail in her comments: for some reason, the designer prevents the components from becoming too small. Strange, although the designer does not set this minimum size (10 x 10), but instead seems to randomly set the size to arbitrary values: 140 x 41 in D6, as indicated by OP, and 100 x 41 here in D7.

Well, since TBevel does not use or publish the AutoSize property, and this property relates to the desired behavior, I decided to stretch its use:

 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; 

This works here in D7, but a more robust implementation might be:

  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; 

And to complete this answer with the call stack, dropping this control in the constructor in the form:

 - Before SetBounds - After SetBounds - Before SetBounds - After SetBounds - Before SetParent - Before SetBounds - After SetBounds - After SetParent - Before SetBounds - After SetBounds - Before SetParent - After SetParent 

But I think you should not rely on this particular order or number of calls: I suspect that this may be different from Delphi versions.

+2
source

Found an article that should explain this. I think you need to override TControl SetBounds.

Read more here http://www.delphidabbler.com/tips/77

0
source

Source: https://habr.com/ru/post/1392959/


All Articles