开发者

How to create an embedded resource cursor in C# Winforms?

I'm trying to add a cus开发者_StackOverflowtom 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");

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 );
        }

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.

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?


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"));

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.


For cursors, you can just do:

using (MemoryStream ms = new MemoryStream(Properties.Resources.magnify))
{
  myMagCursor = New Cursor(ms);
}

Hope you don't expect to see it in color, though.


Getting the overload you are using to work is troublesome. There are never-ending namespace problems with it. By far the best way to do this is by adding the cursors as resources through the Project + Properties, Resources tab. Click on the arrow of the Add Resource button, Add Existing File and select the .cur file.

At runtime, you can use the Properties.Resources.cursorname property. That returns a byte[], you whack that into a cursor with code like this:

 this.Cursor = new Cursor(new System.IO.MemoryStream(Properties.Resources.arrow_i));

Note the use of MemoryStream to turn the byte[] into a stream so the Cursor(Stream) overload works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜