C# Winforms 中的自定义光标

发布于 2024-11-19 10:01:42 字数 409 浏览 2 评论 0原文

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

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

Does anyone know of an easy way to use a custom cursor? I have both a .cur and .png of my cursor. I tried adding it as a resource to my project and also tried including it as a file in the project. Ideally I'd like to embed it but I just want to get it working.

When I use Cursor cur = new Cursor("mycursor.cur") I get "Image format is not valid. The image file may be corrupted". I tried this http://mahesg.wordpress.com/2008/02/09/embedding-cursor/ but it didn't work. Using WinForm1.Properties.Resources.mycursor returns a byte[] which I have no idea how to convert into a Cursor type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狠疯拽 2024-11-26 10:01:42

由于某种原因,游标类对于它将读取的内容过于挑剔。您可以使用 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
<System.Runtime.InteropServices.DllImport("user32.dll")>
Private Shared Function LoadCursorFromFile(fileName As String) As IntPtr
End Function

For some reason the cursor class is far too picky about what it will read. You can create the handle yourself using the windows API then pass that to the cursor class.

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
む无字情书 2024-11-26 10:01:42

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

Write new Cursor(new MemoryStream(Properties.Resources.mycursor))

旧情勿念 2024-11-26 10:01:42

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

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

并在图像的属性窗口中将“构建操作”切换为“嵌入”

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

例如:

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

Adding custom icon to cursor in C# :

Add Icon file to Project resources (ex : Processing.ico)

And in properties window of image switch "Build Action" to "Embedded"

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

Ex:

Cursor cur = new Cursor(Properties.Resources.Processing.Handle);
this.Cursor = cur;
梦里°也失望 2024-11-26 10:01:42

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

执行此操作将会起作用

  1. 将图标文件(例如 cut.ico)添加到项目
  2. 现在将图标添加到项目资源
    要添加到资源,请右键单击项目 -> 属性 -> 资源 现在将 ico 文件从项目文件夹(U 添加到第 1 点中的项目文件夹)中拖放到资源上
  3. 此代码应该可以解决问题
System.Windows.Forms.Cursor _customCutCursor = 
   new System.Windows.Forms.Cursor(Properties.Resources.cut.Handle);

Objective: To change cursor to a custom cursor when user need to do cut activity in a sample winforms UI

Do this it will work

  1. Add the icon file (e.g cut.ico) to the project
  2. Now add icon to project resource
    To add to resource right click on project->properties->Resources Now drop the ico file from the project folder (U added to project folder in point 1) on Resources
  3. This code should do the trick
System.Windows.Forms.Cursor _customCutCursor = 
   new System.Windows.Forms.Cursor(Properties.Resources.cut.Handle);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文