我已经了解并实践了自定义组件的开发,不过都是继承自一个其他基本组件的,现在我想实现一个组件比如是一个文本框前面一个Label,后面一个Button,就是说三个组件合成一个组件,大家给个思路。
我试者先继承一个edit,然后在构造的时候创建label和button,设定Parent,可是好像不成功,label和button不会显示。各位能人指教下,谢了。
我试者先继承一个edit,然后在构造的时候创建label和button,设定Parent,可是好像不成功,label和button不会显示。各位能人指教下,谢了。
解决方案 »
- C++中的CRC8转换为delphi代码问题
- 在拔掉网线后,如何得到网卡的MAC
- 给小风的(2)
- 大家现在做的系统,是不是都用到流程引擎?
- 我看到了数据库这一块,现在我要是想练习ORACLE以及SLQ SERVER,是不是还应该另外安装这两个软件的客户端软件呐?
- 对于程序员来说,什么样的项目经理才算是好的领头人呢?
- 李维的Delphi5 电子商务篇中的第3章第二个范例为什么调不出来?
- const Format: string; const Args: array of const):
- 关于TTreeView
- 当listview的item选择时,如何设置其icon随着变化呢?
- 请问我添加了TTABLE在设tablename时出错in module 'sqldb2v5.dll'
- windows组件服务的问题
TmyControl=class(TWinControl)
private
FLabel:TLabel;
FEdit:TEdit;
FButton:TButton;
public
constructor Create;override;
destructor Destroy; override;
end;
对于“我已经了解并实践了自定义组件的开发,不过都是继承自一个其他基本组件的”的说法是不正确的。
谢谢maozefa(阿发伯),我用的是D6,能贴给我吗?谢了.
FEdit:TEdit;
FButton:TButton;
加为私有
private
FEditLabel: TBoundLabel;
FLabelPosition: TLabelPosition;
FLabelSpacing: Integer;
procedure SetLabelPosition(const Value: TLabelPosition);
procedure SetLabelSpacing(const Value: Integer);
protected
procedure SetParent(AParent: TWinControl); override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure SetName(const Value: TComponentName); override;
procedure CMVisiblechanged(var Message: TMessage);
message CM_VISIBLECHANGED;
procedure CMEnabledchanged(var Message: TMessage);
message CM_ENABLEDCHANGED;
procedure CMBidimodechanged(var Message: TMessage);
message CM_BIDIMODECHANGED;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override;
procedure SetupInternalLabel;
property EditLabel: TBoundLabel read FEditLabel;
property LabelPosition: TLabelPosition read FLabelPosition write SetLabelPosition;
property LabelSpacing: Integer read FLabelSpacing write SetLabelSpacing;
end;
....
....
{ TCustomLabeledEdit }constructor TCustomLabeledEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLabelPosition := lpAbove;
FLabelSpacing := 3;
SetupInternalLabel;
end;procedure TCustomLabeledEdit.CMBidimodechanged(var Message: TMessage);
begin
inherited;
FEditLabel.BiDiMode := BiDiMode;
end;procedure TCustomLabeledEdit.CMEnabledchanged(var Message: TMessage);
begin
inherited;
FEditLabel.Enabled := Enabled;
end;procedure TCustomLabeledEdit.CMVisiblechanged(var Message: TMessage);
begin
inherited;
FEditLabel.Visible := Visible;
end;procedure TCustomLabeledEdit.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (AComponent = FEditLabel) and (Operation = opRemove) then
FEditLabel := nil;
end;procedure TCustomLabeledEdit.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
SetLabelPosition(FLabelPosition);
end;procedure TCustomLabeledEdit.SetLabelPosition(const Value: TLabelPosition);
var
P: TPoint;
begin
if FEditLabel = nil then exit;
FLabelPosition := Value;
case Value of
lpAbove: P := Point(Left, Top - FEditLabel.Height - FLabelSpacing);
lpBelow: P := Point(Left, Top + Height + FLabelSpacing);
lpLeft : P := Point(Left - FEditLabel.Width - FLabelSpacing,
Top + ((Height - FEditLabel.Height) div 2));
lpRight: P := Point(Left + Width + FLabelSpacing,
Top + ((Height - FEditLabel.Height) div 2));
end;
FEditLabel.SetBounds(P.x, P.y, FEditLabel.Width, FEditLabel.Height);
end;procedure TCustomLabeledEdit.SetLabelSpacing(const Value: Integer);
begin
FLabelSpacing := Value;
SetLabelPosition(FLabelPosition);
end;procedure TCustomLabeledEdit.SetName(const Value: TComponentName);
begin
if (csDesigning in ComponentState) and ((FEditlabel.GetTextLen = 0) or
(CompareText(FEditLabel.Caption, Name) = 0)) then
FEditLabel.Caption := Value;
inherited SetName(Value);
if csDesigning in ComponentState then
Text := '';
end;procedure TCustomLabeledEdit.SetParent(AParent: TWinControl);
begin
inherited SetParent(AParent);
if FEditLabel = nil then exit;
FEditLabel.Parent := AParent;
FEditLabel.Visible := True;
end;procedure TCustomLabeledEdit.SetupInternalLabel;
begin
if Assigned(FEditLabel) then exit;
FEditLabel := TBoundLabel.Create(Self);
FEditLabel.FreeNotification(Self);
FEditLabel.FocusControl := Self;
end;
==========================================================
参见上面代码中的SetParent过程,你也应该继承这个过程