2

I'm creating a WinForms application in Visual C#, and would like to change the cursor to a custom image (.cur) that I created. I want the cursor image to change to this over the form window only.

All previously answered questions suggest setting this.cursor to a new cursor object created with the path to the cursor, with statements like these:

this.Cursor = new Cursor(@"C:\green.cur"); // this
this.Cursor = new Cursor(GetType(), @"C:\green.cur"); // or this

but both statements result in the same error :

Image format is not valid. The image file may be corrupted.

I've also tried importing the cursor with LoadImage after importing user32.dll as suggested here but this resulted in the error :

Win32 handle passed to Cursor is not valid or is the wrong type.

I'm able to set the cursor to the standard wait cursor with the following statement :

this.Cursor = Cursors.WaitCursor;

The same .cur file works perfectly when used to set the cursor in a C++ program where the cursor is embedded in a resource.

1 Answer 1

2

You need to use LoadCursorFromFile:

[DllImport("user32.dll")]
static extern IntPtr LoadCursorFromFile(string lpFileName);

and when you want to change your cursor:

IntPtr cursor = LoadCursorFromFile(@"C:\green.cur");
this.Cursor = new Cursor(cursor);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. This seems to restrict the cursor size to 32 * 32. Is there any way I could load a larger cursor? The .cur file is currently 128 * 128.
To change the size you need to use the method SetSystemCursor which will change the cursor of the computer not only your program. Are you sure that this is what you wise for?
No, I want it changed just over the program. LoadImage does this in C++, and has parameters to set the cursor size ( and also resizes the cursor if necessary ). I was wondering if you could find some way to resize the cursor without using LoadImage, or tell me what could possibly be wrong with using LoadImage as I have. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.