vb.net 从资源文件设置自定义光标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/432379/
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
Set Custom Cursor from Resource File
提问by KerryF
In my VB.net project I created a custom cursor (Window.cur). How can I assign that to the cursor without having to use the full file path to that file?
在我的 VB.net 项目中,我创建了一个自定义光标 (Window.cur)。如何将其分配给光标而不必使用该文件的完整文件路径?
VB.Net has My.Resources but it does not show the cursors that are embedded in the project.
VB.Net 有 My.Resources 但它不显示嵌入在项目中的光标。
I found an example that used code like this: New Cursor(Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("Window.cur") but that does not work.
我找到了一个使用这样的代码的例子: New Cursor(Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("Window.cur") 但这不起作用。
回答by Hans Passant
Guessing the resource name can be difficult. To find out, run Ildasm.exe on your program. Double-click "Manifest" and look for the .mresource.
猜测资源名称可能很困难。要找到答案,请在您的程序上运行 Ildasm.exe。双击“清单”并查找 .mresource。
Another way to do it that avoids guessing: Project + Properties, Resource tab. Click the arrow on the "Add Resource" button, Add Existing File and select your .cur file. Make your code look like this:
另一种避免猜测的方法:项目+属性,资源选项卡。单击“添加资源”按钮上的箭头,添加现有文件并选择您的 .cur 文件。让你的代码看起来像这样:
Dim ms As New System.IO.MemoryStream(My.Resources.Cursor1)
Button1.Cursor = New Cursor(ms)
回答by KerryF
Thanks for the help! I assumed that if I created the resource in the Visual Studio IDE it would add it to my project. Silly me!
谢谢您的帮助!我假设如果我在 Visual Studio IDE 中创建资源,它会将它添加到我的项目中。傻我!
I had to go to the Project tab to add the Window.Cur file using Add Resource (thanks nobugz!) and then use the code he mentioned:
我必须转到“项目”选项卡以使用“添加资源”添加 Window.Cur 文件(感谢 nobugz!),然后使用他提到的代码:
Dim ms As New System.IO.MemoryStream(My.Resources.Window)
Button.Cursor = New Cursor(ms)
I would vote up on the answer if I could but I can't as I only have a value of 13 currently.
如果可以的话,我会投票赞成答案,但我不能,因为我目前只有 13 的值。
回答by KerryF
you mustn't use 32bit color cursors.
您不能使用 32 位颜色光标。
回答by Otávio Décio
You are missing the namespace. You probably want to use:
您缺少命名空间。您可能想要使用:
MyNamespace.MySubfolder.Window.cur
EDIT: Also, make sure your Build Action for the item is "Embedded Resource", otherwise it will not be include in your dll/exe.
编辑:另外,请确保您对该项目的构建操作是“嵌入式资源”,否则它将不会包含在您的 dll/exe 中。
回答by user50612
Suppose you are assigning "Cursor1.cur" to be the cursor for the control "Button1."
假设您将“Cursor1.cur”指定为控件“Button1”的光标。
In your Form.Load event you would do something like -
在您的 Form.Load 事件中,您会执行以下操作 -
Button1.Cursor = New Cursor(Me.GetType(), "Cursor1.cur")