embedding a cursor in an application

  • Thread starter Thread starter Wilfried Mestdagh
  • Start date Start date
W

Wilfried Mestdagh

Hi,

I create a new cursor like this:

Bitmap bmp = new Bitmap(fileName);
panel1.Cursor = new Cursor(bmp.GetHicon());

But now I want to embed the image in the executable. I have already add
the *.ICO file to the project, but how to access it in code ?

I'm using VS2005 SP1.
 
Found it. This was not so evident to find, so I wants to share:

- Add existing item to project and select the icon file
- Select the file in solution explorer, click right: properties
- Set Build action to: Embedded resource

Then you can get it a runtime with folowing code fragment. The trick is
that the namespace must be included in the name, and the whole thing
seems case sensitive:

using System.IO;
using System.Reflection;

Stream s =
Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsApplication2.NE.ico");
bmp = new Bitmap(s);
Cursor = new Cursor(bmp.GetHicon());
 
Also realize that you can add the icon to the resource file and access it
like so:

Cursor myCursor = GetCursor(Resources.NameOfYourIconResourse);

private Cursor GetCursor(byte[] data)
{
using (MemoryStream s = new MemoryStream(data))
return new Cursor(s);
}


Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top