求助:自定义组件开发问题(50分)

  • 主题发起人 主题发起人 wallysw
  • 开始时间 开始时间
W

wallysw

Unregistered / Unconfirmed
GUEST, unregistred user!
我在自定义一个组件时,在设计期间,把组件放到FORM1里,拖动Labelededit时,为什么speedbutton不跟它走动啊?完整代码如下:

---------------------------------------------

unit edop;

interface

uses
SysUtils, Classes, Controls, StdCtrls, ExtCtrls, Buttons;

type
Tedop = class(TLabeledEdit)
private
BTN: TSpeedButton;
FBtnVisible: Boolean;
procedure SetVisible(const Value: boolean);
procedure SetLeft(const Value: integer);
procedure SetTop(const Value: integer);
function GetTop:integer;
function GetLeft:integer;
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOWner: TComponent);override;
Destructor Destroy;override;
{ Public declarations }
published
property BtnVisible: boolean read FBtnVisible write SetVisible;
property Top: integer read GetTop write SetTop;
property Left: integer read GetLeft write SetLeft;
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Standard', [Tedop]);
end;

{ Tedop }

constructor Tedop.Create(AOWner: TComponent);
begin
inherited Create(AOWner);
end;


destructor Tedop.Destroy;
begin
BTN.Free;
inherited Destroy;
end;

function Tedop.GetLeft: integer;
begin
result := inherited Left;
end;

function Tedop.GetTop: integer;
begin
result := inherited Top;
end;

procedure Tedop.SetLeft(const Value: integer);
begin
inherited Left := Value;
btn.Left := Value + Width;
end;

procedure Tedop.SetTop(const Value: integer);
begin
inherited Top := Value;
btn.Top := Value;
end;

procedure Tedop.SetVisible(const Value: boolean);
begin
FBtnVisible := Value;
if Value then
begin
BTN := TSpeedButton.Create(Self);
BTN.Left := Self.Left + Self.Width;
BTN.Top := Self.Top;;
BTN.Parent := Self.Parent;
end else
begin
BTN.Free;
end;
end;

end.

---------------------------------

望哪位大哥大姐帮我解决一下。谢谢。
 
这属于控件联合问题,很简单,我转贴一点资料你就明白了
下面是转贴,没仔细整理,因为我觉得这样可以看出大家探讨的过程和思路

我的意思是 希望一个类C(包含 两个元素 a:TButton;b:TImage),希望button ,显示在
image的下面,而Sender 是 C;
pengjinlongex 发表于 2002-11-24 16:12:00
从Twincontrol继承,
tmyclass=class9twincontrol)
private
fa:timage;
fb:tbutton;
...
end;
将button的事件引出,用self替代sender即可
雨人 发表于 2002-11-24 16:50:00
TMYclass=clss(Twincontronl)
private
FButton:Tbutton;
FImage:=TImage;
constractor create(Aowner:Tcomponent);
...
TMyclass.create(Aowner:Tcompnent);
begin
inherited create;
Fbutton:=Tbutton.create(self);
Fbutton.parent:=self;
Fimage:=Timage.create(self);
Fimage.parent:=self;
end;

killer01234 发表于 2002-11-25 9:16:00
雨人:
那么在MyClass 的一个实例显示时。我怎样使button总在Image的下面。
killer01234 发表于 2002-11-25 9:23:00
pengjinlongex:
请问怎样将事件引出。谢谢
小汽车 发表于 2002-11-25 9:27:00
这样的控件我做过,你要的话我可以给你!不过你的那分要给我哦:)
联系h1yn@163.com
我做的是,和TImage差不多,但他具有Tbutton 的功能!!!
JohnsonGuo 发表于 2002-11-25 9:54:00
随便写了一个,只引出了Button.OnClick.在D6下运行通过.
unit ImageButton;

interface

uses
Windows, Messages, SysUtils, Classes,
Controls, ExtCtrls, StdCtrls;

type
TImageButton = class(TWinControl)
private
FImage: TImage;
FButton: TButton;
FOnClick: TNotifyEvent;
protected
procedure ButtonClick(Sender :TObject);
procedure DoClick; virtual;
procedure Resize; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property OnClick: TNotifyEvent read FOnClick write FOnClick;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TImageButton]);
end;

{ TImageButton }

procedure TImageButton.ButtonClick(Sender: TObject);
begin
DoClick;
end;

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

Width := 200;
Height := 220;

FImage := TImage.Create(Self);
FImage.Parent := Self;
FImage.SetBounds(0, 0, 200, 200);

FButton := TButton.Create(Self);
FButton.Parent := Self;
FButton.SetBounds(0, 200, 200, 20);
FButton.OnClick := ButtonClick;
end;

destructor TImageButton.Destroy;
begin
FImage.Free;
FButton.Free;
inherited;
end;

procedure TImageButton.DoClick;
begin
if Assigned(FOnClick) then FOnClick(Self);
end;

procedure TImageButton.Resize;
begin
inherited;
if HandleAllocated then begin
FImage.SetBounds(0, 0, Width, Height - 20);
FButton.SetBounds(0, Height - 20, Width, 20);
end;
end;

end.
killer01234 发表于 2002-11-25 11:23:00
JohnsonGuo:
我找你的试了一下,只是在单击button 才能响应,而单击image没响应

JohnsonGuo 发表于 2002-11-25 14:15:00
看到Create中的一句FButton.OnClick := ButtonClick没有?
在Create中添加FImage.OnClick := ButtonClick就可以了.
其他事件的处理方法相同,就不用我一一写出来了吧.

killer01234 发表于 2002-11-25 15:05:00
谢谢各位。分数有限。分一下。
 
我去试一下,先给分了。谢谢咯[:)]
 
后退
顶部