0

我已经为编写允许在此处隐藏组件的组件找到了很多帮助(THIDEPANEL。现在我遇到了第一个问题:

OnCreate这个类的事件中,我采用面板的宽度和高度,我想在隐藏/取消隐藏面板时恢复到原始值。实际上隐藏过程总是减小面板的大小

constructor THidePanel.Create(AOwner: TComponent);
begin
  inherited;

  // The inner panel
  WorkingPanel := TPanel.Create(Self);
  WorkingPanel.Caption := '***';

  // The hide unhide
  FActivateButton := TButton.Create(Self);
  FActivateButton.Parent := self;
  FActivateButton.Caption := '<';
  FActivateButton.OnClick := H_ActivateButtonClick;
  FActivateButton.Width := BoarderSize;
  FActivateButton.Height := BoarderSize;
  WorkingPanel.Caption := '';

  // Grab the size of the hide panel, later restore to this values
  FLargeWidth := Self.Width;
  FLargeHeight := Self.Height;

  SetButtonPosition(TopRight);
end;
4

1 回答 1

2

这是因为FLargeWidth私有字段的值无效。您在构造函数期间分配它Self.Width(并且您可能永远不会更新它)。这不是您在设计时或运行时设置的宽度,而是来自 的硬编码宽度TCustomPanel.Create,即185. 请注意,当控件的构造函数运行时,控件尚未放置。

如果你想记住设置的宽度,那么你应该“覆盖TControl.SetWidth”。但是由于该方法是私有的(不是虚拟的),因此您需要覆盖SetBoundsResize以响应Width's 的更改。我会选择后者,可能还有一个附加条件:

procedure THidePanel.Resize;
begin
  if not HasCustomWidth then  //< Replace this with your own logic condition
    FLargeWidth := Width;
  inherited Resize;
end;
于 2013-04-09T21:38:53.127 回答