5

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

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?

1

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1; You could also use a tool like .Net Reflector, or possibly ILSpy, to see how the resources are packed into your assembly. In absence of those, the print-to-textbox diagnostic will work well.
Using your suggestion to see the resource names I discovered it was in "Piccolo.Resources.magnify". Thanks
5

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.

Comments

5

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.

2 Comments

Would this cause memory leaks, since the MemoryStream is not disposed?
@Uwe - some programmers insist on disposing the unmanaged resources held by a byte[]. I am not one of them. Feel free to do it your way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.