PDA

Click to See Complete Forum and Search --> : [2.0] Using Color cursors from resources.


conipto
Apr 2nd, 2006, 01:01 AM
Ok, so much to my dismay, it appears the .NET framework doesn't support color Mouse Cursors. That's fine, since searching around, I found a suitable API method that seems to work.


#region API Calls for Color Cursor use..
[DllImport("user32.dll")]
public static extern IntPtr LoadCursorFromFile(string fileName);

[DllImport("user32.dll")]
public static extern IntPtr SetCursor(IntPtr cursorHandle);

[DllImport("user32.dll")]
public static extern uint DestroyCursor(IntPtr cursorHandle);
#endregion
/// <summary>
/// It's insane how much code it takes to load a color cursor. geez.
/// </summary>
private void Surface_MouseEnter(object sender, EventArgs e)
{
Cursor myCursor = new Cursor(GetType(), "PencilCursor.cur");
IntPtr colorCursorHandle = LoadCursorFromFile("PencilCursor.cur" );
myCursor.GetType().InvokeMember("handle",BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Instance | System.Reflection.BindingFlags.SetField,null,myCursor,new object [] { colorCursorHandle } );
this.Cursor = myCursor;
}



However, This only works if the cursor file is in the apps executable directory. Originally, I had just added the cursor to the project, and changed it's built action to Embed Resource. This allowed the first call in the funciton to load it from the resource manifest, which was great, but it was solid black. Is there a way to use the API method which loads a color cursor, but with my embedded resource?

Bill

jmcilhinney
Apr 2nd, 2006, 01:35 AM
There's a LoadCursor API as well that specifically loads a cursor from a resource. Don't have a usage example though.

conipto
Apr 2nd, 2006, 03:21 AM
After trying to get the LoadCursor API function to work for a while, I started to look around for examples.. and found this page:

http://www.pinvoke.net/default.aspx/user32/LoadCursor.html

With the author saying it doesn't seem to work right for embedded resources :(

Pretty much no matter what I do, it just loads a nothing cursor... Has anyone ever used this method with a color cursor?

Bill