THidepanel @运行时 [英] THidepanel @ runtime
问题描述
我已经找到了很多帮助编写组件的帮助,这些组件允许在此处隐藏组件(
I already found much help for writing a components which allows Hiding of components here ( THIDEPANEL. Now I suffer a first issues:
在此类的 OnCreate
事件中,我将Panel的宽度和高度设为高,我想在隐藏/取消隐藏面板的同时恢复到原始值.实际上,隐藏过程总是会减小面板的尺寸
During the OnCreate
event of this class I take the Panel width and height, I want to restore to the original values while hidden / unhiding the panel. Actually the hide process always decrease the size of the panel
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;
推荐答案
这是因为 FLargeWidth
私有字段具有无效值.您在构造函数期间使用 Self.Width
对其进行分配(并且可能永远不会对其进行更新).这不是您在设计时或运行时设置的宽度,而是 185
>.请注意,在运行控件的构造函数时,尚未放置该控件.
It is because the FLargeWidth
private field has an invalid value. You assign it with Self.Width
during the constructor (and you presumably never update it). That is not the width you set at design time or at run time, but it is the hard coded width from TCustomPanel.Create
, which is 185
. Note that when a control's constructor is run, the control is not placed yet.
如果要记住设置的宽度,则应覆盖 TControl.SetWidth
".但是由于该方法是私有的(不是虚拟的),因此您需要覆盖 SetBounds
或 Resize
以便响应 Width
的更改.我会选择后者,可能还有其他条件:
If you want to remember the set width, then you should "override TControl.SetWidth
". But since that method is private (not virtual), you need to override either SetBounds
or Resize
in order to response to Width
's change. I would choose the latter, probably with an additional condition:
procedure THidePanel.Resize;
begin
if not HasCustomWidth then //< Replace this with your own logic condition
FLargeWidth := Width;
inherited Resize;
end;
这篇关于THidepanel @运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!