我希望在某些按钮不可见时自动调整大小按钮,保持相同的宽度。我使用了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;
你能告诉我如何解决这个问题吗?
答案 0 :(得分:1)
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;