Simple implementation of C Form custom cursor

  • 2020-06-03 08:09:35
  • OfStack

The following is a complete example, which can be compiled from the command line to see the effect.


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
namespace ColorCursor
{
 /// <summary>
 ///  What this example does:   in .NET Implements a custom cursor in. 
 /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        [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 );

  
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        public Form1()
        {
            Cursor myCursor = new Cursor(Cursor.Current.Handle);
            IntPtr colorCursorHandle = LoadCursorFromFile(@"C:WINNTCursorsdinosau2.ani" );    
            //dinosau2.ani for windows Your own cursor: 
            myCursor.GetType().InvokeMember("handle",BindingFlags.Public | 
            BindingFlags.NonPublic | BindingFlags.Instance | 
            BindingFlags.SetField,null,myCursor,
            new object [] { colorCursorHandle } );
            this.Cursor = myCursor;
        }
    }
}


Related articles: