Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #28: How to Implement a Custom Mouse Cursor

In Tip of the Day #27 we talked about how to change the mouse cursor. But what if you want to have a cursor icon that is not supported by Silverlight? This tutorial will show you how to do it.

Demo (Silverlight 3 Beta 1 required):

The first thing you will want to do is to hide the mouse cursor at the root level of your Silverlight application. In our Page.xaml, we set the Canvas Mouse to “None”:

MainPage.xaml:

<UserControl x:Class="CustomCursor.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Canvas x:Name="LayoutRoot" Cursor="None"  Background="White">
    </Canvas>
</UserControl>

Next, create a customer Silverlight user control called CustomCursor. The only thing this control need to contain is a Image control.

Cursor.XAML:

<UserControl x:Class="CustomCursor.CustomCursor"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Image x:Name="MyCursor"></Image>
</UserControl>

In the code behind for the CustomCursor class add a method to set the image and another method to position the control.

Cursor.XAML.cs:

namespace CustomCursor
{
    public partial class CustomCursor : UserControl
    {
        public CustomCursor()
        {
            InitializeComponent();
        }
 
        public void SetCursor(string resource)
        {
            Uri uri = new Uri(resource, UriKind.Relative);
            MyCursor.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
        }
 
        public void MoveTo(Point pt)
        {
            this.SetValue(Canvas.LeftProperty, pt.X); 
            this.SetValue(Canvas.TopProperty, pt.Y);
        }
    }
}

Finally, go back to MainPage.xaml.cs and add an instance of your CustomCursor to your Canvas. Also, monitor for the MouseMove event so you can position the cursor to be at the location of your mouse.

namespace CustomCursor
{
    public partial class MainPage : UserControl
    {
        CustomCursor customCursor = new CustomCursor();
 
        public MainPage()
        {
            InitializeComponent();
 
            customCursor.SetCursor("fireball.png");
 
            LayoutRoot.Children.Add(customCursor);
 
            this.MouseMove += new MouseEventHandler(MainPage_MouseMove);
        }
 
        void MainPage_MouseMove(object sender, MouseEventArgs e)
        {
            customCursor.MoveTo(e.GetPosition(null));
        }
    }
}

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

miguelito928 said:

Mike,

Appreciate the posts you are writing.  Keep them coming!

Your demo in this post is a little bit off in its logic.  You are able to move the custom cursor out of the red box if you continue to travel in a postive X and/or Y direction.  The instance your delta between cursor moves is in a negative X and/or Y direction, the custom cursor jumps back into the red box.

Perhaps you are just missing an Absolute Value in your calculations?

# August 18, 2008 3:17 PM

Microsoft Weblogs said:

In Tip of the Day #27 we talked about how to change the mouse cursor. But what if you want to have a

# August 18, 2008 3:24 PM

Mark Mazurik said:

Do you also want to wire up MouseEnter to set Visibility to Visible and MouseLeave to Collapsed?

# August 18, 2008 4:30 PM

mike.snow said:

Miguel - The idea was to change the mouse cursor only if you are over the Silverlight control, which is the red box. Once you leave the control it should revert to the default cursor.

Mark - Yes, you can do that. This is only meant to get people going on the right track.

# August 18, 2008 5:02 PM

jordanhammond said:

Fine if you want a square cursor, but what about if you want a cross arrow (move cursor)..  Can a PNG image with transparancy be used so you don't end up with white space between the arrow's points?

# August 19, 2008 3:14 AM

Silverlight news for August 19, 2008 said:

Pingback from  Silverlight news for August 19, 2008

# August 19, 2008 3:46 AM

Dew Drop - August 19, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - August 19, 2008 | Alvin Ashcraft's Morning Dew

# August 19, 2008 9:04 AM

mike.snow said:

Jordan Hammond - Yes, you can use a PNG image with transparency. I have confirmed this works fine.

# August 19, 2008 3:51 PM

Mirrored Blogs said:

Post: Approved at: Aug-19-2008 Silverlight heating up with Grid Girls Seems Silverlight is really getting

# August 19, 2008 3:59 PM

Community Blogs said:

Martin Mihaylov on Resizing the SL Object, Dan Wahlin on the Grid and a Flyout StackPanel, Robby Ingebretsen

# August 19, 2008 5:17 PM

mr.saif said:

It is was very helpful to me. Thanks :)

# August 20, 2008 2:32 AM

Brauliod said:

Wow !

Quite interesting. The good news of this post is that we have an smart workaround for the custom cursor issue, but... Is going Ms to add some standard interface to allow changing the mouse cursor to a custom one ?

# August 24, 2008 10:01 AM

mike.snow said:

Hi Brauliod, not that I am aware of. Thanks.

# August 24, 2008 4:59 PM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #32 &#160; Title : How to Declare a Custom User Control from a XAML Page.

# August 26, 2008 1:14 PM

News said:

Silverlight Tip of the Day #32 Title : How to Declare a Custom User Control from a XAML Page. Demo :

# August 26, 2008 2:57 PM

Kieren5 said:

This too hard for what it is, and has serious limitations. To recreate, add a control that consumes mouse events (such as Slider control), to the LayoutRoot grid element.

Various controls in Silverlight, consume various Mouse events, e.Handled = true, such as MouseEnter, MouseLeave, and MouseMove.

The problem with routed events in Silverlight is that they only bubble, there is NO tunnelling. You don't know which controls handle which events, until runtime, which is when your container control doesn't get the event.

In Wpf, there are preview events (the tunnelling in a routed event), so when a control does handle an event, you still get a chance to see the event fire in the preview event.

I have a simple scenario, that I can't implement in Silverlight because of the incomplete routed mouse events:

silverlight.net/.../21154.aspx

# August 31, 2008 6:20 AM

shamrat231 said:

Hi, for some reason the demo url...as i run it,it donot show the icon in released 2.0 version. Could u tell me what i need to change on the code behind for it to work

Sharker Khaleed Mahmud(MCPD)

# November 2, 2008 3:20 AM

mike.snow said:

Shamrat - I need to update the Demo's to RTM version. Will be doing that soon.

# November 3, 2008 1:15 PM

shamrat231 said:

Please do provide a download copy of the working source code in RTM.

Sharker Khaleed Mahmud(MCPD)

# November 4, 2008 9:14 AM

edhalsim said:

Hi,

I tried creating a cross cursor with a transparent background using Paint.Net, but when it shows in my Silverlight application the transparent part shows up as a black square.  Any ideas what I may be doing wrong?  Thanks.

- Ed.

# December 27, 2008 11:41 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

mike.snow said:

edhalsim - Make certain you are using a PNG with the transparent parts "cut out". See this blog for a tutorial on how to do it:

silverlight.net/.../part-vii-creating-transparent-png-files-for-silverlight.aspx

# January 5, 2009 12:30 PM