WPF – loading grab and grabbing cursors from resource

Of all the cursors that we come across on a daily basis, the grab image and grabbing image cursors are two notable absentees in the list of supported cursors in WPF/Silverlight.

So if you happen to need these two cursors as I did earlier in the day, then here’s a few easy steps to get you going:

1. Download the grab.cur and grabbing.cur files from here and here.

2. Include them in your project, under a Resources/Cursors folder, like this:

image

3. Make sure the Build Action for both is set to ‘Resource’ (which is similar to Embedded Resource, except Resource is intended for WPF/Silverlight and Embedded Resource is intended for older technologies):

image

4. You won’t be able to put them in as Resources directly, but you can put a reference to them using dummy TextBlock controls:

<UserControl.Resources>
    <ResourceDictionary>
        <TextBlock x:Key="CursorGrab" Cursor="Resources/Cursors/grab.cur"/>
        <TextBlock x:Key="CursorGrabbing" Cursor="Resources/Cursors/grabbing.cur"/>
    </ResourceDictionary>
</UserControl.Resources>

5. Now you can retrieve the references to these cursors in code like this:

_grabCursor = ((TextBlock) Resources["CursorGrab"]).Cursor;
_grabbingCursor = ((TextBlock) Resources["CursorGrabbing"]).Cursor;
  • Alexander

    Super! No conversation and no win32 wrapper. very nice.
    thanx

  • http://compilewith.net Paul Jackson

    Great post, and thanks for the cursor resources.
    Is there any particular reason that you chose to use a TextBlock over FrameworkElement–where the Cursor property is first defined–to cache the cursor references?

    -PJ

  • http://CyberWerx.co.uk William Dickinson

    Many thanks for the post and the .cur files

  • http://www.porombka.pl Paul Porombka

    HI there, the better solution is to use .NET built-in converter – CursorConverter.

    Just write small code:

    public static Cursor LoadCursorFromResource(string resourceName)
    {
    CursorConverter cc = new CursorConverter();
    Cursor cursor = cc.ConvertFrom(resourceName) as Cursor;
    return cursor;
    }

    Important thing is to set as a parameter full path to the cursor, incuding assembly name:

    e.g. /MyAssemblyExample;component/Resources/MyGreatCursor.cur

    Now You can load any cursor – including *.ani :-)