有没有办法在winforms中使用自定义光标?
似乎没有选择。但是当我尝试手动将游标添加为资源时,从代码中调用它,它表示它无法从byte []类型转换为Cursor。
答案 0 :(得分:11)
将图标文件添加到项目资源(例如:Processing.ico)
在图像切换“Build Action”到“Embedded”的属性窗口中
Cursor cur = new Cursor(Properties.Resources.**Imagename**.Handle);
this.Cursor = cur;
Ex:
Cursor cur = new Cursor(Properties.Resources.Processing.Handle);
this.Cursor = cur;
答案 1 :(得分:4)
来自MSDN documentation on the Cursor
class(略有更正):
// 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");
答案 2 :(得分:1)
我使用过User32.dll的LoadCursorFromFile()
方法。网上有很多样本。
OR
Cursor
类型的ctor也有IO.Stream
超载。将byte[]
加载到MemoryStream
并将其提供给新的Cursor
。
答案 3 :(得分:0)
将文件添加到资源后,在图片的属性窗口中:将Build Action
切换为Embedded Resource
并写入您的代码:
"name of control".Cursor = new System.Windows.Forms.Cursor(Properties.Resources."name of image".Handle);
答案 4 :(得分:0)
使用convertico.com将光标从任何格式转换为ico(这是最好的方法),使用文件资源管理器将光标复制到项目的调试文件夹并编写以下代码(C#):
||