FAQ Item: How to set the cursor of a windows form using Visual C#.NET?
Locked
-
Monday, February 01, 2010 2:53 PMModeratorHow to set the cursor of a windows form using Visual C#.NET?
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
All Replies
-
Monday, February 01, 2010 2:54 PMModerator
In .NET applications, we can set the Form.Cursor property to a Cursor object to change the Form’s cursor. The Cursor object can be created from four sources: a specified Windows Handle, a specified Data Stream, a specified File Name, or a specified ResourceType and CursorName. The following four constructor functions of Cursor are mapped to the above four object creation sources:
1. IntPtr
http://msdn.microsoft.com/en-us/library/a5bbc3fe.aspx
2. Stream
http://msdn.microsoft.com/en-us/library/7x58z7ya.aspx
3. String
http://msdn.microsoft.com/en-us/library/kkw8k45d.aspx
4. Typ, String
http://msdn.microsoft.com/en-us/library/kkw8k45d.aspx
Some system cursors cannot be loaded directly using the full path file name because they are in a native format which managed code is not aware of. In that case, we should call the Windows API LoadCursorFromFile using PInvoke, and then choose the first pattern to create a Cursor object from a File Handler. C# codes look like,
------------------------------------------------------------
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadCursorFromFile(string path);
private void Form1_Load(object sender, EventArgs e) {
this.Cursor = new Cursor(LoadCursorFromFile(@"c:\windows\cursors\hand.cur"));
}
------------------------------------------------------------
Related threads:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/997eabe1-99a4-48cc-8223-f6ea3d1ff920
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked As Answer by Ji.ZhouModerator Friday, March 05, 2010 6:58 AM

