Board index » delphi » Change Size of all Components on the Form

Change Size of all Components on the Form

I would like to create a sizeable Form which changes size,position and
fontheight of each component on the Form in the same ratio as the formsize.

I realized this with the following code but if i put a TTabSet-Component
on the Form Ill get a GPF.
If anyone have a solution or a better code for this problem please send
it to me or to the newsgroup.

Thanks
  Muratore Mario (muratore.ma...@magnet.at)

function Tform1.ChkComponent(component : TComponent) : TControl;
var
  inlist : boolean;
  classname : string[63];
begin
  inlist:= false;
  classname:= TObject(component).classname;
  if classname = 'TGroupBox' then
    begin
      result:= TGroupBox(component);
      inlist:= true;
    end;
  if classname = 'TComboBox' then
    begin
      result:= TComboBox(component);
      inlist:= true;
    end;
  if classname = 'TListBox' then
    begin
      result:= TListBox(component);
      inlist:= true;
    end;
  if classname = 'TRadioButton' then
    begin
      result:= TRadioButton(component);
      inlist:= true;
    end;
  if classname = 'TCheckBox' then
    begin
      result:= TCheckBox(component);
      inlist:= true;
    end;
  if classname = 'TPanel' then
    begin
      result:= TPanel(component);
      inlist:= true;
    end;
  if classname = 'TRadioGroup' then
    begin
      result:= TRadioGroup(component);
      inlist:= true;
    end;
  if classname = 'TMemo' then
    begin
      result:= TMemo(component);
      inlist:= true;
    end;
  if classname = 'TEdit' then
    begin
      result:= TEdit(component);
      inlist:= true;
    end;
  if classname = 'TLabel' then
    begin
      result:= TLabel(component);
      inlist:= true;
    end;
  if classname = 'TButton' then
    begin
      result:= TButton(component);
      inlist:= true;
    end;
  if classname = 'TFileListBox' then
    begin
      result:= TFileListBox(component);
      inlist:= true;
    end;
  if not inlist then
    result:= nil;
end;

procedure Tform1.ChangeSize;
var
  index     : integer;
  factor_x,
  factor_y  : real;
  control   : Tcontrol;

begin
  factor_x:= clientwidth / designwidth;
  factor_y:= clientheight / designheight;
  LockWindowUpdate(handle);
  for index:= componentcount-1 downto 0 do
    begin
      control:= ChkComponent(components[index]);
      if control <> nil then
        with Obj_info^[index], TSizeControl(control) do
          begin
            if Font.Height <> round(schrifthoehe / designheight * saveheight)
then
              schrifthoehe:= round(font.height * designheight / saveheight);
            Font.Height:= round( factor_y * schrifthoehe);
            width:= round(laenge * factor_x);
            left:= round(x1 * factor_x);
            height:= round(hoehe * factor_y);
            top:= round(y1 * factor_y );
            parentfont:= elternschrift;
          end;
    end;
    LockwindowUpdate(0);
    saveheight:= clientheight;
end;

 

Re:Change Size of all Components on the Form


Quote
In article <32592674.7...@magnet.at>, Muratore Mario <muratore.ma...@magnet.at> wrote:
>I would like to create a sizeable Form which changes size,position and
>fontheight of each component on the Form in the same ratio as the formsize.

>I realized this with the following code but if i put a TTabSet-Component
>on the Form Ill get a GPF.
>If anyone have a solution or a better code for this problem please send
>it to me or to the newsgroup.

I always use the following component.
=============================

unit Elastic;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, ExtCtrls;

type
  TElasticPanel = class( TPanel )
  private
     FHorz, FVert: boolean;
     nOldWidth, nOldHeight: integer;
     bResized: boolean;
  protected
     procedure WMSize( var message: TWMSize ); message WM_SIZE;
  public
     nCount: integer;
     constructor Create( AOwner: TComponent ); override;
  published
     property ElasticHorizontal: boolean read FHorz write FHorz default
TRUE;
     property ElasticVertical: boolean read FVert write FVert default
TRUE;
  end;

procedure Register;

implementation

constructor TElasticPanel.Create( AOwner: TComponent );
begin
  inherited Create( AOwner );
  FHorz := TRUE;
  FVert := TRUE;
  nOldWidth := Width;
  nOldHeight := Height;
  bResized := FALSE;
end;

procedure TElasticPanel.WMSize( var message: TWMSize );
var
  bResize: boolean;
  xRatio: real;
  i: integer;
  ctl: TWinControl;
begin
  Inc( nCount );
  if Align = alNone then
     bResize := TRUE
  else
     bResize := bResized;
  if not ( csDesigning in ComponentState ) and bResize then
     begin
        if FHorz then
           begin
              xRatio := Width / nOldWidth;
              for i := 0 to ControlCount - 1 do
                 begin
                    ctl := TWinControl( Controls[i] );
                    ctl.Left := Round( ctl.Left * xRatio );
                    ctl.Width := Round( ctl.Width * xRatio );
                 end;
           end;
        if FVert then
           begin
              xRatio := Height / nOldHeight;
              for i := 0 to ControlCount - 1 do
                 begin
                    ctl := TWinControl( Controls[i] );
                    ctl.Top := Round( ctl.Top * xRatio );
                    ctl.Height := Round( ctl.Height * xRatio );
                 end;
           end;
     end
  else
     begin
        nOldWidth := Width;
        nOldHeight := Height;
     end;
  bResized := TRUE;
  nOldWidth := Width;
  nOldHeight := Height;
end;

procedure Register;
begin
  RegisterComponents('Additional', [TElasticPanel]);
end;

end.

Happy Hacking The Graphical Gnome

----------------------------------------------
| Rob den Braasem    | Voice :-31-79-3531520 |
|                    | Fax   :-31-79-3513561 |
|                    | Mail  : r...@ktibv.nl  |
|                    |-----------------------+---------------------+
| KTI bv             | "I die when it's time for me to die.        |
| P.O. Box 86        | So let me live  my life the way I want to." |
| 2700 AB Zoetermeer |                                             |
| The Netherlands    |   Jimi Hendrix                              |
-------------------------------------------------------------------+

Re:Change Size of all Components on the Form


I would think that just checked to see if it is a TWinControl would suffice or
even just use a try execept loop and ignore invalid casts rather than checking
all these types and adding them to a list.

Tom.

In article <32592674.7...@magnet.at>, Muratore Mario

Quote
<muratore.ma...@magnet.at> wrote:

]I would like to create a sizeable Form which changes size,position and
]fontheight of each component on the Form in the same ratio as the formsize.
]
]I realized this with the following code but if i put a TTabSet-Component
]on the Form Ill get a GPF.
]If anyone have a solution or a better code for this problem please send
]it to me or to the newsgroup.
]
]Thanks
]  Muratore Mario (muratore.ma...@magnet.at)
]
]
<munch>
.
.

Tom.
tho...@dgsys.com
http://web.dgsys.com
http://www2.dgsys.com/~thomas

Other Threads