【问题标题】:Resizing panels components proportionally in Delphi在 Delphi 中按比例调整面板组件的大小
【发布时间】:2020-11-13 05:29:42
【问题描述】:

所以我使用的是 Delphi Rad Studio 10.3 版。

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

我正在尝试创建一个包含 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.

问候, 罗马人

【问题讨论】:

    标签: delphi delphi-10.3-rio


    【解决方案1】:

    首先,您应该注意到控件客户区的宽度为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,表格会被分成大小相同的两部分。

    【讨论】:

    • 你好。好的,谢谢!你的答案正是我想要的。我可以再问一个问题吗? Persay 我想要面板 1 中的 4 个按钮,同时调整面板的大小,我该怎么做?
    • 我假设您有一列垂直的按钮,并且希望按钮的宽度随着面板的增加而增加。如果是这样,只需将每个按钮的Anchor 设置为[akLeft, akTop, akRight](即添加akRight)。在设计时很好地定位它们,它们会在运行时自动调整大小。
    • 你好。实际上,按钮在提供的图像中相邻的两列中,但不要担心。我想到了!再次感谢!
    • @Andreas Rejbrand 介意帮我解决另一个问题吗? stackoverflow.com/questions/63086596/…
    猜你喜欢
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 2014-07-21
    相关资源
    最近更新 更多