Embedding Cursor

How to embed a existing cursor file into the application?

I was using Visual C# 2005 to build a GUI application. I needed to add existing cursor file into the project.

The procedure i followed was:

1) I added a existing cursor file into project.

2) Selected cursor file in the Solution Explorer > Chose View->Properties > Change the “Build Action” to “Embedded Resources”.

3) Code

Cursor newCur = new Cursor(“xyz.cur”);
this.Cursor = newCur;

But application was throwing exception “Could not find file C:\App\Debug\rotate.cur” even though i have added it in application.

After googling i found the solution at http://www.codeprof.com/dev-archive/37/9-31-373566.shtm

Here it goes:

1) Follow procedure 1 and 2 as done by me and Add

using System.Reflection;
using System.Resources;
using System.IO;

2) Actual code for getting resource (cursor)
 
Assembly asm = Assembly.GetExecutingAssembly();
using( Stream resStream = asm.GetManifestResourceStream( “MyNameSpace.xyz.cur” ) )
{
  Cursor cursor = new Cursor( resStream );
}

where MyNamespace is name of our namespace.

3) Use

Form.Cursor = cursor;

The link also give the detailss of how to print all our resource names to the console (for example to lookup if the resource exists):

Assembly asm = Assembly.GetExecutingAssembly();
Console.WriteLine( “Manifest resources for {0}”, asm.FullName );

foreach( String resourceName in asm.GetManifestResourceNames() )
{
   Console.WriteLine( “\t{0}”, resourceName );
}

About these ads

2 thoughts on “Embedding Cursor

  1. jzel laserna

    uhm… what do you mean in PROJECT??? where can i find that PROJECT??? please help me…. thanks!!

    Reply
  2. mahesg Post author

    Your Project in Solution Explorer.
    Right click on your Project and click “Add New Item”.
    Add the cursor file.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s