如何在C#Winforms中创建嵌入式资源光标? [英] How to create an embedded resource cursor in C# Winforms?

查看:179
本文介绍了如何在C#Winforms中创建嵌入式资源光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义光标添加到C#WinForms应用程序作为一个嵌入式的资源。它似乎嵌入部分未正常工作的文档暗示它应该

I'm trying to add a custom cursor to a C# Winforms application as an embedded resource. It seems the embedding part is not working as the docs imply it should.

如果我在运行时从文件加载光标它铁锅就好了:

If I load the cursor from a file at runtime it woks just fine:

myMagCursor = new Cursor("../Resources/magnify.cur");



如此看来光标文件是不错的。我也跟着上
MSDN
嵌入光标(从C#示例中的注释):

So it seems the cursor file is good. I followed this info on MSDN to embed the cursor (from the comments in the C# example):

//In Visual Studio:
//        1. Select the cursor file in the Solution Explorer
//        2. Choose View->Properties.
//        3. In the properties window switch "Build Action" to "Embedded"



然后试图用这样的:

And then tried to use it like this:

myMagCursor = new Cursor(GetType(), "magnify.cur");



这给空引用异常,我想是因为资源是找不到的。我也试过这种方法(在网络上其他地方找到):

Which gives a null reference exception, I assume because the resource is not found. I also tried this approach (found elsewhere on the web):

namespace Piccolo.Forms
{
    public partial class Hanger
    {
    ...
        Assembly asm = Assembly.GetExecutingAssembly();
        using( Stream resStream = asm.GetManifestResourceStream("Piccolo.magnify.cur") )
        {
            myMagCursor = new Cursor( resStream );
        }



我试过Piccolo.magnify.cur,Piccolo.Forms .magnify.cur,Piccolo.Forms.Hanger.magnify.cur,Hanger.magnify.cur等我推断光标还没有嵌入

I've tried "Piccolo.magnify.cur", "Piccolo.Forms.magnify.cur", "Piccolo.Forms.Hanger.magnify.cur", "Hanger.magnify.cur" etc. I infer the cursor has not been embedded.

光标文件是与一帮.ICO的资源文件夹,png格式和.jpg文件,所有正常工作的工具条按钮和出现在Resources.resx文件(?)项目。他们都没有了嵌入的资源属性。我的光标文件确实有嵌入的资源,但不会出现在Resources.resx。

The cursor file is in a Resources folder with a bunch of .ico, .png and .jpg files that all work correctly as toolstrip buttons and appear in the 'Resources.resx' file(?) in the project. None of them have the "Embedded Resource" property. My cursor file does have the "Embedded Resource", but does not appear in 'Resources.resx'.

什么是我的光标文件丢失得到它正确地嵌入?

What am I missing with the cursor file to get it properly embedded?

推荐答案

我已经得到了你的第二个方法在WPF应用程序的工作。该部分应工作一样的,不管,因为它们都使用相同类型的资源流。下面是我用的线,成功。

I have gotten your second approach to work in a WPF application. That part should work the same regardless, as they both are using the same type of resource streams. Here is the line I have used, successfully.

canvas1.Cursor = new Cursor(Assembly.GetExecutingAssembly().GetManifestResourceStream("WpfApplication2.mallet.cur"));



最有可能的,你把你的光标某种类型的文件夹中,因此Piccolo.magnify的.cur的一部分是错误的。你可以做的是打印出所有的资源流的名字到一个文本框或看到的东西到底是什么,你应该把在那里。通过以下实现这一点:

Most likely, you put your cursor in a folder of some sort, and therefore the "Piccolo.magnify.cur" part is wrong. What you can do is print out all of the resource stream names to a textbox or something to see exactly what you should be putting there. Accomplish this by using the following:

String[] resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();

和打印出来的那些不过你选择。这应该指向您在正确的方向。

and print those out however you chose. This should point you in the right direction.

这篇关于如何在C#Winforms中创建嵌入式资源光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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