Monday, April 09, 2012

Reactive Extensions & WPF - Hiding mouse pointer when idle

Ever wanted to easily hide the mouse pointer after x seconds of idle activity in your WPF application? There are many ways to accomplish this, here is one way you might find useful: the MouseIdle Behavior extension.

This attachable behavior uses Reactive Extensions (Rx) to handle mouse movement and a normal DispatcherTimer to Control the interval routine.

Usage is as simple as adding the namespace to the declaration of your window:

xmlns:extensions="clr-namespace:Usoniandream.WPF.Extensions;assembly=Usoniandream.WPF.Extensions"

After that all you have to do is add the code to the interactivity block, like this:

 <i:Interaction.Behaviors>
        <extensions:MouseIdleBehavior
                TimeToIdle="3000" />
 i:Interaction.Behaviors>

The complete source is available for download at the following url: https://github.com/peterdrougge/Code-Samples/blob/master/WPF/Behaviors/MouseIdleBehavior.cs

Maybe you'll find this useful :)

P.

No comments: