如何在调整屏幕大小后更改运行时组件的对齐方式

问题描述 投票:-1回答:2

我在Google中进行了大量搜索,以获取关于如何解决当前问题的更清晰的信息,但是我将组件的可伸缩性作为最佳答案,但这还不是我的问题。

所以,长话短说:我想在用户调整窗口大小后重新调整表单上的组件,从SQL查询中以动态方式填充表单,这是构造函数代码:

procedure TForm2.MakeWindow;
var
  dummyMaskedit, dummyEdit: TEdit;
  dummyMemo: TMemo;
  dummyCombobox: TComboBox;
  dummyLabel: TLabel;
  dummyLBox: TListBox;
  dummybutton: TButton;
  i, f: integer;
  buffer, workarea: double;
begin
  FDQDB.Close;
  FDQDB.Open('SELECT * FROM Defs WHERE active = 1');

  i := 0;
  f := 1;
  buffer := Layout1.Width;
  workarea := Layout1.Width;
  SetLength(aMasks, 0);
  while not FDQDB.Eof do
  begin
    case AnsiIndexStr(FDQDB.FieldByName('comptype').AsString,
      ['tedit', 'tcombobox', 'tmaskedit', 'tlistbox']) of
      0: // TEdit
        begin
          dummyEdit := TEdit.Create(self);
          dummyEdit.Parent := Form2.Layout1;
          dummyEdit.Width := FDQDB.FieldByName('size').AsInteger;
          if buffer - dummyEdit.Width >= 0 then
          begin
            buffer := buffer - dummyEdit.Width - 8;
            dummyEdit.Position.Y := Panel2.Position.Y + 22 + (45 * i);
            dummyEdit.Position.X := workarea - buffer - dummyEdit.Width + 5;
          end
          else
          begin
            inc(i);
            buffer := workarea;
            buffer := buffer - dummyEdit.Width - 8;
            dummyEdit.Position.Y := Panel2.Position.Y + 22 + (45 * i);
            dummyEdit.Position.X := workarea - buffer - dummyEdit.Width + 5;
          end;
          dummyEdit.Name := 'field' + IntToStr(f);
          inc(f);

          dummyLabel := TLabel.Create(self);
          with dummyLabel do
          begin
            Parent := dummyEdit.Parent;
            Text := FDQDB.FieldByName('Descricao').AsString;
            Position.Y := dummyEdit.Position.Y - 17;
            Position.X := dummyEdit.Position.X;
          end;
        end;

      1: // TComboBox
        begin
          dummyCombobox := TComboBox.Create(self);
          dummyCombobox.Parent := Form2.Layout1;
          dummyCombobox.Width := FDQDB.FieldByName('size').AsInteger;
          if buffer - dummyCombobox.Width >= 0 then
          begin
            buffer := buffer - dummyCombobox.Width - 8;
            dummyCombobox.Position.Y := Panel2.Position.Y + 22 + (45 * i);
            dummyCombobox.Position.X := workarea - buffer -
              dummyCombobox.Width + 5;
          end
          else
          begin
            inc(i);
            buffer := workarea;
            buffer := buffer - dummyCombobox.Width - 8;
            dummyCombobox.Position.Y := Panel2.Position.Y + 22 + (45 * i);
            dummyCombobox.Position.X := workarea - buffer -
              dummyCombobox.Width + 5;
          end;
          dummyCombobox.Name := 'field' + IntToStr(f);
          dummyCombobox.Items.Delimiter := '|';
          dummyCombobox.Items.StrictDelimiter := true;
          dummyCombobox.Items.DelimitedText :=
            AnsiUpperCase(FDQDB.FieldByName('combovalue').AsString);
          inc(f);

          dummyLabel := TLabel.Create(self);
          with dummyLabel do
          begin
            Parent := dummyCombobox.Parent;
            Text := FDQDB.FieldByName('Descricao').AsString;
            Position.Y := dummyCombobox.Position.Y - 17;
            Position.X := dummyCombobox.Position.X;
          end;
        end;

      2: // TMaskEdit
        begin
          dummyMaskedit := TEdit.Create(self);
          dummyMaskedit.Parent := Form2.Layout1;
          dummyMaskedit.Width := FDQDB.FieldByName('size').AsInteger;
          if buffer - dummyMaskedit.Width >= 0 then
          begin
            buffer := buffer - dummyMaskedit.Width - 8;
            dummyMaskedit.Position.Y := Panel2.Position.Y + 22 + (45 * i);
            dummyMaskedit.Position.X := workarea - buffer -
              dummyMaskedit.Width + 5;
          end
          else
          begin
            inc(i);
            buffer := workarea;
            buffer := buffer - dummyMaskedit.Width - 8;
            dummyMaskedit.Position.Y := Panel2.Position.Y + 22 + (45 * i);
            dummyMaskedit.Position.X := workarea - buffer -
              dummyMaskedit.Width + 5;
          end;
          dummyMaskedit.Name := 'field' + IntToStr(f);
          inc(f);

          SetLength(aMasks, length(aMasks) + 1);
          Masks.field := dummyMaskedit.Name;
          Masks.mask := FDQDB.FieldByName('mask').AsString;
          aMasks[length(aMasks) - 1] := Masks;

          dummyMaskedit.OnExit := MaskTextExit;

          dummyLabel := TLabel.Create(self);
          with dummyLabel do
          begin
            Parent := dummyMaskedit.Parent;
            Text := FDQDB.FieldByName('Descricao').AsString;
            Position.Y := dummyMaskedit.Position.Y - 17;
            Position.X := dummyMaskedit.Position.X;
          end;
        end;

      3: // TListBox
        begin
          dummyLBox := TListBox.Create(self);
          dummyLBox.Parent := Form2.Layout1;
          dummyLBox.Width := FDQDB.FieldByName('size').AsInteger;
          inc(i);
          buffer := workarea;
          if buffer - dummyLBox.Width >= 0 then
          begin
            buffer := buffer - dummyLBox.Width - 8;
            dummyLBox.Position.Y := Panel2.Position.Y + 22 + (45 * i);
            dummyLBox.Position.X := workarea - buffer - dummyLBox.Width + 5;
          end
          else
          begin
            inc(i);
            buffer := workarea;
            buffer := buffer - dummyLBox.Width - 8;
            dummyLBox.Position.Y := Panel2.Position.Y + 22 + (45 * i);
            dummyLBox.Position.X := workarea - buffer - dummyLBox.Width + 5;
          end;
          dummyLBox.Name := 'field' + IntToStr(f);
          inc(f);

          SetLength(aMasks, length(aMasks) + 1);
          Masks.field := dummyLBox.Name;
          Masks.mask := FDQDB.FieldByName('mask').AsString;
          aMasks[length(aMasks) - 1] := Masks;

          dummyLabel := TLabel.Create(self);
          with dummyLabel do
          begin
            Parent := dummyLBox.Parent;
            Text := FDQDB.FieldByName('Descricao').AsString;
            Position.Y := dummyLBox.Position.Y - 17;
            Position.X := dummyLBox.Position.X;
          end;

          dummybutton := TButton.Create(self);
          with dummybutton do
          begin
            Parent := dummyLBox.Parent;
            Text := '+';
            Width := 20;
            Position.X := dummyLBox.Width + dummyLBox.Position.X + 3;
            Position.Y := dummyLBox.Position.Y;
            Name := 'ba' + dummyLBox.Name;
            OnClick := ButtonsAddClick;
          end;

          dummybutton := TButton.Create(self);
          with dummybutton do
          begin
            Parent := dummyLBox.Parent;
            Text := '-';
            Width := 20;
            Position.X := dummyLBox.Width + dummyLBox.Position.X + 3;
            Position.Y := dummyLBox.Position.Y + 28;
            Name := 'br' + dummyLBox.Name;
            OnClick := ButtonsRemClick;
          end;
        end;
    end;
    FDQDB.Next;
  end;
  FDQDB.Close;
end;

这在应用程序的窗口状态下工作得很好,此代码被应用于表单的onCreate事件:

procedure TForm2.FormCreate(Sender: TObject);
begin
  FDCDB.Params.Database := ExtractFilePath(ParamStr(0)) + 'window.db';
  MakeWindow;
end;

第一种形式,即应用程序的主要形式,是召唤具有运行时内置组件的新形式的形式:

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  Application.CreateForm(TForm2, Form2);
  Form2.Layout1.parent := Self.Layout1.Parent;
  Form2.Layout1.Width := Self.Layout1.Width;
end;

第二种形式与第一种形式一样具有TLayout。然后,当我创建表单时,将第二个表单的TLayout带到第一个表单。到目前为止一切顺利,当以窗口模式启动应用程序时,它可以正常工作。但是,当我将form1置于全屏模式时,然后单击创建form2的按钮时,组件将保持与窗口模式下相同的位置。我尝试将工作区变量更改为这些:

Screen.width, //components behave as the application were in fullscreen the whole time
Screen.WorkAreaWidth, //same as screen.width
(Layout1.Parent as TLayout).Width, //invalid typecast
(Layout1.GetParentComponent as TLayout).Width //invalid typecast

他们都没有工作。我希望能够基于其可视宽度来调整组件在屏幕上的位置,因此,如果用户在创建新表单之前调整窗口的大小,组件将正确对齐。有人知道解决方案吗?在此先感谢

delphi resize firemonkey firedac
2个回答
0
投票

该表单具有ClientWidth和ClientHeight属性,它们在OnResize中很好地起作用,可以根据可用区域动态调整控件的大小。在窗体可见之前,将触发OnResize。例如,有一个带有100边框的Memo1:

procedure TForm1.FormResize(Sender: TObject);
begin
  memo1.Position.X := 100;  // Only needs to be done once but here so all code is in one spot
  memo1.Position.Y := 100;
  memo1.Width := form1.ClientWidth - 200;
  memo1.Height := form1.ClientHeight - 200;
end;

0
投票

但是当我将form1置于全屏模式时,请点击创建form2时,组件与窗口模式。

TForm1.SpeedButton1Click(),您调用Application.CreateForm(TForm2, Form2);,该触发器触发TForm2.FormCreate(Sender: TObject);,后者又调用MakeWindow。此时,Form2仅知道设计时间的大小。

MakeWindow完成后,TForm1.SpeedButton1Click()继续:

Form2.Layout1.parent := Self.Layout1.Parent;
Form2.Layout1.Width := Self.Layout1.Width;

但是这些不再影响控件的布局。

例如,在调用Form2.Layout1.Width之前需要设置MakeWindow

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  Application.CreateForm(TForm2, Form2);
  Form2.Layout1.parent := Self.Layout1.Parent;
  Form2.Layout1.Width := Self.Layout1.Width;
  MakeWindow; // remove it from TForm2.FormCreate()
end;
© www.soinside.com 2019 - 2024. All rights reserved.