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 5 10028
Paul,
Why are you using LoadImage? If you are storing a cursor as an embedded
resource, you can just call the GetManifestResourceStream (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.com
"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:eY*************@TK2MSFTNGP15.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.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
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 GetManifestResourceStream (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.
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.com
"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:Ow*************@TK2MSFTNGP15.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 GetManifestResourceStream (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,
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.
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.com
"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:ON**************@TK2MSFTNGP11.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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);...
|
by: bern11 |
last post by:
How do you embed a custom cursor? The help says:
// On the command line:
// Add the following flag:
// ...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
| |