读MD6中...制作了一个感应MouseEnter,MouseLeave的mybutton,大家指正(10分)

  • 主题发起人 主题发起人 ahxia
  • 开始时间 开始时间
A

ahxia

Unregistered / Unconfirmed
GUEST, unregistred user!
因为读 MD 6有感,学做了一个感知MouseLeave,MouseEnter事件的button,
请大侠们指正一下。(好像简单的不应该会有错误了 :) 。。。

unit myButton;

interface

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

type
TmyButton = class(TButton)
private
fMouseEnter: TNotifyEvent;
fMouseLeave: TNotifyEvent;
protected
procedure cmMouseLeave(var msg: TMessage); message cm_MouseLeave;
procedure cmMouseEnter(var msg: TMessage); message cm_MouseEnter;
public
{ Public declarations }
published
property OnMouseEnter: TNotifyEvent read fMouseEnter write fMouseEnter;
property OnMouseLeave: TNotifyEvent read fMouseLeave write fMouseLeave;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('my', [TmyButton]);
end;

{ TmyButton }

procedure TmyButton.cmMouseEnter(var msg: TMessage);
begin
if Assigned(fMouseEnter) then
fMouseEnter(self);
end;

procedure TmyButton.cmMouseLeave(var msg: TMessage);
begin
if Assigned(fMouseLeave) then
fMouseLeave(self);
end;

end.
 
如果连这个也有错误,就说不过去了。[:D]
 
有一个问题,就是从TButton继承好呢? 还是TCustomButton更好?
 
又想了一下, 是不是这样更好些? 似乎更加符合封装的要求...
unit myButton;

interface

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

type
TmyButton = class(TButton)
private
fMouseEnter: TNotifyEvent;
fMouseLeave: TNotifyEvent;
procedure cmMouseLeave(var msg: TMessage); message cm_MouseLeave;
procedure cmMouseEnter(var msg: TMessage); message cm_MouseEnter;
protected
procedure MouseLeave; dynamic;
procedure MouseEnter; dynamic;
public
{ Public declarations }
published
property OnMouseEnter: TNotifyEvent read fMouseEnter write fMouseEnter;
property OnMouseLeave: TNotifyEvent read fMouseLeave write fMouseLeave;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('my', [TmyButton]);
end;

{ TmyButton }
procedure TmyButton.MouseEnter;
begin
if Assigned(fMouseEnter) then
fMouseEnter(self);
end;

procedure TmyButton.MouseLeave;
begin
if Assigned(fMouseLeave) then
fMouseLeave(self);
end;

procedure TmyButton.cmMouseEnter(var msg: TMessage);
begin
MouseEnter;
end;

procedure TmyButton.cmMouseLeave(var msg: TMessage);
begin
MouseLeave;
end;

end.
 
多人接受答案了。
 

Similar threads

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