当某些按钮不可见时调整按钮的大小并保持相同的宽度

Resizing buttons with keeping the same width when some buttons are invisible

我想实现自动调整按钮大小,当某些按钮不可见时保持相同的宽度。我使用了 Andreas Rejbrand at this link 编写的代码,但是当我将一些按钮设置为不可见时,问题变得更加复杂。在我们有隐形按钮的地方有缝隙。 我的想法是检查有多少按钮是不可见的,然后根据可见按钮的数量设置 btnWidth。在这种情况下,我实际上不知道如何检查按钮是否不可见。

我想为按钮使用 TAdvGlowButton 组件,为面板使用 TPanel 组件并添加 OnResize 面板程序如下:

procedure TForm3.Panel4Resize(Sender: TObject);
var
   i: Integer;
   btnWidth: Integer;
begin
   btnWidth := Panel4.Width div Panel4.ControlCount;
   for i := 0 to Panel4.ControlCount - 1  do
      begin
         Panel4.Controls[i].Left := i * btnWidth;
         Panel4.Controls[i].Width := btnWidth;
      end;
end;

你能告诉我如何解决这个问题吗?

procedure TForm3.Panel4Resize(Sender: TObject);
const
  cLeftMargin = 10;  //Margin at the left side of the group of buttons
  cSpacing = 10;     //Spacing/Margin between the buttons
  cRightMargin = 10; //Margin at the right side of the group of buttons
var
   i, VisibleControls, lLeft: Integer;
   btnWidth: Integer;
begin
   //count number of visible controls
   VisibleControls := 0;
   for i := 0 to Panel4.ControlCount - 1  do
      if Panel4.Controls[i].Visible then
         inc(VisibleControls);

   btnWidth := (Panel4.Width-cLeftMargin-cRightMargin - cSpacing*(VisibleControls-1)) div VisibleControls;

   //distribute the visible controls
   lLeft := cLeftMargin;
   for i := 0 to Panel4.ControlCount - 1  do
      if Panel4.Controls[i].Visible then
      begin
         Panel4.Controls[i].Left := lLeft;
         Panel4.Controls[i].Width := btnWidth;
         lLeft := lLeft + btnWidth + cSpacing;
      end;
end;