Discussion:
[Lazarus] Custom control with stepwise size adjustment
gabor via Lazarus
2018-09-19 12:29:16 UTC
Permalink
Hi.
How to properly create a control with step-size adjustment? I mean a
control whose width is a multiple of, eg. 16 (16,32,48...).
In delphi, I simply override TCustomControl.SetBounds method.

procedure TMyControl.SetBounds(aTop, aLeft, aWidth, aHeight: Integer);
begin
aWidth := (aWidth div 16) * 16;
if aWidth = 0 then
aWidth := 16;
aHeight := (aHeight div 16) * 16;
if aHeight = 0 then
aHeight := 16;
inherited SetBounds(aTop, aLeft, aWidth, aHeight);
end;

Unfortunately, the same does not work well with LCL. Especially in
connection with Anchors or in form designer.
So how to deal with it?

Regards, Michał.
--
Vojtěch Čihák via Lazarus
2018-09-19 13:17:03 UTC
Permalink
Try set Constraints - set MinHeight=MaxHeight=your calculated height, and the same for Width.
But it will maybe fool Lazarus autosizing mechanism.
 
V.
______________________________________________________________
> Od: "gabor via Lazarus" <***@lists.lazarus-ide.org>
> Komu: ***@lists.lazarus-ide.org
> Datum: 19.09.2018 14:29
> Předmět: [Lazarus] Custom control with stepwise size adjustment
>
Hi.
How to properly create a control with step-size adjustment? I mean a
control whose width is a multiple of, eg. 16 (16,32,48...).
In delphi, I simply override TCustomControl.SetBounds method.

procedure TMyControl.SetBounds(aTop, aLeft, aWidth, aHeight: Integer);
begin
  aWidth := (aWidth div 16) * 16;
  if aWidth = 0 then
    aWidth := 16;
  aHeight := (aHeight div 16) * 16;
  if aHeight = 0 then
    aHeight := 16;
  inherited SetBounds(aTop, aLeft, aWidth, aHeight);
end;

Unfortunately, the same does not work well with LCL. Especially in
connection with Anchors or in form designer.
So how to deal with it?

Regards, Michał.
--
_______________________________________________
Lazarus mailing list
***@lists.lazarus-ide.org
https://lists.lazarus-ide.org/listinfo/lazarus <https://lists.lazarus-ide.org/listinfo/lazarus>
gabor via Lazarus
2018-09-20 11:33:43 UTC
Permalink
Thanks for the hint but I found a solution. I've moved code to
ChangeBounds method and it looks like it works correctly with Anchors.
There is still a problem with form designer, but I can live with it - I
simply use object inspector instead of using a mouse.

Regards, Michał.


W dniu 2018-09-19 o 15:17, Vojtěch Čihák via Lazarus pisze:
> Try set Constraints - set MinHeight=MaxHeight=your calculated height,
> and the same for Width.
>
> But it will maybe fool Lazarus autosizing mechanism.
>
> V.
>
> ______________________________________________________________
> > Od: "gabor via Lazarus" <***@lists.lazarus-ide.org>
> > Komu: ***@lists.lazarus-ide.org
> > Datum: 19.09.2018 14:29
> > Předmět: [Lazarus] Custom control with stepwise size adjustment
> >
>
> Hi.
> How to properly create a control with step-size adjustment? I mean a
> control whose width is a multiple of, eg. 16 (16,32,48...).
> In delphi, I simply override TCustomControl.SetBounds method.
>
> procedure TMyControl.SetBounds(aTop, aLeft, aWidth, aHeight: Integer);
> begin
>   aWidth := (aWidth div 16) * 16;
>   if aWidth = 0 then
>     aWidth := 16;
>   aHeight := (aHeight div 16) * 16;
>   if aHeight = 0 then
>     aHeight := 16;
>   inherited SetBounds(aTop, aLeft, aWidth, aHeight);
> end;
>
> Unfortunately, the same does not work well with LCL. Especially in
> connection with Anchors or in form designer.
> So how to deal with it?
>
> Regards, Michał.
> --
> _______________________________________________
> Lazarus mailing list
> ***@lists.lazarus-ide.org
> https://lists.lazarus-ide.org/listinfo/lazarus
>
>
--
Juha Manninen via Lazarus
2018-09-20 17:20:23 UTC
Permalink
On Wed, Sep 19, 2018 at 3:29 PM gabor via Lazarus
<***@lists.lazarus-ide.org> wrote:
> How to properly create a control with step-size adjustment?

You can override procedures CalculatePreferredSize and
GetPreferredSize, defined in TControl.
TWinControl has more virtual methods that you should check out.

Juha
--
gabor via Lazarus
2018-09-20 18:37:24 UTC
Permalink
W dniu 2018-09-20 o 19:20, Juha Manninen via Lazarus pisze:
> You can override procedures CalculatePreferredSize and
> GetPreferredSize, defined in TControl.

I tried. Unfortunately, it does not work with Anchors or Align -
preferred values are ignored. But size correction in ChangeBounds
actually works.

> TWinControl has more virtual methods that you should check out.

I noticed. The AutoSize mechanism is bit complicated.

Regards, Michał
--
Loading...