Recent

Author Topic: How best to set this up - suggestions welcome!  (Read 751 times)

Handoko

  • Hero Member
  • *****
  • Posts: 4214
  • My goal: build my own game engine using Lazarus
Re: How best to set this up - suggestions welcome!
« Reply #15 on: April 07, 2021, 04:27:00 am »
Good to know you can solve it.

In addition of the previous demo, if you want to resize both columns:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, StdCtrls, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Label1: TLabel;
  16.     Label2: TLabel;
  17.     Label3: TLabel;
  18.     Shape1: TShape;
  19.     procedure FormCreate(Sender: TObject);
  20.     procedure FormResize(Sender: TObject);
  21.   private
  22.     procedure AutoCenterAll;
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. begin
  36.   Constraints.MinWidth  := 320;
  37.   Constraints.MinHeight := 240;
  38.   Label1.AutoSize       := False;
  39.   Label1.Alignment      := taCenter;
  40.   Label1.Left           := 0;
  41.   Label2.AutoSize       := False;
  42.   Label2.Alignment      := taCenter;
  43.   Label2.Left           := 0;
  44.   Label3.AutoSize       := False;
  45.   Label3.Alignment      := taCenter;
  46. end;
  47.  
  48. procedure TForm1.FormResize(Sender: TObject);
  49. begin
  50.   AutoCenterAll;
  51. end;
  52.  
  53. procedure TForm1.AutoCenterAll;
  54. begin
  55.   Shape1.Left  := (Form1.Width div 2) - (Shape1.Width div 2);
  56.   Label1.Width := Form1.Width;
  57.   Label2.Width := Shape1.Left;
  58.   Label3.Left  := Shape1.Left + Shape1.Width;
  59.   Label3.Width := Form1.Width - Label3.Left;
  60. end;
  61.  
  62. end.

The code above will make both columns the same size. If you want one is bigger than the other, you can add percentage into the calculation.
« Last Edit: April 07, 2021, 04:30:36 am by Handoko »

egsuh

  • Hero Member
  • *****
  • Posts: 725
Re: How best to set this up - suggestions welcome!
« Reply #16 on: April 07, 2021, 04:59:32 am »
It's good to hear that good solution is found. I'm just writing this because I have similar problems as well, and thinking many ways. If the spaces between labels don't have to be transparent, I think easiest way is to set FlowPanel's size to fit exactly necessary size, and position it at the center of the form using Anchors --- do not need to calculate positions.

engkin

  • Hero Member
  • *****
  • Posts: 2702
Re: How best to set this up - suggestions welcome!
« Reply #17 on: April 07, 2021, 06:02:29 am »
I ended up creating a procedure to position the passed control based on a passed in location so inside the procedure if I pass in location 0 then that puts in one place and if I pass in position 1 that puts it in a different place.  Just used a case statement:
Code: Pascal  [Select][+][-]
  1.     case Location of
  2.       0 : begin         // Category 0 location
  3.             control.Left:=((cX div 2) - (control.width div 2));
  4.             control.Top := 450;
  5.           end;
  6.  
  7.       1 : begin         // Category 1 location
  8.             control.Left:=(cX + (cX div 2) - (control.width div 2));
  9.             control.Top := 450;
  10.           end;
cX is the center of the screen so with this example position 0 is column 1 and position 1 is column 2 - seems to work.

This code is similar to:
Code: Pascal  [Select][+][-]
  1.       control.Left:=(Location*cX + (cX div 2) - (control.width div 2));
  2.       control.Top := 450;

and maybe:

Code: Pascal  [Select][+][-]
  1.       control.Left:=Location*cX + (cX - control.width) div 2;
  2.       control.Top := 450;

As in:
Code: Pascal  [Select][+][-]
  1. var
  2.   Lbls:array of TLabel;
  3.   i, cX: integer;
  4. begin
  5.   Lbls := [Label1, Label2, Label3, Label4, Label5, Label6];
  6.   cX := Width div 2;
  7.   for i := 0 to High(Lbls) do begin
  8.     Lbls[i].Left := cX*(i and 1) + (cX-Lbls[i].Width) div 2;
  9.     Lbls[i].Top  := 450+Lbls[i].Height*(i div 2);
  10.   end;
  11. end;


 

TinyPortal © 2005-2018