I want to use an image or icon as a custom cursor in WPF app. How can I do that?
You have two basic options:
When the mouse cursor is over your control, hide the system cursor by setting this.Cursor = Cursors.None;
and draw your own cursor using whatever technique you like. Then, update the position and appearance of your cursor by responding to mouse events. Here are two examples:
http://www.hanselman.com/blog/DeveloperDesigner.aspx
Additional examples can be found here:
WPF Tutorial - How To Use Custom Cursors
Create a new Cursor object by loading an image from a .cur or .ani file. You can create and edit these kinds of files in Visual Studio. There are also some free utilites floating around for dealing with them. Basically they're images (or animated images) which specify a "hot spot" indicating what point in the image the cursor is positioned at.
If you choose to load from a file, note that you need an absolute file-system path to use the Cursor(string fileName)
constructor. Lamely, a relative path or Pack URI will not work. If you need to load the cursor from a relative path or from a resource packed with your assembly, you will need to get a stream from the file and pass it in to the Cursor(Stream cursorStream)
constructor. Annoying but true.
On the other hand, specifying a cursor as a relative path when loading it using a XAML attribute does work, a fact you could use to get your cursor loaded onto a hidden control and then copy the reference to use on another control. I haven't tried it, but it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With