473,568 Members | 2,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom Cursor in Resource will not Load

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.GetMan ifestResourceNa mes())
System.Diagnost ics.Trace.Write Line(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.Ze ro;
uint ErrCode;

//Name = "ProLogic.Curso rs." + CursorName + ".CUR";
Name = "ProLogic.Curso r1.CUR";

HInst = Win32.GetModule HandleW(null);

HCursor = Win32.LoadImage (HInst, Name, Win32.IMAGE_CUR SOR, 0, 0,
Win32.LR_DEFAUL TCOLOR | Win32.LR_DEFAUL TSIZE | Win32.LR_SHARED );
//HCursor = Win32.LoadCurso r (HInst, Name);
if (HCursor.ToInt3 2() == 0)
{ ErrCode = Win32.GetLastEr ror();
} else
myCursor = new Cursor (HCursor);

return (myCursor);
}
#region <<< Class Win32 >>>

/// <author>Shrijee t 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_COPYRETURNOR G = 0x0004;
public const uint LR_COPYDELETEOR G = 0x0008;
public const uint LR_LOADFROMFILE = 0x0010;
public const uint LR_LOADTRANSPAR ENT = 0x0020;
public const uint LR_DEFAULTSIZE = 0x0040;
public const uint LR_VGACOLOR = 0x0080;
public const uint LR_LOADMAP3DCOL ORS = 0x1000;
public const uint LR_CREATEDIBSEC TION = 0x2000;
public const uint LR_COPYFROMRESO URCE = 0x4000;
public const uint LR_SHARED = 0x8000;
[DllImport("User 32.dll")]
public static extern IntPtr LoadImage(IntPt r hinst,

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

[DllImport("User 32.dll")]
public static extern IntPtr LoadCursor(IntP tr hinst,
[MarshalAs(Unman agedType.LPTStr )] string lpszName);

[DllImport( "kernel32.d ll" )]
public extern static IntPtr
GetModuleHandle W([MarshalAs(Unman agedType.LPStr)] string lpModuleName );

[DllImport( "kernel32.d ll" )]
public extern static uint GetLastError();
}

#endregion
Dec 31 '05 #1
5 10201
Paul,

Why are you using LoadImage? If you are storing a cursor as an embedded
resource, you can just call the GetManifestReso urceStream (on the Assembly
class, passing the name of the resource, in this case, a cursor, to get).
That will return a Stream to you. You can then take that Stream and pass
that to the constructor of the Cursor class. At that point, you have your
cursor, and if you need the handle of it, you can call the Handle property
of the Cursor instance.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Paul Cheetham" <PA******@dsl.p ipex.com> wrote in message
news:eY******** *****@TK2MSFTNG P15.phx.gbl...
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.GetMan ifestResourceNa mes())
System.Diagnost ics.Trace.Write Line(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.Ze ro;
uint ErrCode;

//Name = "ProLogic.Curso rs." + CursorName + ".CUR";
Name = "ProLogic.Curso r1.CUR";

HInst = Win32.GetModule HandleW(null);

HCursor = Win32.LoadImage (HInst, Name, Win32.IMAGE_CUR SOR, 0, 0,
Win32.LR_DEFAUL TCOLOR | Win32.LR_DEFAUL TSIZE | Win32.LR_SHARED );
//HCursor = Win32.LoadCurso r (HInst, Name);
if (HCursor.ToInt3 2() == 0)
{ ErrCode = Win32.GetLastEr ror();
} else
myCursor = new Cursor (HCursor);

return (myCursor);
}
#region <<< Class Win32 >>>

/// <author>Shrijee t 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_COPYRETURNOR G = 0x0004;
public const uint LR_COPYDELETEOR G = 0x0008;
public const uint LR_LOADFROMFILE = 0x0010;
public const uint LR_LOADTRANSPAR ENT = 0x0020;
public const uint LR_DEFAULTSIZE = 0x0040;
public const uint LR_VGACOLOR = 0x0080;
public const uint LR_LOADMAP3DCOL ORS = 0x1000;
public const uint LR_CREATEDIBSEC TION = 0x2000;
public const uint LR_COPYFROMRESO URCE = 0x4000;
public const uint LR_SHARED = 0x8000;
[DllImport("User 32.dll")]
public static extern IntPtr LoadImage(IntPt r hinst,

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

[DllImport("User 32.dll")]
public static extern IntPtr LoadCursor(IntP tr hinst,
[MarshalAs(Unman agedType.LPTStr )] string lpszName);

[DllImport( "kernel32.d ll" )]
public extern static IntPtr
GetModuleHandle W([MarshalAs(Unman agedType.LPStr)] string lpModuleName );

[DllImport( "kernel32.d ll" )]
public extern static uint GetLastError();
}

#endregion

Dec 31 '05 #2
Hi Nicholas,

I have tried that method, but I end up with a monochrome version of the
cursor instead of the colour version as it should be.
Since none of the .Net Cursor constructors seem able to load a colour
cursor correctly I was forced to try and load it using the Win32 API and
then try to assign it to the cursor class.
Any other ideas greatly appreciated.

Thanks.
Paul Cheetham
Nicholas Paldino [.NET/C# MVP] wrote:
Paul,

Why are you using LoadImage? If you are storing a cursor as an embedded
resource, you can just call the GetManifestReso urceStream (on the Assembly
class, passing the name of the resource, in this case, a cursor, to get).
That will return a Stream to you. You can then take that Stream and pass
that to the constructor of the Cursor class. At that point, you have your
cursor, and if you need the handle of it, you can call the Handle property
of the Cursor instance.

Hope this helps.

Dec 31 '05 #3
Paul,

Looking into this, it seems that ultimately, it uses some of the Ole
methods to load it as a picture first, and then load it as a cursor. I
think that along the way, this is where the colors get skewed.

In order to get a cursor that you would load through LoadCursor, you
will have to compile through the command line (or adjust the tasks in your
project file if you are using .NET 2.0 with MSBUILD). You can use the
/win32res flag to embed a Win32 resource.

This means you will have to create a .rc file, run it through the
Resource Compiler (used in Visual C++ typically) and then point to the
resulting .res file with the /win32res flag.

Once you do that, in your code, you can call LoadCursor through the
P/Invoke layer. You to get the handle, you would pass the Module (in most
assemblies, there is only one Module, but you can create multi-module
assemblies) to the static GetHINSTANCE method on the Marshal class. You
would then pass this handle as the first parameter in the LoadCursor method.
Then, you should have your cursor handle, and you can pass that to the
Cursor object.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Paul Cheetham" <PA******@dsl.p ipex.com> wrote in message
news:Ow******** *****@TK2MSFTNG P15.phx.gbl...
Hi Nicholas,

I have tried that method, but I end up with a monochrome version of the
cursor instead of the colour version as it should be.
Since none of the .Net Cursor constructors seem able to load a colour
cursor correctly I was forced to try and load it using the Win32 API and
then try to assign it to the cursor class.
Any other ideas greatly appreciated.

Thanks.
Paul Cheetham
Nicholas Paldino [.NET/C# MVP] wrote:
Paul,

Why are you using LoadImage? If you are storing a cursor as an
embedded resource, you can just call the GetManifestReso urceStream (on
the Assembly class, passing the name of the resource, in this case, a
cursor, to get). That will return a Stream to you. You can then take
that Stream and pass that to the constructor of the Cursor class. At
that point, you have your cursor, and if you need the handle of it, you
can call the Handle property of the Cursor instance.

Hope this helps.


Dec 31 '05 #4

Nicholas,

Thankyou for your help.
I will try your suggestion - I will create a Win32 .rc file and compile
it into my project (I am assuming there will be a way to do this in VS 2003)

Hopefully it should be reasonably straight forward from there.

Thankyou again.

Paul

Nicholas Paldino [.NET/C# MVP] wrote:
Paul,

Looking into this, it seems that ultimately, it uses some of the Ole
methods to load it as a picture first, and then load it as a cursor. I
think that along the way, this is where the colors get skewed.

In order to get a cursor that you would load through LoadCursor, you
will have to compile through the command line (or adjust the tasks in your
project file if you are using .NET 2.0 with MSBUILD). You can use the
/win32res flag to embed a Win32 resource.

This means you will have to create a .rc file, run it through the
Resource Compiler (used in Visual C++ typically) and then point to the
resulting .res file with the /win32res flag.

Once you do that, in your code, you can call LoadCursor through the
P/Invoke layer. You to get the handle, you would pass the Module (in most
assemblies, there is only one Module, but you can create multi-module
assemblies) to the static GetHINSTANCE method on the Marshal class. You
would then pass this handle as the first parameter in the LoadCursor method.
Then, you should have your cursor handle, and you can pass that to the
Cursor object.

Jan 1 '06 #5
Paul,

In VS.NET 2003 (rather, .NET 1.1 and before) there is no way to do this.
You have to use the command-line compiler (csc) with the options I
mentioned.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Paul Cheetham" <PA******@dsl.p ipex.com> wrote in message
news:ON******** ******@TK2MSFTN GP11.phx.gbl...

Nicholas,

Thankyou for your help.
I will try your suggestion - I will create a Win32 .rc file and compile it
into my project (I am assuming there will be a way to do this in VS 2003)

Hopefully it should be reasonably straight forward from there.

Thankyou again.

Paul

Nicholas Paldino [.NET/C# MVP] wrote:
Paul,

Looking into this, it seems that ultimately, it uses some of the Ole
methods to load it as a picture first, and then load it as a cursor. I
think that along the way, this is where the colors get skewed.

In order to get a cursor that you would load through LoadCursor, you
will have to compile through the command line (or adjust the tasks in
your project file if you are using .NET 2.0 with MSBUILD). You can use
the /win32res flag to embed a Win32 resource.

This means you will have to create a .rc file, run it through the
Resource Compiler (used in Visual C++ typically) and then point to the
resulting .res file with the /win32res flag.

Once you do that, in your code, you can call LoadCursor through the
P/Invoke layer. You to get the handle, you would pass the Module (in
most assemblies, there is only one Module, but you can create
multi-module assemblies) to the static GetHINSTANCE method on the Marshal
class. You would then pass this handle as the first parameter in the
LoadCursor method. Then, you should have your cursor handle, and you can
pass that to the Cursor object.

Jan 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6893
by: Simon Wigzell | last post by:
I have an image with several links in it in mapped areas. I am using a custom cursor on the page and would like to have a custom cursor appear on mouse over of the mapped links. Something like: This works for the body, font or img tag: <body style="cursor: url('cursor2.cur')"> <font style="cursor: url('cursor1.cur')">Text</font> <img...
0
1436
by: Christian H | last post by:
Hello, I've got some trouble trying to use a custom cursor. I'm trying to use ResourceManager to load the cursor from a whatever resX file I choose to have it in. It seems that the Cursor() constructor expects a Stream. What I get from Resourcemanager.GetObject() is an Object. Does anyone know how to convert the Object to a byte Stream...
2
10591
by: Paul Cheetham | last post by:
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.
2
3890
by: Bubbles Van | last post by:
I'm trying to load an animated cursor resource directly from the exe file. I have added it in into the assembly and set it's build action to embedded resource. I have a function written as follows: Private Function GetResCursor(ByVal strIdentifier As String) As Cursor Return New...
4
2804
by: Schorschi | last post by:
I am trying to mimic the following (which works) but Use the stock GetManifestResourceStream route. For example Public Declare Unicode Function LoadCursorFromFile Lib "USER32.DLL" Alias "LoadCursorFromFileW" (ByVal thePath As String) As IntPtr Dim theCursor As Cursor
3
3296
by: Sam | last post by:
Hi All Has anyone ever created a custom cursor that is bigger than 32x32 pixels? Is it possible? Regards, Sam
3
1404
by: Academic | last post by:
If I do the following: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor Do I need to reset the cursor or will that happen automatically when I leave the subroutine?
1
3023
by: Wilfried Mestdagh | last post by:
Hi, I'm trying to create a custom cursor with following code: Bitmap bmp = new Bitmap(@"c:\1.png"); Graphics g = Graphics.FromImage(bmp); Cursor cursor = new Cursor(bmp.GetHicon()); this.Cursor = cursor; But I get the exception: "A graphic object cannot be created with an
4
3525
by: bern11 | last post by:
How do you embed a custom cursor? The help says: // On the command line: // Add the following flag: // /res:CursorFileName.Cur,Namespace.CursorFileName.Cur What if you are compiling with the IDE? I don't see a 'command line options' field anywhere...???
0
7690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7909
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8115
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7654
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6273
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5216
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3649
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
930
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.