Wednesday, August 09, 2006
Loading a color cursor in .NET 2.0
The MSDN documentation on the Cursor class says
The Cursor class does not support animated cursors (.ani files) or cursors with colors other than black and white.Consequently, if you try to display a color cursor, it appears as a black outline of the original. So, supposing that you already have a .cur file containing a color cursor, follow these steps to display it. If you don't already have a color .cur file, you can use Visual Studio 2005 to create such a file.
- Add the .cur to the project's resources using the Visual Studio 2005 resource designer by using the the "Add existing file" toolbar item. I'm going to assume that you named the resource "hand2."
-
Add a P/Invoke declaration for LoadCursorFromFile.
[DllImport("user32.dll")] static extern IntPtr LoadCursorFromFile(string lpFileName);
-
At runtime, copy the cursor resource to a temporary file, ask the Win32 API to load that file as a cursor, and create .NET Cursor object from the Win32 handle.
private Cursor LoadColorHandCursor() { string path = Path.GetTempFileName(); File.WriteAllBytes(path, Properties.Resources.hand2); Cursor hand = new Cursor(LoadCursorFromFile(path)); File.Delete(path); return hand; }
Here's a working code example.
Comments:
<< Home
Very good, brief and informative tutorial. I was trying to load a colored cursor into my .net application with no hope at all until I read your tutorial. Thanks a lot. but the question is why Microsoft didn't do a wrapper or a class for doing this in .net framework directly? What if a .net developer don't know or never use the Win32 APIs (like me)? Why they didn't think of that ??
Better one below
// create any bitmap
Bitmap b = new Bitmap( 55, 25 );
Graphics g = Graphics.FromImage ( b );
// do whatever you wish
g.DrawString ( "myText", this.Font, Brushes.Blue, 0, 0 );
// this is the trick!
IntPtr ptr = b.GetHicon();
Cursor c = new Cursor( ptr );
// attach cursor to the form
this.Cursor = c;
URL:http://www.ii.uni.wroc.pl/~wzychla/cs_enFAQ.html
// create any bitmap
Bitmap b = new Bitmap( 55, 25 );
Graphics g = Graphics.FromImage ( b );
// do whatever you wish
g.DrawString ( "myText", this.Font, Brushes.Blue, 0, 0 );
// this is the trick!
IntPtr ptr = b.GetHicon();
Cursor c = new Cursor( ptr );
// attach cursor to the form
this.Cursor = c;
URL:http://www.ii.uni.wroc.pl/~wzychla/cs_enFAQ.html
Check out my solution at pinvoke.net (look for LoadCursor API). I'm also going to post an article on codeproject.com with a sample project. Look for “embed animated/color cursor as native win32 resource”.
This solution allows you to load real, alpha blend, color, animated, and hot spot enabled cursor file from embedded native Win32 resource in your assembly.
http://www.pinvoke.net/default.aspx/user32.loadcursor
Post a Comment
This solution allows you to load real, alpha blend, color, animated, and hot spot enabled cursor file from embedded native Win32 resource in your assembly.
http://www.pinvoke.net/default.aspx/user32.loadcursor
<< Home