4
votes

I have a set of cursors (.cur files) that I want to use with my WPF/VB.net application, without changing the cursors system-wide. I am assuming that I would somehow use each WPF object's "cursor" property, but I'm not sure how to use my own cursors.

What should I do to accomplish this?

3

3 Answers

0
votes

Have you tried using the file path overload to create a cursor?

Cursor cursor = new Cursor("<path>");

Or the stream for that matter?

Once you have a cursor object you can assign it to the controls where it should be shown. (FrameworkElement.Cursor)


If you use the cursor as a resource, e.g. in a cursors folder in your project

screen

you can reference it anywhere in your XAML, e.g.

<Window Cursor="Cursors/wait_il.cur">...
0
votes

assuming that cursor is in /Resources/ folder and build action is set to Resource:

declare:

<TextBlock x:Key="MyCursor" Cursor="/Resources/grab.cur" />

Then apply to the main window in initializer:

this.Cursor = (FindResource("MyCursor") as TextBlock).Cursor;
0
votes

Ok since H.B was winging at me here is a class :p

public class CustomCursor
{
    private System.Windows.Input.Cursor _cursor = null;
    public System.Windows.Input.Cursor Cursor
    {
        get
        {
            if (_cursor == null)
                _cursor = GetCursor();
            return _cursor;
        }
    }

    public string RelativePath { get; set; }

    public CustomCursor()
    {
    }

    public CustomCursor(string relativePath)
    {
        RelativePath = relativePath;
    }

    public System.Windows.Input.Cursor GetCursor()
    {
        if (RelativePath == null)
            throw new ArgumentNullException("You must set RelativePath first");

        string directory = Directory.GetCurrentDirectory();
        string absPath = directory + '\\' + RelativePath;

        if (!File.Exists(absPath))
            throw new FileNotFoundException();

        return new System.Windows.Input.Cursor(absPath);
    }
}

Use in code behind like this:

this.Cursor = new CustomCursor("grab.cur").Cursor;

Or declare in xaml:

<local:CustomCursor x:Key="MyCursor" RelativePath="grab.cur"/>

And reference like this:

this.Cursor = (FindResource("MyCursor") as CustomCursor).Cursor;