【问题标题】:How do I make two controls each occupy half their parent's area?如何让两个控件各自占据其父区域的一半?
【发布时间】:2013-05-21 12:46:39
【问题描述】:

我有一个附加了侧边栏的应用程序 (TPanel --> alRight),其中使用了 CategoryPanel (alClient)。此 CategoryPanel 恰好有 2 个未对齐的组。我想分享这两组的边界,这样它就会以 50/50 的比例填满整个面板空间。不幸的是,CategoryGroups 不支持设计时对齐,这迫使我每次想要测试它时都运行我的应用程序。我尝试将每个 CategoryGroup 设置为面板一半的高度,但它显示滚动条。 (见图2)

如何以 50/50 的比例正确对齐/共享边界?

【问题讨论】:

  • 当类别组关闭时,您希望发生什么?
  • 如果它们折叠起来,什么都不会发生,尺寸会保持不变。

标签: delphi delphi-xe vcl


【解决方案1】:

根据你的cmets,你想运行这段代码:

procedure TForm1.UpdateGroupHeights;
begin
  if not CategoryPanel1.Collapsed then
    CategoryPanel1.Height := CategoryPanelGroup1.ClientHeight div 2;
  if not CategoryPanel2.Collapsed then
    CategoryPanel2.Height := CategoryPanelGroup1.ClientHeight -
      CategoryPanelGroup1.ClientHeight div 2;
end;

当您希望影响组布局的任何更改时。所以我认为你需要从以下事件中调用这个函数:

  • 表单的OnCreate 事件。
  • TCategoryPanelGroupOnResize 事件。
  • 两个类别面板的OnCollapseOnExpand 事件。

当一个面板折叠而另一个面板展开时,这看起来有点奇怪。我个人会重新调整代码以填充所有可用空间。

if not CategoryPanel1.Collapsed then
  ;//nothing to do
if CategoryPanel1.Collapsed and not CategoryPanel2.Collapsed then
  CategoryPanel2.Height := CategoryPanelGroup1.ClientHeight-CategoryPanel1.Height;
if not CategoryPanel1.Collapsed and CategoryPanel2.Collapsed then
  CategoryPanel1.Height := CategoryPanelGroup1.ClientHeight-CategoryPanel2.Height;
if not CategoryPanel1.Collapsed and not CategoryPanel2.Collapsed then
begin
  CategoryPanel1.Height := CategoryPanelGroup1.ClientHeight div 2;
  CategoryPanel2.Height := CategoryPanelGroup1.ClientHeight-CategoryPanel1.Height;
end;

【讨论】:

  • 完全忘记了 ClientHeight!非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 2020-07-26
相关资源
最近更新 更多