C#Winforms中的自定义光标

有谁知道使用自定义光标的简单方法? 我有一个.cur和.png的光标。 我尝试将它作为资源添加到我的项目中,并尝试将其作为项目中的文件包含在内。 理想情况下我想嵌入它,但我只想让它工作。

当我使用Cursor cur = new Cursor("mycursor.cur")我得到“图像格式无效。图像文件可能已损坏”。 我试过这个http://mahesg.wordpress.com/2008/02/09/embedding-cursor/但它没有用。 使用WinForm1.Properties.Resources.mycursor返回一个byte [],我不知道如何转换为Cursor类型。

由于某种原因,光标类对它将读取的内容非常挑剔。 您可以使用Windows API自己创建句柄,然后将其传递给游标类。

C#:

 //(in a class) public static Cursor ActuallyLoadCursor(String path) { return new Cursor(LoadCursorFromFile(path)) } [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern IntPtr LoadCursorFromFile(string fileName); 

VB.Net:

 '(in a class)' Public Shared Function ActuallyLoadCursor(path As String) As Cursor Return New Cursor(LoadCursorFromFile(path)) End Function  Private Shared Function LoadCursorFromFile(fileName As String) As IntPtr End Function 

编写new Cursor(new MemoryStream(Properties.Resources.mycursor))

在C#中向游标添加自定义图标:

将图标文件添加到项目资源(例如:Processing.ico)

并在图像开关“Build Action”到“Embedded”的属性窗口中

 Cursor cur = new Cursor(Properties.Resources.**Imagename**.Handle); this.Cursor = cur; 

例如:

 Cursor cur = new Cursor(Properties.Resources.Processing.Handle); this.Cursor = cur; 

目标:当用户需要在示例winforms UI中执行剪切活动时,将光标更改为自定义光标

这样做会起作用

  1. 将图标文件(例如cut.ico)添加到项目中
  2. 现在将图标添加到项目资源要添加到资源,请右键单击project-> properties-> Resources现在从资源上删除项目文件夹中的ico文件(U添加到项目文件夹中的点1)
  3. 这段代码应该做的技巧System.Windows.Forms.Cursor _customCutCursor = new System.Windows.Forms.Cursor(Properties.Resources.cut.Handle);