Failure to load custom cursor resource

P

Paul Cheetham

Hi,

I am trying to load a custom cursor from my programs resources.
I have successfully included the cursor resource, and I have confirmed
that the name I am using is correct.

Below is the code I am using to try and load the cursor - which always
returns null.

I have tried using LoadImage and LoadCursor with the same result.

Can anyone see what I cant?



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



/// <author>Shrijeet Nair</author>
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 );

}



Thanks for any help.

Paul
 
D

D. Yates

Try something like this:
private void button1_Click(object sender, System.EventArgs e)
{
// Assumes that the assemblies default namespace is
CursorExampleXYZ and that
// the cursor file, JPGHANDX.cur, build action is set to Embedded
Resourse.
this.Cursor = new
Cursor(this.GetType().Assembly.GetManifestResourceStream(
"CursorExampleXYZ.JPGHANDX.cur"));
}
 
P

Paul Cheetham

Hi,

Thanks for your reply.
Unfortunately that's the way I was doing it originally, but my cursor is
a colour cursor, and loading it that way creates a mono version of it,
since someone at Microsoft was a bit shortsighted when writing the
framework.
In order to get the colour cursor I need to load it from the resource
using the Win32 functions, and create an HCURSOR, which I can then use
in the cursor constructor to get the right result.

But it fails to load from the resource using the Win32 functions.


Paul


D. Yates said:
Try something like this:
private void button1_Click(object sender, System.EventArgs e)
{
// Assumes that the assemblies default namespace is
CursorExampleXYZ and that
// the cursor file, JPGHANDX.cur, build action is set to Embedded
Resourse.
this.Cursor = new
Cursor(this.GetType().Assembly.GetManifestResourceStream(
"CursorExampleXYZ.JPGHANDX.cur"));
}



Hi,

I am trying to load a custom cursor from my programs resources.
I have successfully included the cursor resource, and I have confirmed
that the name I am using is correct.

Below is the code I am using to try and load the cursor - which always
returns null.

I have tried using LoadImage and LoadCursor with the same result.

Can anyone see what I cant?



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



/// <author>Shrijeet Nair</author>
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 );

}



Thanks for any help.

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