C# 自定义鼠标样式
作者:互联网
这里调用系统 API 的方式。
▲ 样式效果先准备好鼠标样式cur
文件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MouseSt
{
using System.Runtime.InteropServices;
using System.Reflection;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Cursor myCursor = new Cursor(Cursor.Current.Handle);
IntPtr colrCursorHandle = MouseStye.LoadCursorFromFile("hand.cur");
myCursor.GetType().InvokeMember("handle", BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.SetField, null, myCursor, new object[] { colrCursorHandle });
this.pictureBox1.Cursor = myCursor; // pictureBox区域
Cursor myCursor1 = new Cursor(Cursor.Current.Handle);
IntPtr colrCursorHandle1 = MouseStye.LoadCursorFromFile("pen.cur");
myCursor.GetType().InvokeMember("handle", BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.SetField, null, myCursor1, new object[] { colrCursorHandle1 });
this.pictureBox2.Cursor = myCursor1;
}
}
class MouseStye
{
[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);
}
}
InvokeMember
第二个选项参数具体还没有深究,先照着写吧。
参考:
C# winForm 自定义鼠标样式的两种方法:
https://www.cnblogs.com/tianguook/archive/2010/08/03/1791572.html
标签:IntPtr,鼠标,自定义,C#,myCursor,System,Cursor,BindingFlags,using 来源: https://www.cnblogs.com/huvjie/p/15343352.html