Does anyone know of an easy way to use a custom cursor? I have both a .cur and .png of my cursor. I tried adding it as a resource to my project and also tried including it as a file in the project. Ideally I'd like to embed it but I just want to get it working.
When I use Cursor cur = new Cursor("mycursor.cur") I get "Image format is not valid. The image file may be corrupted". I tried this http://mahesg.wordpress.com/2008/02/09/embedding-cursor/ but it didn't work. Using WinForm1.Properties.Resources.mycursor returns a byte[] which I have no idea how to convert into a Cursor type.
For some reason the cursor class is far too picky about what it will read. You can create the handle yourself using the windows API then pass that to the cursor class.
C#:
//(in a class)
public static Cursor ActuallyLoadCursor(String path) {
return new Cursor(LoadCursorFromFile(path))
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr LoadCursorFromFile(string fileName);
VB.Net:
'(in a class)'
Public Shared Function ActuallyLoadCursor(path As String) As Cursor
Return New Cursor(LoadCursorFromFile(path))
End Function
<System.Runtime.InteropServices.DllImport("user32.dll")>
Private Shared Function LoadCursorFromFile(fileName As String) As IntPtr
End Function
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