P
Paul Cheetham
Hi,
I have some custom colour cursors which I have added to my c# project,
and set them to be compiled as "Embedded Resource"
It seems impossible to load a colour cursor using the standard Cursor
class, so I have had to resort to loading it using the Win32 API, and
creating the cursor with - new Cursor(HCURSOR)
The code I am using is shown at the bottom of this post.
I have confirmed that the cursors I am trying to load are embedded in
the exe file, by listing them to the output window as follows:
foreach(string s in
this.GetType().Assembly.GetManifestResourceNames())
System.Diagnostics.Trace.WriteLine(s);
However, whenever I try to load one of these using the Win32 LoadImage
function (I have also tried LoadCursor) it fails, with the following error:
1813 - The specified resource type cannot be found in the image file.
As far as I can tell the resource is there, but it is either in a
different format to what the Win32 functions expect it, or it's not
really there at all.
I have spent a great deal of time on this, and it is something I need to
solve for several projects.
Has anybody managed to solve this problem, and actually use cursors
properly in .Net as is possible in Win32?
Thankyou.
Paul Cheetham
(Using Visual Studio 2003, .Net Framework 1.1)
Code:
public static Cursor LoadCursor (string CursorName)
{
Cursor myCursor = null;
String Name;
IntPtr HCursor=IntPtr.Zero, HInst=IntPtr.Zero;
uint ErrCode;
//Name = "ProLogic.Cursors." + CursorName + ".CUR";
Name = "ProLogic.Cursor1.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)
{ ErrCode = Win32.GetLastError();
} else
myCursor = new Cursor (HCursor);
return (myCursor);
}
#region <<< Class Win32 >>>
/// <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 );
[DllImport( "kernel32.dll" )]
public extern static uint GetLastError();
}
#endregion
I have some custom colour cursors which I have added to my c# project,
and set them to be compiled as "Embedded Resource"
It seems impossible to load a colour cursor using the standard Cursor
class, so I have had to resort to loading it using the Win32 API, and
creating the cursor with - new Cursor(HCURSOR)
The code I am using is shown at the bottom of this post.
I have confirmed that the cursors I am trying to load are embedded in
the exe file, by listing them to the output window as follows:
foreach(string s in
this.GetType().Assembly.GetManifestResourceNames())
System.Diagnostics.Trace.WriteLine(s);
However, whenever I try to load one of these using the Win32 LoadImage
function (I have also tried LoadCursor) it fails, with the following error:
1813 - The specified resource type cannot be found in the image file.
As far as I can tell the resource is there, but it is either in a
different format to what the Win32 functions expect it, or it's not
really there at all.
I have spent a great deal of time on this, and it is something I need to
solve for several projects.
Has anybody managed to solve this problem, and actually use cursors
properly in .Net as is possible in Win32?
Thankyou.
Paul Cheetham
(Using Visual Studio 2003, .Net Framework 1.1)
Code:
public static Cursor LoadCursor (string CursorName)
{
Cursor myCursor = null;
String Name;
IntPtr HCursor=IntPtr.Zero, HInst=IntPtr.Zero;
uint ErrCode;
//Name = "ProLogic.Cursors." + CursorName + ".CUR";
Name = "ProLogic.Cursor1.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)
{ ErrCode = Win32.GetLastError();
} else
myCursor = new Cursor (HCursor);
return (myCursor);
}
#region <<< Class Win32 >>>
/// <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 );
[DllImport( "kernel32.dll" )]
public extern static uint GetLastError();
}
#endregion