C# Form自定义光标的简单实现
下面是完整的例子,可以通过命令行编译即可看到效果。
using system;
using system.drawing;
using system.windows.forms;
using system.runtime.interopservices;
using system.reflection;
namespace colorcursor
{
/// <summary>
/// 本例子的作用: 在.net中实现自定义光标。
/// </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为windows自带的光标:
mycursor.gettype().invokemember("handle",bindingflags.public |
bindingflags.nonpublic | bindingflags.instance |
bindingflags.setfield,null,mycursor,
new object [] { colorcursorhandle } );
this.cursor = mycursor;
}
}
}
下一篇: c#泛型学习详解 创建线性链表