Benjamin Roux

Silverlight Expert - Silverlight Fan - MVP Client App Dev

[Silverlight] A busy mouse pointer

Hello everyone,

Today I’m gonna present you a control which can be really useful, since you can find him in Windows: the busy mouse pointer. This control is used for notice the user than something is happening.

loading

 

The code for this control is quite simple.

#region Dependency Properties
 
public bool IsBusy
{
    get { return (bool)GetValue(IsBusyProperty); }
    set 
    { 
        SetValue(IsBusyProperty, value);
        ChangeState();
    }
}
 
// Using a DependencyProperty as the backing store for IsBusy.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsBusyProperty =
    DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyPointer), new PropertyMetadata(new PropertyChangedCallback(OnBusyChanged)));
 
private static void OnBusyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    ((BusyPointer)sender).IsBusy = (bool)e.NewValue;
}
 
#endregion
 
#region Initialization
 
public BusyPointer()
{
    this.DefaultStyleKey = typeof(BusyPointer);
}
 
#endregion
 
#region Methods
 
private void ChangeState()
{
    if (IsBusy) VisualStateManager.GoToState(this, "Busied", true);
    else VisualStateManager.GoToState(this, "Normal", true);
}
 
#endregion

Juste a very simple control with a IsBusy property. When the value is true the animation starts, when it’s false it stops.

And the theme I use.

<Style TargetType="local:BusyPointer">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:BusyPointer">
                <Grid x:Name="LayoutRoot" Background="Transparent">
                    <vsm:VisualStateManager.VisualStateGroups>
                        <vsm:VisualStateGroup x:Name="CommonStates">
                            <vsm:VisualState x:Name="Normal" />
                            <vsm:VisualState x:Name="Busied">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="VisualElement" Storyboard.TargetProperty="(UIElement.RenderTransform).Angle" RepeatBehavior="Forever">
                                        <SplineDoubleKeyFrame KeyTime="0:0:1" Value="360" />
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>
                    <Ellipse Width="25" Height="25" StrokeThickness="5.5" x:Name="VisualElement">
                        <Ellipse.Stroke>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF096475" Offset="0.571"/>
                                <GradientStop Color="#FFA8FCFC" Offset="1"/>
                            </LinearGradientBrush>
                        </Ellipse.Stroke>
                        <Ellipse.RenderTransform>
                            <RotateTransform CenterX="12.5" CenterY="12.5" />
                        </Ellipse.RenderTransform>
                    </Ellipse>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

You can change the colors, the animation, the shape, everything!

Have fun!

Comments

[Silverlight] A busy mouse pointer - Benjamin Roux said:

Pingback from  [Silverlight] A busy mouse pointer - Benjamin Roux

# January 11, 2010 12:35 PM

Nathan Brouwer said:

Isn't this the same as the Cursors.Wait ? Anyway, nice theme... :)

# January 11, 2010 2:33 PM

Benjamin Roux said:

Hum... Yeah, but you can use it in a control for example (like the BusyIndicator control) :)

I'll post another control using it.

# January 11, 2010 2:45 PM

Benjamin Roux said:

Re, Ok the title is not really explicit but in this post we’re going to see an image control which display

# January 11, 2010 3:08 PM

[Silverlight] An image loading control | I love .NET! said:

Pingback from  [Silverlight] An image loading control | I love .NET!

# January 11, 2010 5:46 PM

Twitter Mirror said:

A busy mouse pointer for Silverlight http://weblogs. asp.net /broux/archive/2010/01/11/silverlight-a

# January 14, 2010 7:13 AM

Twitter Trackbacks for [Silverlight] A busy mouse pointer - Benjamin Roux [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 [Silverlight] A busy mouse pointer - Benjamin Roux         [asp.net]        on Topsy.com

# January 14, 2010 10:31 AM

Miguel Madero said:

I don't get it, is this a pointer or just something we'll drop in the screen?

# January 16, 2010 8:47 PM

Benjamin Roux said:

Both. It can be used as an alternative for Cursors.Wait or be used as a full part control (like the BusyIndicator).

# January 17, 2010 1:38 AM

Lama Chandra said:

Can you please post the usercontrol code as a zip file? Thanks.

# January 19, 2010 4:30 PM

Benjamin Roux said:

Done! (I updated the post)

# January 19, 2010 4:50 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)