我认为标签是合乎逻辑的
消失,因为 Richedit 是
父母
这是错误的。在您的代码中,TLabel 的父级应该是TDBRichEditExt 的父级。请注意,在TDBRichEditExt、Parent 和Self.Parent 的方法中是同一件事。 如果您希望TLabel 的父级是TDBRichEditExt 本身 - 您这样做不 - 那么您应该设置lblCaption.Parent := self;。
现在,如果TLabel 的父级是TDBRichEditExt 的父级,那么TLabel 的Top 属性指的是TDBRichEditExt 的父级,而不是TDBRichEditExt 本身。因此,如果TDBRichEditExt 的父级是TForm,则Top := -5 意味着TLabel 将位于表单上边缘上方五个像素。你的意思是
lblCaption.Top := Self.Top - 5;
但是 -5 是一个太小的数字。你真正应该使用的是
lblCaption.Top := Self.Top - lblCaption.Height - 5;
另外在标签和 Rich Edit 之间留出了 5 px 的空间。
另外,你想要
lblCaption.Left := Self.Left;
另一个问题
但这不起作用,因为在创建组件时,我认为还没有设置 Parent。因此,您需要在更合适的时间进行标签的定位。另外,这会在你的组件每次移动时移动标签,这一点非常重要!
TDBRichEditExt = class(TRichEdit)
private
FLabel: TLabel;
FLabelCaption: string;
procedure SetLabelCaption(LabelCaption: string);
public
constructor Create(AOwner: TComponent); override;
procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override;
published
LabelCaption: string read FLabelCaption write SetLabelCaption;
end;
procedure TDBRichEditExt.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited;
if not assigned(Parent) then
Exit;
FLabel.Parent := self.Parent;
FLabel.Top := self.Top - FLabel.Height - 5;
FLabel.Left := self.Left;
end;
详情
此外,当您隐藏TDBRichEditExt 时,您也希望隐藏标签。因此你需要
protected
procedure CMVisiblechanged(var Message: TMessage); message CM_VISIBLECHANGED;
在哪里
procedure TDBRichEditExt.CMVisiblechanged(var Message: TMessage);
begin
inherited;
if assigned(FLabel) then
FLabel.Visible := Visible;
end;
对于Enabled 属性也是如此,每次TDBRichEditExt 的父级更改时,您还需要更新TLabel 的父级:
protected
procedure SetParent(AParent: TWinControl); override;
与
procedure TDBRichEditExt.SetParent(AParent: TWinControl);
begin
inherited;
if not assigned(FLabel) then Exit;
FLabel.Parent := AParent;
end;