您的位置:首页 - C#
WinForm自定义光标C#例子

WinForm代码如下:
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace KeleyiColorCursor
{
public partial class CursorForm : 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);

public CursorForm()
{
InitializeComponent();
Cursor myCursor = new Cursor(Cursor.Current.Handle);
string m_cursorFile = @"D:\keleyi\pen_r.cur";

//pen_r.cur文件下载地址:
//http://down.keleyi.com/files/cursors/pen_r.cur


//或者设为动态光标
// m_cursorFile = @"D:\keleyi\dinosau2.ani";
//下载地址: http://down.keleyi.com/files/cursors/dinosau2.ani

IntPtr colorCursorHandle = LoadCursorFromFile(m_cursorFile);


myCursor.GetType().InvokeMember("handle", BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.SetField, null, myCursor,
new object[] { colorCursorHandle });
this.Cursor = myCursor;
}
}
}

运行后,如果把光标移入窗体,光标消失了,那是因为没有下载光标文件到特定文件夹。
光标文件下载:http://down.keleyi.com/files/cursors/dinosau2.ani
http://down.keleyi.com/files/cursors/pen_r.cur
本站导航
相关文章
柯乐义 Copyright© keleyi.com