App

How to detect No mouse movement

마피아9 2010. 2. 22. 02:36

SUNDAY, JUNE 8, 2008

How to detect NO mouse movement

In some occasions you might want to show or hide stuff in your Silverlight application when the visitor stopped moving his/her mouse.
For instance, most media players use this method to hide the play/pause/forward/mute controlbar.

You can achieve this by doing following this:

1. Create a DispatcherTimer

  1. private DispatcherTimer _nomousemoveTimer = new DispatcherTimer();  


2. Use the constructor of your class to create eventhandlers for events listed below and set the interval of the DispatcherTimer (in this case I set it to 3 seconds):
  1. this.MouseMove += new MouseEventHandler(Page_MouseMove);  
  2. this.Loaded += new RoutedEventHandler(Page_Loaded);  
  3. _nomousemoveTimer.Tick += new EventHandler(_nomousemoveTimer_Tick);  
  4. _nomousemoveTimer.Interval = new TimeSpan(0, 0, 3);  


3. In the Page Loaded, start the timer:
  1. _nomousemoveTimer.Start();  


4. Now, in the Page_MouseMove event, stop the timer and start it immediately after stopping it:
  1. _nomousemoveTimer.Stop();  
  2. _nomousemoveTimer.Start();  


5. Finally, in the Tick event of the timer, execute your code:
  1. private void _nomousemoveTimer_Tick(object sender, EventArgs e)  
  2. {  
  3.     //do your stuff  
  4. }