我做的这个控件是为了给TButton控件增加Mouseleave事件,有了这个事件,我们就可以编出类似网页中的悬停按钮的效果,打开Delphi,选Component|new Component出现如下的画面,
我的控件类名为TButton1,父类为TButton,库单元文件名为Button1.单击确定按钮,Component Wizard粗略的生成模板样式的代码,其中有类声明,全局变量声明及传递到RegisterComponent方法中的参数等,编译好的整个文件的程序清单如下:
unit Button1;
interface
uses
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;
implementation
procedure 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:='ll';
end;
procedure TForm1.Button11MouseLeave(Sender: TObject);
begin
label1.Caption:='dd';
end;
至于换图片,还不简单