怎么样定义一个鼠标离开的事件?(100分)

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

zbc

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好,我现在从TShape重新派生了一个类,为了响应鼠标离开的事件,我想拦截CM_MOUSELEAVE消息来处理
但是现在我不太清楚,如何才能创建一个可发布的事件以响应鼠标的离开动作。请教!谢谢!
 
搜索onmouseleave,
有很多相关的帖子
 
定义消息
 
我要的是如何用虚拟函数来点燃事件,请给代码,测试通过,马上结贴!
 
{TMyEdit}
TMyEdit=Class(TEdit)
private
FOnMouseLeave,FOnMouseEnter: TNotifyEvent;
procedure WZMouseLeave(var Msg:TMessage);message CM_MOUSELEAVE;
procedure WZMouseEnter(var Msg:TMessage);message CM_MOUSEENTER;
public
published
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
end;

procedure TMyEdit.WZMouseEnter(var Msg: TMessage);
begin
inherited;
if Assigned(FonMouseEnter) then
FOnMouseEnter(Self);
end;

procedure TMyEdit.WZMouseLeave(var Msg: TMessage);
begin
inherited;
if Assigned(FonMouseLeave) then
FOnMouseLeave(Self);
end;
 
下面代码生成一个有鼠标到达和离开事件的图片的控件
TAdmyImage = class(TImage)
private
{ Private declarations }
FArrive: TNotifyEvent;
FLeave : TNotifyEvent;
procedure CMMouseEnter(var AMsg: TMessage);
message CM_MOUSEENTER;
procedure CMMouseLeave(var AMsg: TMessage);
message CM_MOUSELEAVE;
procedure SetArrive(Value: TNotifyEvent);
procedure SetLeave(Value: TNotifyEvent);
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property OnMouseArrive:TNotifyEvent read FArrive write SetArrive;
property OnMouseLeave: TNotifyEvent read FLeave write SetLeave;
end;

procedure Register;
implementation
procedure Register;
begin
RegisterComponents('AdmyCom', [TAdmyImage]);
end;

procedure TAdmyImage.SetArrive(Value: TNotifyEvent);
begin
FArrive:=Value;
end;

procedure TAdmyImage.SetLeave(Value: TNotifyEvent);
begin
FLeave:=Value;
end;

procedure TAdmyImage.CMMouseEnter(var AMsg: TMessage);
begin
if assigned(FArrive)then
OnMouseArrive(self);
end;

procedure TAdmyImage.CMMouseLeave(var AMsg: TMessage);
begin
if assigned(FLeave)then
OnMouseLeave(self);
end;
 
这是论c++builder的吧,能用c++builder来写吗?
 
纯delphi代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
private
procedure CMMouseEnter(var Msg: TMessage);
message CM_MOUSEENTER;
procedure CMMouseLeave(var Msg: TMessage);
message CM_MOUSELEAVE;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CMMouseEnter(var Msg: TMessage);
var
anObject: TObject;
begin
anObject := TObject(Msg.lParam);
if anObject <> nil then
begin
if (anObject = Edit1) then
begin
ShowMessage('saf');
end;
end;
end;

procedure TForm1.CMMouseLeave(var Msg: TMessage);
var
anObject: TObject;
begin
anObject := TObject(Msg.lParam);
if anObject = nil then
begin

end;
end;
end.

 
可以利用控件中的一个属性!即交点属性,当光标一离开后就触发该事件了。
 
后退
顶部