调整按钮大小,使它们都具有相同的宽度 [英] Resizing buttons so they are all the same width

查看:91
本文介绍了调整按钮大小,使它们都具有相同的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个宽的 TPanel,上面有几个按钮(实际上是一个工具栏)。所有按钮都具有Align = Left。我创建了一个函数,可以将按钮的大小调整为相同的大小,并计算按钮的宽度,以使它们充满整个TPanel。我在TPanel的OnResize事件处理程序中调用此函数。

I have a "wide" TPanel with several buttons on it (essentially a tool bar). All the buttons have Align=Left. I have created a function which will resize the buttons to the same size and calculate the width of them so they fill the entire TPanel. I call this function in the OnResize event handler of the TPanel.

procedure ScaleButtonsOnPanel;
var i: Integer;
begin
  for i:=0 to mPanel.ControlCount-1 do begin
      mPanel.Controls[i].Width := round(mPanel.width/mPanel.ControlCount-1)
  end;
end;

问题是如果我最小化然后恢复按钮的布局,从设计布局更改后。

The problem is if I minimize and then restore the form the layout of the buttons change from the design layout.

谁能提供一种解决方案,使面板上的按钮可以调整大小,但保持设计时间顺序(从左到右放置)?

Can anyone offer a solution to having buttons on a panel which can be resized but maintain the design time order (in terms of left to right placement) ?

推荐答案

我真的看不到您的问题。但是,当然,您必须设置按钮的位置,不仅是按钮的大小。

I do not really see your problem. But of course, you must set the position of the buttons, not only their size.

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

这很好。

请参阅 https://privat.rejbrand.se/panelresize.wmv

好,现在知道了。我认为 alLeft 实际上是您的问题。具有相同对齐方式的控件倾向于更改其顺序。这是众所周知的德尔福烦恼。就像我上面做的一样。只要确保您以正确的顺序浏览按钮即可。如果您不能依赖 Panel1.Controls 的顺序,则可以这样做:设置 Tag 属性每个工具栏按钮在工具栏中的位置(0、1,...),然后执行

OK, now I see. I think the alLeft is actually your problem. Controls with the same align tend to change their order. This is a well-known Delphi annoyance. Do it like I do above, instead. Just make sure that you go through the buttons in the right order. If you cannot rely on the ordering of Panel1.Controls, then you can do like this: Set the Tag property of each toolbar button to its position (0, 1, ...) in the toolbar then do

procedure TForm1.Panel1Resize(Sender: TObject);
var
  i: Integer;
  btnWidth: integer;
begin
  btnWidth := Panel1.Width div Panel1.ControlCount;
  for i := 0 to Panel1.ControlCount - 1 do
  begin
    Panel1.Controls[i].Left := Panel1.Controls[i].Tag * btnWidth;
    Panel1.Controls[i].Width := btnWidth;
  end;
end;

这篇关于调整按钮大小,使它们都具有相同的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆