First: (call the system API)
First, two namespaces are introduced.
Copy codeThe Code is as follows: using System. Runtime. InteropServices;
Using System. Reflection;
Import APICopy codeThe Code is as follows: [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 );
Next, use your mouse style.Copy codeThe Code is as follows: private void Form1_Load (object sender, EventArgs e)
{
Cursor myCursor = new Cursor (Cursor. Current. Handle );
IntPtr colorCursorHandle = LoadCursorFromFile ("my. cur"); // Mouse icon path
MyCursor. GetType (). InvokeMember ("handle", BindingFlags. Public |
BindingFlags. NonPublic | BindingFlags. Instance |
BindingFlags. SetField, null, myCursor,
New object [] {colorCursorHandle });
This. Cursor = myCursor;
}
Method 2: (you do not need to use the API. The mouse style only requires a transparent background image, in png or gif format)
Copy codeThe Code is as follows: public void SetCursor (Bitmap cursor, Point hotPoint)
{
Int hotX = hotPoint. X;
Int hotY = hotPoint. Y;
Bitmap myNewCursor = new Bitmap (cursor. Width * 2-hotX, cursor. Height * 2-hotY );
Graphics g = Graphics. FromImage (myNewCursor );
G. Clear (Color. FromArgb (0, 0, 0, 0 ));
G. DrawImage (cursor, cursor. Width-hotX, cursor. Height-hotY, cursor. Width,
Cursor. Height );
This. Cursor = new Cursor (myNewCursor. GetHicon ());
G. Dispose ();
MyNewCursor. Dispose ();
}
Use this method in the event you want to change the mouse style, for example:Copy codeThe Code is as follows: private void Form1_Load (object sender, EventArgs e)
{
Bitmap a = (Bitmap) Bitmap. FromFile ("myCur.png ");
SetCursor (a, new Point (0, 0 ));
}