By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

Failure to load custom cursor resource

P: n/a

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
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.GetManifestResource Stream(
"CursorExampleXYZ.JPGHANDX.cur"));
}

"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:Oj****************@TK2MSFTNGP11.phx.gbl...

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

Nov 17 '05 #2

P: n/a

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 wrote:
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.GetManifestResource Stream(
"CursorExampleXYZ.JPGHANDX.cur"));
}

"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:Oj****************@TK2MSFTNGP11.phx.gbl...
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


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.