[英]Custom cursor in C# Winforms
有谁知道使用自定义 cursor 的简单方法? 我的 cursor 同时拥有 a.cur 和 .png。 我尝试将其作为资源添加到我的项目中,并尝试将其作为文件包含在项目中。 理想情况下,我想嵌入它,但我只想让它工作。
当我使用Cursor cur = new Cursor("mycursor.cur")
我得到“图像格式无效。图像文件可能已损坏”。 我试过这个http://mahesg.wordpress.com/2008/02/09/embedding-cursor/但它没有用。 使用WinForm1.Properties.Resources.mycursor
返回一个 byte[] ,我不知道如何将其转换为 Cursor 类型。
出于某种原因,cursor class 对读取的内容过于挑剔。 您可以使用 windows API 自己创建句柄,然后将其传递给 cursor ZA2F2ED4F8DCEBC2CBB4Z21A21A22。
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
<System.Runtime.InteropServices.DllImport("user32.dll")>
Private Shared Function LoadCursorFromFile(fileName As String) As IntPtr
End Function
编写new Cursor(new MemoryStream(Properties.Resources.mycursor))
将图标文件添加到项目资源(例如:Processing.ico)
并在图像开关“构建操作”到“嵌入”的属性 window
Cursor cur = new Cursor(Properties.Resources.**Imagename**.Handle);
this.Cursor = cur;
前任:
Cursor cur = new Cursor(Properties.Resources.Processing.Handle);
this.Cursor = cur;
目标:当用户需要在示例 winforms UI 中进行剪切活动时,将 cursor 更改为自定义 cursor
这样做会奏效
System.Windows.Forms.Cursor _customCutCursor =
new System.Windows.Forms.Cursor(Properties.Resources.cut.Handle);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.