
Signature
Marc Butenko
mbutenko@bresnan.net
Neither the file method nor the embedded resource method works. Sorry,I
had tried both methods before posting; I should have mentioned that. I'm
thinking that I'll need to load it using win32 and then create the c#
cursor from the handle, but I'm unsure of how to load the .Net resource
with a Win32 call...
"Marc Butenko" <mbutenko@state.mt.us> wrote in news:##lnM$xJEHA.3380
@TK2MSFTNGP09.phx.gbl:
> Correction:
>
> To load from a file, it should be:
> Me.Cursor = New Cursor("myCursor.cur")
>
> Sorry,
Roger Norris - 15 May 2004 16:32 GMT
> Neither the file method nor the embedded resource method works. Sorry,I
> had tried both methods before posting; I should have mentioned that. I'm
> thinking that I'll need to load it using win32 and then create the c#
> cursor from the handle, but I'm unsure of how to load the .Net resource
> with a Win32 call...
Just ran into this problem myself, and then noticed in the Cursor
class documentation: "The Cursor class does not support animated
cursors (.ani files) or cursors with colors other than black and
white." And after a few more attempts, I found that the Cursor class
also has problems with 16x16 monochrome images. Finally got it to
work with a 32x32 monochrome image.
That does make me wonder, though, why Visual Studio .NET creates a
color cursor by default when adding a new cursor file :)
If you really want a color cursor, I know a partial solution.
First, we need to import a few Win32 calls:
[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 );
Loading the cursor is easy:
IntPtr cursorHandle = Win32.LoadCursorFromFile( "MyCursor.cur" );
Setting the cursor is more obnoxious, because it keeps wanting to
reset itself. I tried catching the WM_SETCURSOR message by adding a
message filter (Application.AddMessageFilter), but that didn't work -
I never received the message. Odd, because I received tons of other
messages - mouse moving, etc...
Eventually I just added an event handler to Application.Idle:
Application.Idle += new EventHandler( cursorIdle );
...
private void cursorIdle( object sender, System.EventArgs e )
{
SetCursor( cursorHandle );
}
But this solution isn't perfect, because the cursor still flickers
back to the default pointer in some instances - such as when you click
a mouse button. If someone has a way around the flicker, I'd love to
hear about it.
Roger Norris - 15 May 2004 16:48 GMT
> Neither the file method nor the embedded resource method works. Sorry,I
> had tried both methods before posting; I should have mentioned that. I'm
> thinking that I'll need to load it using win32 and then create the c#
> cursor from the handle, but I'm unsure of how to load the .Net resource
> with a Win32 call...
Ignore my previous message! I think I found a way to do it... haven't
encountered any bad side effects (yet), and no flicker this time.
Create a black-and-white version of the cursor and load it normally:
Cursor myCursor = new Cursor( "MonochromeCursor.cur" );
Next, use platform invoke (as shown in previous message) to load the
color version:
IntPtr colorCursorHandle = LoadCursorFromFile( "ColorCursor.cur" );
...and then we use reflection to set myCursor's private "handle" field
:)
myCursor.GetType().InvokeMember(
"handle",
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | System.Reflection.BindingFlags.SetField,
null,
myCursor,
new object [] { cursorHandle } );
Finally, set the form's cursor object:
form.Cursor = myCursor;