Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How to load Cursor from Resource file?

Tags:

c#

.net

I have imported a file "x.ani" into Resource file Resources.resx. Now trying to load that file using ResourceManager.GetObject("aero_busy.ani")

Cursor.Current = (Cursor)ResourcesX.GetObject("aero_busy.ani");

but it didn't work .. (certainly) :)

How can I change the current Cursor using resource object?

like image 440
InfantPro'Aravind' Avatar asked Aug 01 '11 10:08

InfantPro'Aravind'


4 Answers

I do it by adding the cursor .cur file into the Resources part of the project (I am using Visual Studio). I'm not sure if it would have to be .cur, so long as the development program can load it.

Having done that in the variables declaration part of my code I create a MemoryStream from the cursor file:

private static System.IO.MemoryStream cursorMemoryStream = new System.IO.MemoryStream(myCurrentProject.Properties.Resources.myCursorFile);

...and then you can create the cursor from the MemoryStream:

private Cursor newCursor = new Cursor(cursorMemoryStream);

You can then assign the cursor as you like within the program, e.g.

pictureBox1.Cursor = newCursor;

and the new cursor is compiled as part of the program.

like image 62
Greg Avatar answered Sep 20 '22 02:09

Greg


I haven't found any better way than dumping to a temp file and use the Win32 load cursor from file method. The hack goes something like this (I removed a big chunk of boilerplate code for clarity, in which a temp file is written with the data from the stream). Also, all exception handling etc. was removed.

[DllImport("User32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
private static extern IntPtr LoadCursorFromFile(String str);

public static Cursor LoadCursorFromResource(string resourceName)
{         
     Stream cursorStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);        

     // Write a temp file here with the data in cursorStream

     Cursor result = new Cursor(LoadCursorFromFile(tempFile));
     File.Delete(tempFile);

     return result.
}

You would use this as (remember namespaces when loading embedded resources).

Cursors.Current = LoadCursorFromResource("My.Namespace.Filename");
like image 26
Anders Forsgren Avatar answered Sep 20 '22 02:09

Anders Forsgren


after a few turns to the issue, I find the elegant solution is:

internal static Cursor GetCursor(string cursorName)
    {
        var buffer = Properties.Resources.ResourceManager.GetObject(cursorName) as byte[];

        using (var m = new MemoryStream(buffer))
        {
            return new Cursor(m);
        }
    }
like image 22
Sith2021 Avatar answered Sep 20 '22 02:09

Sith2021


I think the issue has to do with the fact that the cursor must have .cur extension in order to be used as Cursor.

// The following generates a cursor from an embedded resource.

// To add a custom cursor, create or use an existing 16x16 bitmap // 1. Add a new cursor file to your project: // File->Add New Item->Local Project Items->Cursor File // 2. Select 16x16 image type: // Image->Current Icon Image Types->16x16

The above was taken from MSDN.

Update : Found the answer why.

"Note Note

The Cursor class does not support animated cursors (.ani files) or cursors with colors other than black and white."

Found Here

like image 32
Jethro Avatar answered Sep 20 '22 02:09

Jethro