怎样写组件的事件,各位高手能就个例子吗!(100分)

  • 主题发起人 主题发起人 haiyang2008
  • 开始时间 开始时间
H

haiyang2008

Unregistered / Unconfirmed
GUEST, unregistred user!
本人菜鸟一个,老板让写组件,无奈请教各位大虾!!
能让本菜鸟明白者,奖分100
 
通过拦截消息,可以参考delphi中组件的代码。
下面贴个代码给你。是以前回复问题时写的。

//添加鼠标移入移出事件及图片切换

unit SpeedButtonEx;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, Buttons, Graphics;

type
TSpeedButtonEx = class(TSpeedButton)
private
FEnterGlyph: TBitmap;
FLeaveGlyph: TBitmap;
FOnMouseLeave: TNotifyEvent;
FOnMouseEnter: TNotifyEvent;

procedure SetEnterGlyph(Value: TBitmap);
procedure SetLeaveGlyph(Value: TBitmap);

procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy;override;
published
property EnterGlyph: TBitmap read FEnterGlyph write SetEnterGlyph; //移入图片
property LeaveGlyph: TBitmap read FLeaveGlyph write SetLeaveGlyph; //移出图片
property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter; //移入事件
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave; //移出事件

end;

procedure Register;

implementation

constructor TSpeedButtonEx.Create(AOwner: TComponent);
begin
inherited;
FEnterGlyph := TBitmap.Create;
FLeaveGlyph := TBitmap.Create;
end;

destructor TSpeedButtonEx.Destroy;
begin
FEnterGlyph.Free;
FLeaveGlyph.Free;
inherited;
end;

procedure TSpeedButtonEx.SetEnterGlyph(Value: TBitmap);
begin
FEnterGlyph.Assign(Value);
end;

procedure TSpeedButtonEx.SetLeaveGlyph(Value: TBitmap);
begin
FLeaveGlyph.Assign(Value);
end;

procedure TSpeedButtonEx.CMMouseEnter(var Message: TMessage); //message CM_MOUSEENTER;
begin
if not FEnterGlyph.Empty then
Glyph.Assign(FEnterGlyph);
inherited;
if Assigned(FOnMouseEnter) then
FOnMouseEnter(Self);
end;

procedure TSpeedButtonEx.CMMouseLeave(var Message: TMessage); //message CM_MOUSELEAVE;
begin
if not FLeaveGlyph.Empty then
Glyph.Assign(FLeaveGlyph);
inherited;
if Assigned(FOnMouseLeave) then
FOnMouseLeave(Self);
end;

procedure Register;
begin
RegisterComponents('dwh', [TSpeedButtonEx]);
end;

end.
 
接受答案了.
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部