自定义光标在C#中的WinForms [英] Custom cursor in C# Winforms

查看:190
本文介绍了自定义光标在C#中的WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道一个简单的方法来使用自定义光标?我同时拥有的.cur和我的光标的巴纽。我尝试添加它作为一种资源,我的项目,也尝试过,包括其作为项目中的文件。理想情况下,我想嵌入但我只想得到它的工作。

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.

当我使用光标CUR =新的光标(mycursor。 CUR)我收到图片格式不正确,图像文件可能已损坏。我想这 http://mahesg.wordpress.com/2008/02/09 /嵌入光标/ ,但没有奏效。使用 WinForm1.Properties.Resources.mycursor 返回一个字节,我不知道如何转换成一个游标类型[]

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.

推荐答案

由于某种原因光标类是过于挑剔什么它将读取。您可以创建使用Windows API,然后传递到光标类自己处理

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:

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

这篇关于自定义光标在C#中的WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆