Loading Resources in Win Forms App

P

Paul Cheetham

Hi,

I have several cursors which I have added to my project (c#) and set the
build action to 'Embedded Resource'.

I am now having problems trying to load these, and so I was trying to
find the resource file and check their names, to make sure I was using
the right one.
The cursors don't appear to be in any of the resource files.

Since I am trying to use the Win32 LoadImage function to load these from
the resource file, the name needs to be correct, but I can't even be
sure they are being included in the executable.

Can anyone tell me how I can be sure they are being included, and
determine their names, or perhaps someone has a working example of this?
(I am using LoadImage because I am trying to load colour cursors)


Thankyou.

Paul Cheetham.
 
B

Bob Powell [MVP]

You can check the names in the resources by putting this couple of lines in
your constructor. It's debug code so take it out once you've made a note of
the names.

foreach(string s in this.GetType().Assembly.GetManifestResourceNames())
System.Diagnostics.Trace.WriteLine(s);

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
P

Paul Cheetham

Thanks Bob, That worked great.
(Sorry for the delay replying, been away for a couple of days)

Only problem now is even though I know the resource names are correct,
it is still failing to load my cursor.

The code I am using follows:


public static Cursor LoadCursor (string CursorName)
{
Cursor myCursor = null;
String Name;
IntPtr HCursor=IntPtr.Zero, HInst=IntPtr.Zero;

//Name = "Prologic." + CursorName + ".CUR";
Name = "ProLogic.Cursors.IRON.CUR";

HInst = Win32.GetModuleHandleW(null);

HCursor = Win32.LoadImage(HInst, Name, Win32.IMAGE_CURSOR, 0, 0,
Win32.LR_DEFAULTCOLOR | Win32.LR_DEFAULTSIZE | Win32.LR_SHARED);
//HCursor = Win32.LoadCursor (HInst, Name);
if (HCursor.ToInt32() > 0)
myCursor = new Cursor (HCursor);

return (myCursor);
}


public class Win32
{
public const uint IMAGE_BITMAP = 0;
public const uint IMAGE_ICON = 1;
public const uint IMAGE_CURSOR = 2;

public const uint LR_DEFAULTCOLOR = 0x0000;
public const uint LR_MONOCHROME = 0x0001;
public const uint LR_COLOR = 0x0002;
public const uint LR_COPYRETURNORG = 0x0004;
public const uint LR_COPYDELETEORG = 0x0008;
public const uint LR_LOADFROMFILE = 0x0010;
public const uint LR_LOADTRANSPARENT = 0x0020;
public const uint LR_DEFAULTSIZE = 0x0040;
public const uint LR_VGACOLOR = 0x0080;
public const uint LR_LOADMAP3DCOLORS = 0x1000;
public const uint LR_CREATEDIBSECTION = 0x2000;
public const uint LR_COPYFROMRESOURCE = 0x4000;
public const uint LR_SHARED = 0x8000;


[DllImport("User32.dll")]
public static extern IntPtr LoadImage(IntPtr hinst,

[MarshalAs(UnmanagedType.LPTStr)] string lpszName,
uint uType,
int cxDesired,
int cyDesired,
uint fuLoad);

[DllImport("User32.dll")]
public static extern IntPtr LoadCursor(IntPtr hinst,
[MarshalAs(UnmanagedType.LPTStr)] string lpszName);

[DllImport( "kernel32.dll" )]
public extern static IntPtr
GetModuleHandleW([MarshalAs(UnmanagedType.LPStr)] string lpModuleName );

}



Any insights?



Thankyou.

Paul
 

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

Top