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

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

Cursor cur=new Cursor(文件名);

this.cursor=cur;

两句话 就完事

2 如果图像文件是其他格式  

首先引入命名空间

  1. using System.Runtime.InteropServices;

导入API

  1. [DllImport("user32.dll")]
  2. public 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"));

其实加载光标就两种方式,

1、直接用.cur文件直接获得Cursor对象;

2、获得文件的内存缓存指针,然后获得Cursor对象,获得指针有两种方法①已知文件,由API函数LoadCursorFromFile()获得指针;②如果是资源文件,则可以直接用Properties.Resources.资源名.GetHicon() 来获得;

所以有了资源文件,我们不必把资源文件写入文件,再通过LoadCursorFromFile()获得即可。

 

相关文章:

  • 2021-12-06
  • 2021-08-09
  • 2021-06-19
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-25
  • 2021-08-28
  • 2021-08-15
  • 2021-12-03
  • 2022-03-03
  • 2021-11-09
相关资源
相似解决方案