实际上用同样的方法就行了,只是将父类为Tbutton改为TSpeedButton就行了,DELPHI生成模板文件后,将: FOnMouseLeave: TNotifyEvent; procedure WZMouseLeave(var Msg:TMessage); message CM_MOUSELEAVE;property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;procedure TButton1.WZMouseLeave(var Msg: TMessage); begin inherited;//继承父类 if csLButtonDown in ControlState then begin Self.MouseUp(mbLeft,[ssLeft],0,0); end; if Assigned (FonMouseLeave) then FOnMouseLeave(Self); end;这些一点不动的照抄上去,编译就可以了,我已经实现了。
调用 SETCAPTURE()SPEEDBUTTON1.MOUSEMVOE(C)
对了, SPEEDBUTTON 没有句柄
用消息机制tmessage获得相应的object的名字类型做比较就可以了
这样吧,给一个例子,看着改改:为TButton增加MouseLeave事件(转)
DELPHI等快速编程工具,使用最多的一定是各种控件了,用自带的控件编出的程序往往千篇一律,为了使自己的程序更个性化或者为了使控件的功能更强,我们需要自己编控件,大家不要以为自己编控件好难,看完这篇文章,保证人人都能编自己的控件,就算编不了控件,了解了解控件的原理也是不错的,就算不能了解控件的原理,学习学习……:)
首先,我们做的这个控件是为了给TBotton控件增加Mouseleave事件,有了这个事件,我们就可以编出类似网页中的悬停按钮的效果,首先打开Delphi,选Component|new Component出现对话框, 我们的控件类名为Tbutton1,父类为Tbutton,库单元文件名为Button1。单击确定按钮,Component Wizard粗略的生成模板样式的代码,其中有类声明,全局变量声明及传递到RegisterComponent方法中的参数等,编译好的整个文件的程序清单如下:unit Button1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TButton1 = class(TButton)private FOnMouseLeave: TNotifyEvent;
procedure WZMouseLeave(var Msg:TMessage); message CM_MOUSELEAVE;
{ Private declarations }protected
{ Protected declarations }public
{ Public declarations }published
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
{ Published declarations }
end;procedure Register;implementationprocedure Register;
begin
//在系统中注册控件
RegisterComponents('Samples', [TButton1]);
end;
{ TButton1 }procedure TButton1.WZMouseLeave(var Msg: TMessage);
begin
inherited;//继承父类
if csLButtonDown in ControlState then
begin
Self.MouseUp(mbLeft,[ssLeft],0,0);
end;
if Assigned (FonMouseLeave) then FOnMouseLeave(Self);
end;
end. 代码添加完后,编译后,一个名为Button1的控件即加入Simples项中,新建一个项目,试试以下的代码:procedure TForm1.Button11MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
label1.Caption:='在';
end;procedure TForm1.Button11MouseLeave(Sender: TObject);
begin
label1.Caption:='不在';
end;怎么样,是不很简单。(话后音:快,干嘛?试代码。)以上在Delphi5.0调试通过。
怎么编译?
FOnMouseLeave: TNotifyEvent;
procedure WZMouseLeave(var Msg:TMessage); message CM_MOUSELEAVE;property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;procedure TButton1.WZMouseLeave(var Msg: TMessage);
begin
inherited;//继承父类
if csLButtonDown in ControlState then
begin
Self.MouseUp(mbLeft,[ssLeft],0,0);
end;
if Assigned (FonMouseLeave) then FOnMouseLeave(Self);
end;这些一点不动的照抄上去,编译就可以了,我已经实现了。