WPF — loading grab and grabbing cursors from resource

Of all the cur­sors that we come across on a dai­ly basis, the grab image and grab­bing image cur­sors are two notable absen­tees in the list of sup­port­ed cur­sors in WPF/Silverlight.

So if you hap­pen to need these two cur­sors as I did ear­li­er in the day, then here’s a few easy steps to get you going:

1. Down­load the grab.cur and grabbing.cur files from here and here.

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

image

3. Make sure the Build Action for both is set to ‘Resource’ (which is sim­i­lar to Embed­ded Resource, except Resource is intend­ed for WPF/Silverlight and Embed­ded Resource is intend­ed for old­er tech­nolo­gies):

image

4. You won’t be able to put them in as Resources direct­ly, but you can put a ref­er­ence to them using dum­my TextBlock con­trols:

<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 ref­er­ences to these cur­sors in code like this:

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