1. Computing & Technology

Discuss in my forum

How to Play a Sound When the Mouse Enters a Component

By , About.com Guide

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

©2012 About.com. All rights reserved.

A part of The New York Times Company.