sta*_*nic 5 delphi vcl autosize delphi-xe2
我试图以下列方式使用TFlowPanel组件:
Form1组件上FlowPanel1: TFlowPanel. Form1.Width = 400,FlowPanel1.Align = alTop,FlowPanel1.AutoSize = True,FlowPanel1.AutoWrap = True. FlowPanel15个SpeedButtons上并将它们设置Width为64. Form1.Width = 200).出于某种原因,当用户调整表单大小时,速度按钮不会自动排成两行.虽然,当它们排成两行时AutoSize = False,AutoWrap = True.
这种行为的原因是什么以及如何解决?
编辑:我发现了"快速而肮脏"的解决方案.以下代码是事件的事件处理程序TFlowPanel.OnResize:
procedure TForm1.FlowPanel1Resize(Sender: TObject);
begin
with FlowPanel1 do
begin
AutoSize := False;
Realign; // line up controls
AutoSize := True; // adjust TFlowPanel.Height
end;
end;
Run Code Online (Sandbox Code Playgroud)
但是,我仍然想知道是否有一种解决问题的标准方法.
我无法在代码中找到此类行为的确切原因,但基本上,您已经挑战了两个大小设置属性,即AutoSizeand和Align。我认为问题是,当您调整窗体的大小时,AutoSize配置为True并Align设置为alTop的控件将首先尝试自动调整控件的大小,然后对齐其父控件的顶部。我可以肯定地说的是,至少不应从逻辑含义上将这两个属性组合在一起。
我建议您采用的解决方法是默认情况下关闭自动调整大小,OnResize以防万一暂时将其自动打开然后再关闭以自动调整高度。因此,在代码中它将简单地更改为:
procedure TForm1.FlowPanel1Resize(Sender: TObject);
begin
// there's no Realign here, since the AlignControls request is called
// at control resize, so here you have children already aligned, what
// you then need is to request the control to autosize the height and
// turn off the autosizing to the default, disabled state
FlowPanel1.AutoSize := True;
FlowPanel1.AutoSize := False;
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2492 次 |
| 最近记录: |