473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Color Custom Cursor in .NET 2.0 C#

Does anyone know how to successfully load a custom color (32 bits) cursor in C# (under XP..NET 2.0 - or NET3.0 eventually)?
All I could do using C# .Net support was loading a color cursor but ending up getting it displayed in a black and white. I found an example that manages to display the cursor in color (using GetHIcon) , but I do have a hard time making its background transparent: here is the code that creates the cursor from a image file:

// create any bitmap
Bitmap b = new Bitmap( 250,170 );
Graphics g = Graphics.FromImage ( b );
// do whatever you wish
Image img = Image.FromFile("Image.bmp"));
g.DrawImage(img, 125, 85);
// this is the trick!?
IntPtr ptr = b.GetHicon();
Cursor c = new Cursor( ptr );
// attach cursor to the form
this.Cursor = c;

I have looked all over the web .. sort of ;-) but couldn't find any compelling documentation with an example that works.
Does anyone know how to make the image transparent in the above code?
Or perhaps there is a more elegant way of doing this?
Help is greatly appreciated!
Thank you
Paolo
Oct 4 '07 #1
1 3689
// create any bitmap
Bitmap b = new Bitmap( 250,170 );
Graphics g = Graphics.FromImage ( b );
// do whatever you wish
Image img = Image.FromFile("Image.bmp"));
g.DrawImage(img, 125, 85);
// this is the trick!?
IntPtr ptr = b.GetHicon();
Cursor c = new Cursor( ptr );
// attach cursor to the form
this.Cursor = c;
A bit of this code is unneccessary. Bitmap is a subclass of Image, so you can replace the first few lines:
Bitmap b = (Bitmap)Image.FromFile("Image.bmp");
b.MakeTransparent(Color.FromArgb(r, g, b)); //Put the color you want to make transparent here. May not be neccessary if you're loading an already transparent file
IntPtr ptr = b.GetHicon();
Cursor c = new Cursor( ptr );
this.Cursor = c;
Oct 14 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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: ...
0
by: Steve Rosenberry | last post by:
The following is a simplifed version of OnGiveFeedback() for my drag source control: protected override void OnGiveFeedback( GiveFeedbackEventArgs e ) { e.UseDefaultCursors = false; if(...
0
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()...
2
by: Brad | last post by:
Hi all, newbie here. I'm currently designing a custom splitter control... When the mouse hovers over a certain area on my splitter I need to be able to display a custom cursor that I've designed....
2
by: Steegg | last post by:
I am a newcomer to using Python and Qt and the main problem that I have is the dearth of any example code or books describing the use of Python and Qt together. My current problem is that I want...
6
by: Peter Oliphant | last post by:
I've now asked how three times in this forum with no answers. So I went to the 'windowsforms' newsgroup, and found this post, also unanswered, with the same problem: "Hello, I've tried to...
6
by: Erick | last post by:
I've created a class called Procs and a collection class called Processes which uses a hastable object to store the Procs. Now i want to enumerate with the "For each" to extract all the Procs in...
0
by: Andy Wynn | last post by:
Hello All, I've got a custom control ( Panel-like, but not inheriting from Panel) that has a header region that's custom drawn. Custom Control is using a custom controldesigner class that...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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,...
0
isladogs
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 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.