⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 为tbutton增加mouseleave事件 .txt

📁 为TButton增加MouseLeave事件
💻 TXT
字号:
    
 
为TButton增加MouseLeave事件 

作者: 万重
Wednesday, September 4 2002 3:33 PM 
 
DELPHI等快速编程工具,使用最多的一定是各种控件了,用自带的控件编出的程序往往千篇一律,为了使自己的程序更个性化或者为了使控件的功能更强,我们需要自己编控件,大家不要以为自己编控件好难,看完这篇文章,保证人人都能编自己的控件,就算编不了控件,了解了解控件的原理也是不错的,就算不能了解控件的原理,学习学习……:)

首先,我们做的这个控件是为了给TBotton控件增加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:='在'; 
end; 

procedure TForm1.Button11MouseLeave(Sender: TObject); 
begin 
 label1.Caption:='不在'; 
end; 

  怎么样,是不很简单。以上在Delphi5.0调试通过。
 

 
 
 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -