WPF – loading grab and grabbing cursors from resource

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

Don’t reinvent the patterns. Catalyst gives you consistent APIs for messaging, data, and workflow with key microservice patterns like circuit-breakers and retries for free.

Try the Catalyst beta

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. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

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 *