1. Home
  2. Computing & Technology
  3. Delphi Programming

How to "HotTrack" any control

By , About.com Guide

HotTracking is used when we want to highlight components when the mouse passes over them; for example we could hot track an URL label component.

Code below "HotTracks" Labels and Check Boxes, with a little modification use it to HotTrack any control.

~~~~~~~~~~~~~~~~~~~~~~~~~
{We have to override the WndProc procedure}
procedure WndProc(var Message : TMessage) ; override ;

procedure TForm1.WndProc(var Mesg : TMessage) ;
begin
{ Here we see which component gets changed.
  This bit here tells us which
  component the mouse is over }
     if Mesg.LParam = Longint(Label1) then
        ChangeColor(Label1, Mesg.Msg) ;
     if Mesg.LParam = Longint(Label2) then
        ChangeColor(Label2, Mesg.Msg) ;
     if Mesg.LParam = Longint(Label3) then
        ChangeColor(Label3, Mesg.Msg) ;
     if Mesg.LParam = Longint(CheckBox1) then
        ChangeColor(CheckBox1, Mesg.Msg) ;

   inherited WndProc(Mesg) ;
end;

procedure TForm1.ChangeColor
   (Sender : TObject; Msg : Integer) ;
Begin
{ If a label is the one that the mouse
is over then we do this }
  if Sender IS TLabel Then
   begin
     if (Msg = CM_MOUSELEAVE) then
      (Sender As TLabel).Font.Color:=clWindowText;
     if (Msg = CM_MOUSEENTER) then
      (Sender As TLabel).Font.Color:=clBlue;
   end;
{ If a CheckBox is the one ... }
  if Sender IS TCheckBox Then
   begin
    if (Msg = CM_MOUSELEAVE) then
     (Sender As TCheckBox).Font.Color:=clWindowText ;
    if (Msg = CM_MOUSEENTER) then
     (Sender As TCheckBox).Font.Color:=clRed ;
   end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Send an email (with attachment) from Outlook
« Abort a loop by pressing a key

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TLabel, TStaticText
  6. How to "HotTrack" any control

©2009 About.com, a part of The New York Times Company.

All rights reserved.