这个控件的Event如何Published?(高手请进!)(150分)

  • 主题发起人 主题发起人 zyycc
  • 开始时间 开始时间
Z

zyycc

Unregistered / Unconfirmed
GUEST, unregistred user!
这个控件的Event如何Published?
我编写了一个带Button的Edit,但我想分别Publish其中Button的
与Edit的onClick事件,这能实现吗?
目前,我的代码如下:

type
TEditButton = class(TCustomControl)
private
FEdit: TEdit;
FButton: TButton;
function GetEditText: string;
function GetButtonText: string;
procedure SetButtonText(const Value: string);
procedure SetEditText(const Value: string);
function GetEditReadOnly: Boolean;
procedure SetEditReadOnly(const Value: Boolean);
protected
procedure WmSize(var Message: TMessage); message WM_SIZE;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
public
constructor Create(AOwner: TComponent); override;
published
property EditText: string read GetEditText write SetEditText;
property Font;
property ButtonText: string read GetButtonText write SetButtonText;
property EditReadOnly: Boolean read GetEditReadOnly write SetEditReadOnly;
end;
 
constructor Create(AOwner: TComponent); override;
destructor Destroy(AOwner: TComponent); override;


TEditButton.Create(..)
begin
..
..
FEdit:=TEdit.Create(Self);
FEdit.parent:=self;
FButton:= TButton.Create(self);
FButton.parent:=self;
..
end;

TEditButton.Destroy(..)
begin
FEdit.free;
FButton.free;
inherited destroy
end;

还需要override paint event or process wm_paint
 
type
TEditButton = class(TCustomControl)
private
FEdit: TEdit;
FButton: TButton;
function GetEditText: string;
function GetButtonText: string;
procedure SetButtonText(const Value: string);
procedure SetEditText(const Value: string);
function GetEditReadOnly: Boolean;
procedure SetEditReadOnly(const Value: Boolean);
function GetEditClick:TNotifyEvent;
procedure SetEditClick(Value:TNotifyEvent);
function GetButtonClick:TNotifyEvent;
procedure SetButtonClick(Value:TNotifyEvent);
protected
procedure WmSize(var Message: TMessage); message WM_SIZE;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
public
constructor Create(AOwner: TComponent); override;
published
property EditText: string read GetEditText write SetEditText;
property Font;
property ButtonText: string read GetButtonText write SetButtonText;
property EditReadOnly: Boolean read GetEditReadOnly write SetEditReadOnly;
property EditClick:TNotifyEvent read GetEditClick write SetEditClick;
property ButtonClick:TNotifyEvent read GetButtonClick write SetButtonClick;
end;

function TEditButton.GetEditClick:TNotifyEvent;
begin
Result:=FEdit.OnClick;
end;

procedure TEditButton.SetEditClick(Value:TNotifyEvent);
begin
FEdit.OnClick:=Value;
end;

function TEditButton.GetButtonClick:TNotifyEvent;
begin
Result:=FButton.OnClick;
end;

procedure TEditButton.SetButtonClick(Value:TNotifyEvent);
begin
FButton.OnClick:=Value;
end;
 
published 自己的OnButtonClick和OnEditClick事件.
当点击ButtonClick时触发OnButtonClick,当点击Edit时触发OnEditClick
type
TEditButton = Class(TCustomControl)
private
FEdit : TEdit;
FButton : TButton;
FButtonClick : TNotifyEvent;
FEditClick : TNotifyEvent;
procedure ButtonClick(Sender : TObject);
procedure EditClick(Sender : TObject);
public
constructor Create(AOwner: TComponent);
destructor Destroy; override;
published
property OnButtonClick : TNotifyEvent read FButtonClick write FButtonClick;
property OnEditClick : TNotifyEvent read FEditClick write FEditClick;
end;

procedure Register;
implementation


{ TEditButton }
procedure Register;
begin
RegisterComponents('Samples', [TEditButton]);
end;

constructor TEditButton.Create(AOwner: TComponent);
begin
inherited;
Fedit := TEdit.Create(self);
FButton := TButton.Create(Self);
Fedit.Parent := Self;
FButton.Parent := Self;
FButton.Left := Fedit.Left + Fedit.Width;
FButton.Top := Fedit.Top;

Fedit.OnClick := EditClick;
FButton.OnClick := ButtonClick;
end;

destructor TEditButton.Destroy;
begin
Fedit.Free;
FButton.Free;
inherited;
end;

procedure TEditButton.EditClick(Sender: TObject);
begin
if assigned(FEditClick) then FEditClick(Self);
end;

procedure TEditButton.ButtonClick(Sender : TObject);
begin
if assigned(FButtonClick) then FButtonClick(Self);
end;
 
我按上面写后,然后安装,可在Objcet Inspetor的Events页上什么也没有?是什么原因呢?
我的环境为 CWin98 + Delphi4.0
代码如下:


interface

uses
Windows, Messages, SysUtils,
Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;

type
TEditButton = class(TCustomControl)
private
FEdit: TEdit;
FEditEnter: TNotifyEvent;
FButton: TButton;
FButtonClick: TNotifyEvent;
procedure ButtonClick(Sender:TObject);
Procedure EditEnter(Sender:TObject);
function GetEditText: string;
function GetButtonText: string;
procedure SetButtonText(const Value: string);
procedure SetEditText(const Value: string);
function GetEditReadOnly: Boolean;
procedure SetEditReadOnly(const Value: Boolean);
protected
procedure WmSize(var Message: TMessage); message WM_SIZE;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property OnButtonClick: TNotifyEvent read FButtonClick write FButtonClick;
property OnEditEnter: TNotifyEvent read FEditEnter write FEditEnter;
property EditText: string read GetEditText write SetEditText;
property Font;
property ButtonText: string read GetButtonText write SetButtonText;

end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Unleash', [TEditButton]);
end;

{ TEditButton }

procedure TEditButton.CMFontChanged(var Message: TMessage);
begin
inherited;
FButton.Top := FEdit.Top + 2;
FButton.Height := FEdit.Height - 4;
FButton.Font.Size := FEdit.Font.Size + 3;
end;

constructor TEditButton.Create(AOwner: TComponent);
const
Gap = 0;
begin
inherited Create(AOwner);
Width := 130;
Height := 24;

FEdit := TEdit.Create(Self);
FEdit.Parent := Self;
FEdit.Text := '';
FEdit.Left := Gap;
FEdit.Top := Gap;
FEdit.Width := 130;
FEdit.Height := 24;
FEdit.OnEnter := EditEnter;

FButton := TButton.Create(Self);
FButton.Parent := Self;
FButton.Width := 22;
FButton.Left := FEdit.Left + FEdit.Width - FButton.Width - 2;
FButton.Top := FEdit.Top + 2;
FButton.Height := FEdit.Height - 4;
FButton.Font.Size := FEdit.Font.Size + 3;
FButton.OnClick := ButtonClick;

FEdit.Visible := True;
FButton.Visible := True;
end;


function TEditButton.GetEditReadOnly: Boolean;
begin
Result := FEdit.ReadOnly;
end;

function TEditButton.GetEditText: string;
begin
Result := FEdit.Text;
end;

function TEditButton.GetButtonText: string;
begin
Result := FButton.Caption;
end;

procedure TEditButton.SetEditReadOnly(const Value: Boolean);
begin
FEdit.ReadOnly := Value;
end;

procedure TEditButton.SetEditText(const Value: string);
begin
FEdit.Text := Value;
end;

procedure TEditButton.SetButtonText(const Value: string);
begin
FButton.Caption := Value;
end;

procedure TEditButton.WmSize(var Message: TMessage);
begin
inherited;
FEdit.Width := Width ;
FEdit.Height := Height;
FButton.Left := FEdit.Left + FEdit.Width - FButton.Width - 2;
FButton.Top := FEdit.Top + 2;
FButton.Height := FEdit.Height - 4;
end;

procedure TEditButton.ButtonClick(Sender: TObject);
begin
if assigned(FButtonClick) then FButtonClick(Self);
end;

destructor TEditButton.Destroy;
begin
FEdit.Free;
FButton.Free;
inherited;
end;

procedure TEditButton.EditEnter(Sender: TObject);
begin
if assigned(FEditEnter) then FEditEnter(Self);
end;

end.
 
zyycc,
我把你的代码编译了。EditButton1在Objcet Inspetor的Events页有两个事件:
OnButtonClick;
OnEditEnter;
两个事件都能有效运行。看来是你的系统太古老了。
(WIN2000,DELPHI6)
 
谢谢大家!呵呵,Huzzz,我的控件就这样被你拿跑了,呜呜呜...
 
不是我说你,你这个控件这么丑陋,用得着拿吗。。。
 

Similar threads

I
回复
0
查看
527
import
I
I
回复
0
查看
625
import
I
I
回复
0
查看
541
import
I
I
回复
0
查看
728
import
I
后退
顶部