有没有办法在winforms中使用自定义光标?
似乎别无选择。但是当我尝试手动添加一个游标作为资源,然后从代码中调用它时,它说它不能从类型字节 [] 转换为游标。
有没有办法在winforms中使用自定义光标?
似乎别无选择。但是当我尝试手动添加一个游标作为资源,然后从代码中调用它时,它说它不能从类型字节 [] 转换为游标。
将图标文件添加到项目资源(例如:Processing.ico)
并在图像切换“构建操作”到“嵌入”的属性窗口中
Cursor cur = new Cursor(Properties.Resources.**Imagename**.Handle);
this.Cursor = cur;
Ex:
Cursor cur = new Cursor(Properties.Resources.Processing.Handle);
this.Cursor = cur;
来自该类的MSDN 文档Cursor
(稍作修正):
// The following generates a cursor from an embedded resource.
// To add a custom cursor, create or use an existing 16x16 bitmap
// 1. Add a new cursor file to your project:
// File->Add New Item->Local Project Items->Cursor File
// 2. Select 16x16 image type:
// Image->Current Icon Image Types->16x16
// --- To make the custom cursor an embedded resource ---
// In Visual Studio:
// 1. Select the cursor file in the Solution Explorer
// 2. Choose View->Properties.
// 3. In the properties window switch "Build Action" to "Embedded"
// On the command line:
// Add the following flag:
// /res:CursorFileName.Cur,Namespace.CursorFileName.Cur
//
// Where "Namespace" is the namespace in which you want to use
// the cursor and "CursorFileName.Cur" is the cursor filename.
// The following line uses the namespace from the passed-in type
// and looks for CustomCursor.MyCursor.Cur in the assemblies manifest.
// NOTE: The cursor name is case sensitive.
this.Cursor = new Cursor(GetType(), "MyCursor.Cur");
我使用了LoadCursorFromFile()
User32.dll 中的方法。网上有很多这方面的示例。
或者
该Cursor
类型的 ctor 也有一个IO.Stream
重载。将您的加载byte[]
到 aMemoryStream
并将其提供给新的Cursor
.
使用 convertico.com 将光标从任何格式转换为 ico(这是最好的方法),使用文件资源管理器将光标复制到项目的调试文件夹并编写以下代码(C#):
this.Cursor = new Cursor("default.ico");
将文件添加到资源后,在图像的属性窗口中:切换Build Action
到Embedded Resource
并写入您的代码:
"name of control".Cursor = new System.Windows.Forms.Cursor(Properties.Resources."name of image".Handle);