在 Delphi 中按比例调整面板组件的大小

Resizing panels components proportionally in Delphi

所以我正在使用 Delphi Rad Studio 版本 10.3。

我必须在学校(11 年级)为 PAT 创建统计表格。

我正在尝试创建一个包含 2 个面板的表单。一个用于显示统计数据并与之交互的按钮,另一个用于实际统计数据。

问题来了: 我希望能够调整表单的大小,并且 2 个面板应该按比例调整自己的大小。我的代码中的错误,我似乎无法修复,是在缩小表单后,面板的宽度没有减少,而是增加了。我使用第三个面板来确定更改是调整表单的大小,因为我发现,表单的宽度 != 可放置组件的宽度。

这是我创建的用于舍入 2 个数字 (frmHelp_Dialog.my_round) 的函数的代码:

function TFrmHelp_Dialog.My_Round(number: real): integer;
var
  Decimal_value, Integer_value, snumber: string;
begin
  try
    snumber := floattostrF(number, fffixed, 12, 8);

    Decimal_value := copy(snumber, pos('.', snumber) + 1, length(snumber));
    Integer_value := copy(snumber, 1, pos('.', snumber) - 1);

    if Decimal_value[1] >= '5' then
    begin
      result := strtoint(Integer_value) + 1;
    end
    else
    begin
      result := strtoint(Integer_value);
    end;
  except
    beep;
    messagedlg
      ('An error occured during function "Round" excecution in Help_Dialog.pas.',
      mterror, [mbok], 0);
  end;
end;

这是我的代码:

    unit Stats_u;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, VclTee.TeeGDIPlus,
  VclTee.TeEngine, Vcl.ExtCtrls, VclTee.TeeProcs, VclTee.Chart, VclTee.series,
  Help_Dialog;

type
  TStats = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure FormResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);

  private
  var[enter image description here][1]
    Chart2: TChart;
    Original_Width: integer;

  const
    Panel_Ratio = 0.5;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Stats: TStats;

implementation

{$R *.dfm}

uses Unit1;

procedure TStats.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Stats.hide;
  form1.position := poscreencenter;
  form1.show;
end;

procedure TStats.FormCreate(Sender: TObject);
begin
  Stats.position := poscreencenter;
  // panels
  Original_Width := Panel3.left + Panel3.width;
  Panel2.width := Original_Width - Panel2.left;
  Panel3.SendToBack;
  Panel2.BringToFront;

end;

procedure TStats.FormResize(Sender: TObject);
var
  Current_Width: integer;
begin
  // get current width
  Current_Width := Panel3.left + Panel3.width;
  (*
    showmessage(IntToStr(Current_Width));
    showmessage(IntToStr(original_width));
  *)

  // --set anchors--
  Panel1.anchors := [aktop, akbottom, akleft];
  Panel2.anchors := [aktop, akbottom];

  // panel 1 resize:
  Panel1.width := Panel1.width + FrmHelp_Dialog.My_Round
    (0.1 * (Current_Width - Original_Width));

  Panel2.left := Panel1.width;
  Panel2.width := Panel2.width + FrmHelp_Dialog.My_Round
    (0.9 * (Current_Width - Original_Width));

  Original_Width := Current_Width;

end;

end.

Image During Runtime, not resized yet.

此致, 罗马书

首先,您应该注意到控件客户区的宽度是ClientWidth。使用它而不是 Width,它也包括任何边框。这也适用于表单。

现在,创建一个新的 VCL 应用程序并在其上放置两个 TPanel 控件。

第一个设置Align = alLeft,第二个设置Align = alClient

最后,为表单添加一个 OnResize 处理程序:

procedure TForm1.FormResize(Sender: TObject);
const
  Factor = 0.5;
begin
  Panel1.Width := Round(Factor * ClientWidth);
end;

此处,FactorPanel1 应使用的表格宽度的分数。它是一个介于 0 和 1 之间的实数。Panel2 将使用的表格宽度的分数是 1 - Factor

例如,如果Factor = 0.5,表格将被拆分成大小相同的两个部分。