Mega Code Archive

 
Categories / Delphi / VCL
 

TLabel with rollover events

Title: TLabel with rollover events. Question: It's always bugged me having to write code to handle mouseenter and mouseleave events for labels. (For example, hyperlinks on web pages change colour when you move the mouse over them.) So, in order to rid myself of this problem, I threw together this *very* simple component that you can use instead of the TLabel. Answer: Do "File, New, Unit", highlight the whole unit and paste the code below over it. Save it as "MouseLabel.pas" and then install it as a component. That's all there is to it. You now have a label with mouseenter and mouseleave events. /////////////////////////////////////////////////////////////////// unit MouseLabel; interface uses Windows, Messages, SysUtils, Classes, Controls, StdCtrls; type TMouseLabel = class(TLabel) private FOnMouseEnter, FOnMouseLeave : TNotifyEvent; procedure CMMouseEnter(var Msg: TMessage); message CM_MOUSEENTER; procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE; published property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter; property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave; end; procedure Register; implementation procedure Register; begin RegisterComponents('JCM', [TMouseLabel]); end; procedure TMouseLabel.CMMouseEnter(var Msg: TMessage); begin inherited; if Assigned(FOnMouseEnter) then FOnMouseEnter(Self); end; procedure TMouseLabel.CMMouseLeave(var Msg: TMessage); begin inherited; if Assigned(FOnMouseLeave) then FOnMouseLeave(Self); end; end.