C# 自定义光标

来源:互联网 发布:即时通讯app源码 编辑:程序博客网 时间:2024/04/30 10:39

一种: 把图像文件放到项目的文件夹

1 如果图像文件是.cur格式:

Cursor cur=new Cursor(文件名);

this.cursor=cur;

两句话 就完事

2 如果图像文件是其他格式 就麻烦一点

 

首先引入命名空间

  1. using System.Runtime.InteropServices;

导入API

  1. [DllImport("user32.dll")]
  2. ublic static extern IntPtr LoadCursorFromFile(string fileName);

接下来使用自己的鼠标样式

  1. IntPtr colorCursorHandle = LoadCursorFromFile("my.bmp");//鼠标图标路径
  2. Cursor myCursor = new Cursor(colorCursorHandle);
  3. this.Cursor = myCursor;

二种: 把图像文件放到项目资源

  1 添加引用 using System.Runtime.InteropServices;

2.2 在程序中声明光标资源加载函数LoadCursorFromFile;

[DllImport("user32")]

private static extern IntPtr LoadCursorFromFile(string fileName);

2.3 声明数组 byte[] cursorbuffer=namespace.Resource .CursorName;

Namespace为资源文件所在项目的命名空间名称,CursorName对应光标资源文件名。

2.4 创建一个临时光标文件tempTest.dat;将cursorbuffer中的数据写入数据文件中;

FileStream fileStream = new FileStream("tempTest.dat", FileMode. Create);

fileStream.Write(cursorbuffer, 0, cursorbuffer.Length);

2.5 关闭文件,利用API 函数LoadCursorFromFile从光标临时文件中创建光标。

fileStream.Close();

Cursor .Current =new Cursor(LoadCursorFromFile("temp001.dat"));



原文连接:http://blog.sina.com.cn/s/blog_a401a1ea01019wv7.html

原创粉丝点击