I know the .Net Cursor class doesn't work with color cursors. So I'm
currently using the LoadCursorFromFile API with reflection to set color
cursors:
here is my code:
[DllImport("user32.dll")] public static extern IntPtr LoadCursorFromFile(
string fileName );
IntPtr hwdCursor= LoadCursorFromFile( "color.cur" );
myCursor.GetType().InvokeMember("handle",BindingFl ags.Public |
BindingFlags.NonPublic |BindingFlags.Instance |
System.Reflection.BindingFlags.SetField,null,myCur sor,new object [] {
hwdCursor} );
It works great, but I'd like to be able to embed the cursors. I've been
trying to get theLoadCursor API to work with embedded resources and I am not
having any luck.
here is the code that doesn't work:
[DllImport("user32.dll")] public static extern IntPtr LoadCursor( long hwd,
string fileName );
private static IntPtr hInstance =
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly ().GetModules()[0]);
private static IntPtr hwdRedCursor=
LoadCursor((long)hInstance,"color.cur" );
Any suggestions? 7 3533
This works for me
Andrew
public static Cursor LoadColorCursor(string resourceName,Point hotspot)
{
Assembly ass = Assembly.GetCallingAssembly();
Stream stream = ass.GetManifestResourceStream(resourceName);
// In Windows 2000 in 16 bit color mode, the cursor mask doesn't work
properly
// I have no idea why but the simple loading the cursor from the
resource stream
// works in this case - again, I've no idea why!!!
if((Environment.OSVersion.Version.Major < 5 ||
(Environment.OSVersion.Version.Major == 5 &&
Environment.OSVersion.Version.Minor == 0)) &&
GetColorDepth() == 16)
return new Cursor(stream);
else
{
using(Bitmap bm = new Bitmap(stream))
{
NativeMethods.ICONINFO ii = new NativeMethods.ICONINFO();
ii.IsIcon = false;
ii.xHotspot = hotspot.X;
ii.yHotspot = hotspot.Y;
IntPtr imgHandle = bm.GetHbitmap();
try
{
ii.MaskBitmap = imgHandle;
ii.ColorBitmap = imgHandle;
IntPtr handle = SafeNativeMethods.CreateIconIndirect(ref ii);
return new Cursor(handle);
}
finally
{
SafeNativeMethods.DeleteObject(imgHandle);
}
}
}
}
"H. Williams" wrote: I know the .Net Cursor class doesn't work with color cursors. So I'm currently using the LoadCursorFromFile API with reflection to set color cursors:
here is my code:
[DllImport("user32.dll")] public static extern IntPtr LoadCursorFromFile( string fileName ); IntPtr hwdCursor= LoadCursorFromFile( "color.cur" ); myCursor.GetType().InvokeMember("handle",BindingFl ags.Public | BindingFlags.NonPublic |BindingFlags.Instance | System.Reflection.BindingFlags.SetField,null,myCur sor,new object [] { hwdCursor} );
It works great, but I'd like to be able to embed the cursors. I've been trying to get theLoadCursor API to work with embedded resources and I am not having any luck.
here is the code that doesn't work:
[DllImport("user32.dll")] public static extern IntPtr LoadCursor( long hwd, string fileName ); private static IntPtr hInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly ().GetModules()[0]); private static IntPtr hwdRedCursor= LoadCursor((long)hInstance,"color.cur" );
Any suggestions?
thanks for your reply, but the Cursor class doesn't work with COLOR cursors.
In your code, the line 'return new Cursor(stream)' causes the color cursor
to be converted to black and white. Also the line
'using(Bitmap bm = new Bitmap(stream))' throws a 'System.ArgumentException,
Invalid Parameter Used'
I think the only way to get the cursors in color is to load the cursor and
use reflection to point the current cursor handle to the handle of the color
cursor. This is easy with the LoadCursorFromFile API method because the
cursor files return a handle. However, I don't want to distribute cursors
with my program so I need help on how to get a handle to an embedded
resource.
"AndrewEames" <An*********@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com... This works for me Andrew
public static Cursor LoadColorCursor(string resourceName,Point hotspot) { Assembly ass = Assembly.GetCallingAssembly(); Stream stream = ass.GetManifestResourceStream(resourceName); // In Windows 2000 in 16 bit color mode, the cursor mask doesn't work properly // I have no idea why but the simple loading the cursor from the resource stream // works in this case - again, I've no idea why!!! if((Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0)) && GetColorDepth() == 16)
return new Cursor(stream); else { using(Bitmap bm = new Bitmap(stream)) { NativeMethods.ICONINFO ii = new NativeMethods.ICONINFO(); ii.IsIcon = false; ii.xHotspot = hotspot.X; ii.yHotspot = hotspot.Y; IntPtr imgHandle = bm.GetHbitmap(); try { ii.MaskBitmap = imgHandle; ii.ColorBitmap = imgHandle; IntPtr handle = SafeNativeMethods.CreateIconIndirect(ref ii); return new Cursor(handle); } finally { SafeNativeMethods.DeleteObject(imgHandle); } } } }
"H. Williams" wrote:
I know the .Net Cursor class doesn't work with color cursors. So I'm currently using the LoadCursorFromFile API with reflection to set color cursors:
here is my code:
[DllImport("user32.dll")] public static extern IntPtr LoadCursorFromFile( string fileName ); IntPtr hwdCursor= LoadCursorFromFile( "color.cur" ); myCursor.GetType().InvokeMember("handle",BindingFl ags.Public | BindingFlags.NonPublic |BindingFlags.Instance | System.Reflection.BindingFlags.SetField,null,myCur sor,new object [] { hwdCursor} );
It works great, but I'd like to be able to embed the cursors. I've been trying to get theLoadCursor API to work with embedded resources and I am not having any luck.
here is the code that doesn't work:
[DllImport("user32.dll")] public static extern IntPtr LoadCursor( long hwd, string fileName ); private static IntPtr hInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly ().GetModules()[0]); private static IntPtr hwdRedCursor= LoadCursor((long)hInstance,"color.cur" );
Any suggestions?
Assuming no one comes up with anything better, you could embed the cursor,
write it to a temp file, and then use LoadCursorFromFile on that.
"H. Williams" <hw*******@oslaw.com> wrote in message
news:ES*******************@newssvr27.news.prodigy. net... thanks for your reply, but the Cursor class doesn't work with COLOR cursors. In your code, the line 'return new Cursor(stream)' causes the color cursor to be converted to black and white. Also the line 'using(Bitmap bm = new Bitmap(stream))' throws a 'System.ArgumentException, Invalid Parameter Used' I think the only way to get the cursors in color is to load the cursor and use reflection to point the current cursor handle to the handle of the color cursor. This is easy with the LoadCursorFromFile API method because the cursor files return a handle. However, I don't want to distribute cursors with my program so I need help on how to get a handle to an embedded resource.
"AndrewEames" <An*********@discussions.microsoft.com> wrote in message news:D7**********************************@microsof t.com... This works for me Andrew
public static Cursor LoadColorCursor(string resourceName,Point hotspot) { Assembly ass = Assembly.GetCallingAssembly(); Stream stream = ass.GetManifestResourceStream(resourceName); // In Windows 2000 in 16 bit color mode, the cursor mask doesn't work properly // I have no idea why but the simple loading the cursor from the resource stream // works in this case - again, I've no idea why!!! if((Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0)) && GetColorDepth() == 16)
return new Cursor(stream); else { using(Bitmap bm = new Bitmap(stream)) { NativeMethods.ICONINFO ii = new NativeMethods.ICONINFO(); ii.IsIcon = false; ii.xHotspot = hotspot.X; ii.yHotspot = hotspot.Y; IntPtr imgHandle = bm.GetHbitmap(); try { ii.MaskBitmap = imgHandle; ii.ColorBitmap = imgHandle; IntPtr handle = SafeNativeMethods.CreateIconIndirect(ref ii); return new Cursor(handle); } finally { SafeNativeMethods.DeleteObject(imgHandle); } } } }
"H. Williams" wrote:
I know the .Net Cursor class doesn't work with color cursors. So I'm currently using the LoadCursorFromFile API with reflection to set color cursors:
here is my code:
[DllImport("user32.dll")] public static extern IntPtr LoadCursorFromFile( string fileName ); IntPtr hwdCursor= LoadCursorFromFile( "color.cur" ); myCursor.GetType().InvokeMember("handle",BindingFl ags.Public | BindingFlags.NonPublic |BindingFlags.Instance | System.Reflection.BindingFlags.SetField,null,myCur sor,new object [] { hwdCursor} );
It works great, but I'd like to be able to embed the cursors. I've been trying to get theLoadCursor API to work with embedded resources and I am not having any luck.
here is the code that doesn't work:
[DllImport("user32.dll")] public static extern IntPtr LoadCursor( long hwd, string fileName ); private static IntPtr hInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly ().GetModules()[0]); private static IntPtr hwdRedCursor= LoadCursor((long)hInstance,"color.cur" );
Any suggestions?
That appears to be the only solution.
I found this note:
'The only thing that can't be done as things stand is to instantiate a color
cursor from a .NET resource file without writing it to an intermediate
temporary file on disk. This is because LoadImage (or LoadCursor)
unfortunately does not provide a stream or byte array based overload.'
"James Park" <so*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... Assuming no one comes up with anything better, you could embed the cursor, write it to a temp file, and then use LoadCursorFromFile on that.
"H. Williams" <hw*******@oslaw.com> wrote in message news:ES*******************@newssvr27.news.prodigy. net... thanks for your reply, but the Cursor class doesn't work with COLOR cursors. In your code, the line 'return new Cursor(stream)' causes the color cursor to be converted to black and white. Also the line 'using(Bitmap bm = new Bitmap(stream))' throws a 'System.ArgumentException, Invalid Parameter Used' I think the only way to get the cursors in color is to load the cursor and use reflection to point the current cursor handle to the handle of the color cursor. This is easy with the LoadCursorFromFile API method because the cursor files return a handle. However, I don't want to distribute cursors with my program so I need help on how to get a handle to an embedded resource.
"AndrewEames" <An*********@discussions.microsoft.com> wrote in message news:D7**********************************@microsof t.com... This works for me Andrew
public static Cursor LoadColorCursor(string resourceName,Point hotspot) { Assembly ass = Assembly.GetCallingAssembly(); Stream stream = ass.GetManifestResourceStream(resourceName); // In Windows 2000 in 16 bit color mode, the cursor mask doesn't work properly // I have no idea why but the simple loading the cursor from the resource stream // works in this case - again, I've no idea why!!! if((Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0)) && GetColorDepth() == 16)
return new Cursor(stream); else { using(Bitmap bm = new Bitmap(stream)) { NativeMethods.ICONINFO ii = new NativeMethods.ICONINFO(); ii.IsIcon = false; ii.xHotspot = hotspot.X; ii.yHotspot = hotspot.Y; IntPtr imgHandle = bm.GetHbitmap(); try { ii.MaskBitmap = imgHandle; ii.ColorBitmap = imgHandle; IntPtr handle = SafeNativeMethods.CreateIconIndirect(ref ii); return new Cursor(handle); } finally { SafeNativeMethods.DeleteObject(imgHandle); } } } }
"H. Williams" wrote:
I know the .Net Cursor class doesn't work with color cursors. So I'm currently using the LoadCursorFromFile API with reflection to set color cursors:
here is my code:
[DllImport("user32.dll")] public static extern IntPtr LoadCursorFromFile( string fileName ); IntPtr hwdCursor= LoadCursorFromFile( "color.cur" ); myCursor.GetType().InvokeMember("handle",BindingFl ags.Public | BindingFlags.NonPublic |BindingFlags.Instance | System.Reflection.BindingFlags.SetField,null,myCur sor,new object [] { hwdCursor} );
It works great, but I'd like to be able to embed the cursors. I've been trying to get theLoadCursor API to work with embedded resources and I am not having any luck.
here is the code that doesn't work:
[DllImport("user32.dll")] public static extern IntPtr LoadCursor( long hwd, string fileName ); private static IntPtr hInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly ().GetModules()[0]); private static IntPtr hwdRedCursor= LoadCursor((long)hInstance,"color.cur" );
Any suggestions?
I just tried out another solution, but it's a bit of a pain. Create a
resource file (.rc) that includes the cursor using VC++, create a win32
resource file (.res) from it with Resource Compiler (rc.exe), use the
compiler option /win32res to attach it, and then use LoadCursor in normal
fashion. I used IntPtr instead of string for lpCursorName argument though.
"H. Williams" <hw*******@oslaw.com> wrote in message
news:%g*****************@newssvr12.news.prodigy.co m... That appears to be the only solution. I found this note: 'The only thing that can't be done as things stand is to instantiate a color cursor from a .NET resource file without writing it to an intermediate temporary file on disk. This is because LoadImage (or LoadCursor) unfortunately does not provide a stream or byte array based overload.'
"James Park" <so*****@hotmail.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl... Assuming no one comes up with anything better, you could embed the cursor, write it to a temp file, and then use LoadCursorFromFile on that.
"H. Williams" <hw*******@oslaw.com> wrote in message news:ES*******************@newssvr27.news.prodigy. net... thanks for your reply, but the Cursor class doesn't work with COLOR cursors. In your code, the line 'return new Cursor(stream)' causes the color cursor to be converted to black and white. Also the line 'using(Bitmap bm = new Bitmap(stream))' throws a 'System.ArgumentException, Invalid Parameter Used' I think the only way to get the cursors in color is to load the cursor and use reflection to point the current cursor handle to the handle of the color cursor. This is easy with the LoadCursorFromFile API method because the cursor files return a handle. However, I don't want to distribute cursors with my program so I need help on how to get a handle to an embedded resource.
"AndrewEames" <An*********@discussions.microsoft.com> wrote in message news:D7**********************************@microsof t.com... This works for me Andrew
public static Cursor LoadColorCursor(string resourceName,Point hotspot) { Assembly ass = Assembly.GetCallingAssembly(); Stream stream = ass.GetManifestResourceStream(resourceName); // In Windows 2000 in 16 bit color mode, the cursor mask doesn't work properly // I have no idea why but the simple loading the cursor from the resource stream // works in this case - again, I've no idea why!!! if((Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0)) && GetColorDepth() == 16)
return new Cursor(stream); else { using(Bitmap bm = new Bitmap(stream)) { NativeMethods.ICONINFO ii = new NativeMethods.ICONINFO(); ii.IsIcon = false; ii.xHotspot = hotspot.X; ii.yHotspot = hotspot.Y; IntPtr imgHandle = bm.GetHbitmap(); try { ii.MaskBitmap = imgHandle; ii.ColorBitmap = imgHandle; IntPtr handle = SafeNativeMethods.CreateIconIndirect(ref ii); return new Cursor(handle); } finally { SafeNativeMethods.DeleteObject(imgHandle); } } } }
"H. Williams" wrote:
> I know the .Net Cursor class doesn't work with color cursors. So I'm > currently using the LoadCursorFromFile API with reflection to set > color > cursors: > > here is my code: > > [DllImport("user32.dll")] public static extern IntPtr > LoadCursorFromFile( > string fileName ); > IntPtr hwdCursor= LoadCursorFromFile( "color.cur" ); > myCursor.GetType().InvokeMember("handle",BindingFl ags.Public | > BindingFlags.NonPublic |BindingFlags.Instance | > System.Reflection.BindingFlags.SetField,null,myCur sor,new object [] { > hwdCursor} ); > > It works great, but I'd like to be able to embed the cursors. I've > been > trying to get theLoadCursor API to work with embedded resources and I > am not > having any luck. > > here is the code that doesn't work: > > [DllImport("user32.dll")] public static extern IntPtr LoadCursor( long > hwd, > string fileName ); > private static IntPtr hInstance = > Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly ().GetModules()[0]); > private static IntPtr hwdRedCursor= > LoadCursor((long)hInstance,"color.cur" ); > > Any suggestions? > > >
Here's my somewhat long solution using the temp file. It's fast, there are
no additional files to include with the program, and it works.
private static IntPtr GetCursorHandle(string sResource)
{
//Load cursor from Manifest Resource to Stream
Stream streamFrom =
Assembly.GetExecutingAssembly().GetManifestResourc eStream(sResource);
Stream streamTo =
File.Create(Environment.GetEnvironmentVariable("TE MP")+@"\~cur.tmp");
BinaryReader br = new BinaryReader(streamFrom);
BinaryWriter bw = new BinaryWriter(streamTo);
//Write cursor to temporary file
bw.Write(br.ReadBytes((int)streamFrom.Length));
bw.Flush();
bw.Close();
br.Close();
bw=null;
br=null;
streamFrom.Close();
streamTo.Close();
streamFrom=null;
streamTo=null;
//Load handle of temporary cursor file
IntPtr hwdCursor = LoadCursorFromFile(
Environment.GetEnvironmentVariable("TEMP")+@"\~cur .tmp" );
//Delete temporary cursor file
File.Delete(Environment.GetEnvironmentVariable("TE MP")+@"\~cur.tmp");
return hwdCursor;
}
Did you actually try this? The Cursor class does actually work with color
cursors in some circumstances.
The line 'return new Cursor(stream)' is only executed under Windows 2000 on
16 bit displays and as the comment indicates, I dont why this trick works in
that case
Andrew
"H. Williams" wrote: thanks for your reply, but the Cursor class doesn't work with COLOR cursors. In your code, the line 'return new Cursor(stream)' causes the color cursor to be converted to black and white. Also the line 'using(Bitmap bm = new Bitmap(stream))' throws a 'System.ArgumentException, Invalid Parameter Used' I think the only way to get the cursors in color is to load the cursor and use reflection to point the current cursor handle to the handle of the color cursor. This is easy with the LoadCursorFromFile API method because the cursor files return a handle. However, I don't want to distribute cursors with my program so I need help on how to get a handle to an embedded resource.
"AndrewEames" <An*********@discussions.microsoft.com> wrote in message news:D7**********************************@microsof t.com... This works for me Andrew
public static Cursor LoadColorCursor(string resourceName,Point hotspot) { Assembly ass = Assembly.GetCallingAssembly(); Stream stream = ass.GetManifestResourceStream(resourceName); // In Windows 2000 in 16 bit color mode, the cursor mask doesn't work properly // I have no idea why but the simple loading the cursor from the resource stream // works in this case - again, I've no idea why!!! if((Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0)) && GetColorDepth() == 16)
return new Cursor(stream); else { using(Bitmap bm = new Bitmap(stream)) { NativeMethods.ICONINFO ii = new NativeMethods.ICONINFO(); ii.IsIcon = false; ii.xHotspot = hotspot.X; ii.yHotspot = hotspot.Y; IntPtr imgHandle = bm.GetHbitmap(); try { ii.MaskBitmap = imgHandle; ii.ColorBitmap = imgHandle; IntPtr handle = SafeNativeMethods.CreateIconIndirect(ref ii); return new Cursor(handle); } finally { SafeNativeMethods.DeleteObject(imgHandle); } } } }
"H. Williams" wrote:
I know the .Net Cursor class doesn't work with color cursors. So I'm currently using the LoadCursorFromFile API with reflection to set color cursors:
here is my code:
[DllImport("user32.dll")] public static extern IntPtr LoadCursorFromFile( string fileName ); IntPtr hwdCursor= LoadCursorFromFile( "color.cur" ); myCursor.GetType().InvokeMember("handle",BindingFl ags.Public | BindingFlags.NonPublic |BindingFlags.Instance | System.Reflection.BindingFlags.SetField,null,myCur sor,new object [] { hwdCursor} );
It works great, but I'd like to be able to embed the cursors. I've been trying to get theLoadCursor API to work with embedded resources and I am not having any luck.
here is the code that doesn't work:
[DllImport("user32.dll")] public static extern IntPtr LoadCursor( long hwd, string fileName ); private static IntPtr hInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly ().GetModules()[0]); private static IntPtr hwdRedCursor= LoadCursor((long)hInstance,"color.cur" );
Any suggestions? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Anthony Carrabino |
last post by:
VistaDB 2.0.14 embedded database engine update for .NET has been released.
Features better .NET Provider performance, new VistaDBDataSet component that
provides live data cursors that are fully...
|
by: T.S.Negi |
last post by:
Hi All,
I want to avoid using cursors and loops in stored procedures.
Please suggest alternate solutions with example (if possible).
Any suggestion in these regards will be appreciated.
...
|
by: Mark Yudkin |
last post by:
The documentation is unclear (at least to me) on the permissibility of
accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003,
with separate transactions scope, from separate...
|
by: Andrew Roesch |
last post by:
Hello all,
I believe I have a tough one today. I'm trying to load a color cursor from
an embedded resource in C# and am having quite a bit of trouble doing it.
Let me tell what I have found out...
|
by: Andrew Roesch |
last post by:
If anyone has any ideas, or a better forum to post in, please let me know.
I'm trying to load a color cursor from
an embedded resource in C# and am having quite a bit of trouble doing it.
Let me...
| |
by: a |
last post by:
Hello,
I am doing some multithreading in an MDI app, and I can't seem to get the
cursor to stay as an Hourglass. I call:
Cursor.Current = cursors.wait
at the beginning of my routing, and...
|
by: Just Me |
last post by:
Does Me.Cursor.Current=Cursors.WaitCursor
set the current property of Me.Cursor to Cursors.WaitCursor
And Me.Cursor.Current=Cursors.Default set the Me.Current
property to something (default)...
|
by: VictorReinhart |
last post by:
Hi,
I am intersted in trying to reduce the cost of C# development, by
reducing the number of lines of code. In my opinion, as a business
developer, the biggest opportunity to reduce the number of...
|
by: Dima Kuchin |
last post by:
Hello,
I was trying to find the information about when and where should I use
cursors in DB2, no luck. Maybe you can point me to some article that
describes just that (or tell me which page is...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
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,...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |