12

有没有办法在winforms中使用自定义光标?

似乎别无选择。但是当我尝试手动添加一个游标作为资源,然后从代码中调用它时,它说它不能从类型字节 [] 转换为游标。

4

5 回答 5

12

在 C# 中将自定义图标添加到光标:

将图标文件添加到项目资源(例如: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;
于 2011-09-21T09:25:28.873 回答
4

来自该类的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");
于 2010-05-09T08:48:54.633 回答
1

我使用了LoadCursorFromFile()User32.dll 中的方法。网上有很多这方面的示例。

或者

Cursor类型的 ctor 也有一个IO.Stream重载。将您的加载byte[]到 aMemoryStream并将其提供给新的Cursor.

于 2010-05-09T08:44:01.723 回答
1

使用 convertico.com 将光标从任何格式转换为 ico(这是最好的方法),使用文件资源管理器将光标复制到项目的调试文件夹并编写以下代码(C#):

this.Cursor = new Cursor("default.ico");
于 2019-12-29T11:48:09.247 回答
0

将文件添加到资源后,在图像的属性窗口中:切换Build ActionEmbedded Resource并写入您的代码:

"name of control".Cursor = new System.Windows.Forms.Cursor(Properties.Resources."name of image".Handle);
于 2012-05-22T12:05:23.043 回答