C# 如何更改鼠标光标图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1175870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change the mouse cursor image?
提问by RV.
I need to change the cursor image. Whenever the mouse is over my form I need to load my own image from a local path. I am using version 1.1 of the .NET framwork.
我需要更改光标图像。每当鼠标悬停在我的表单上时,我都需要从本地路径加载我自己的图像。我正在使用 .NET 框架的 1.1 版。
Here is what I have tried:
这是我尝试过的:
Cursor = new Cursor(GetType(), Application.StartupPath+ "\windowfi.cur");
But this throws an exception:
但这会引发异常:
Value cannot be null.
Parameter name: dataStream
值不能为空。
参数名称:数据流
采纳答案by danish
Cursor class has a constructor that takes in cur file path as a parameter. Use that. Like this:
Cursor 类有一个构造函数,它接受 cur 文件路径作为参数。用那个。像这样:
this.Cursor = new Cursor("<your_cur_file_path");
回答by jpoh
This should probably work:
这应该可以工作:
Cursor.Current = new Cursor(GetType(), Application.StartupPath+ @"\windowfi.cur");
or
或者
Cursor.Current = new Cursor(GetType(), Application.StartupPath+ "\windowfi.cur");
Note the use of @ string literal and the \ escape character above to be able to use the backslash character correctly in the path to the cursor's icon. As well as the Currentproperty of the Cursor class.
请注意上面@字符串文字和\转义字符的使用,以便能够在光标图标的路径中正确使用反斜杠字符。以及Cursor 类的Current属性。
回答by JP Alioto
It looks like you're using the wrong overload for the cursor constructor. If you want to use a file path, use the constructor overload that just takes a string. You are using the overload that takes a type and a string. That overload gets an embedded resource.
看起来您对游标构造函数使用了错误的重载。如果要使用文件路径,请使用仅接受 string 的构造函数重载。您正在使用采用类型和字符串的重载。那个重载得到一个嵌入的资源。