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

How to Play a Sound When the Mouse Enters a Component

By Zarko Gajic, About.com

All descendants of TComponent send a CM_MOUSEENTER and CM_MOUSELEAVE messages when the mouse enters and leaves the bounds of the component. You will need to write a message handler for the respective messages if you wish to respond to them - and play a sound, for example, when the mouse "enters" the component.

~~~~~~~~~~~~~~~~~~~~~~~~~
uses MMSystem, ...

   TMyComponent = class(TComponent)
   private
     procedure CMMouseEnter(var msg: TMessage) ; message CM_MOUSEENTER;
     procedure CMMouseLeave(var msg: TMessage) ; message CM_MOUSELEAVE;
   end;

implementation

procedure TMyComponent.CMMouseEnter(var msg: TMessage) ;
begin
   sndPlaySound('c:\mysound.wav', snd_Async or snd_NoDefault)
end;

procedure TMyComponent.CMMouseLeave(var msg: TMessage) ;
begin
   sndPlaySound(nil, snd_Async or snd_NoDefault) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Creating Custom Code Templates in Delphi 2006
« Delphi IDE (Code Editor) Keyboard Shortcuts

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TMouse
  6. How to Play a Sound When the Mouse Enters a (Delphi) Component

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

All rights reserved.