WPF – loading grab and grabbing cursors from resource

Yan Cui

I help clients go faster for less using serverless technologies.

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;

Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game.
  2. Consulting: If you want to improve feature velocity, reduce costs, and make your systems more scalable, secure, and resilient, then let’s work together and make it happen.
  3. Join my FREE Community on Skool, where you can ask for help, share your success stories and hang out with me and other like-minded people without all the negativity from social media.

 

4 thoughts on “WPF – loading grab and grabbing cursors from resource”

  1. 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

  2. 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 :-)

Leave a Comment

Your email address will not be published. Required fields are marked *