blah blah blah is here! blah blah » Close

up0down
link

I am trying to make a custom cursor in C#. How can I do this? I have found this code which lets me change the cursor to text:
// create any bitmap
Bitmap b = new Bitmap(55, 25);
Graphics g = Graphics.FromImage(b);
// do whatever you wish
g.DrawString("+", this.Font, Brushes.Black, 0, 0);
// this is the trick!
IntPtr ptr = b.GetHicon();
Cursor c = new Cursor(ptr);
// attach cursor to the form
this.Cursor = c;
But I don't know how to make it use a custom bitmap I made. Is this possible? If not I can just use the text '+' because I am trying to make a crosshair. But how can I make the + bigger using the code above?
Thanks for any help!

last answered 9 months ago

1 answers

up0down
link

Ehhm I'm not sure but how about loading the Image to a Bitmap and then use it as a Cursor???
Bitmap b = Bitmap.FromFile("c:\\myCursor.bmp");
IntPtr ptr = b.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
.... or if you want do draw it
Bitmap b = new Bitmap(25,25);
Graphics g = Graphics.FromImage(b);
g.Clear(Color.White); // optional drawing white background
g.DrawLine(new Pen(Color.Black, 5.0F), new Point(13,0), new Point(13,25)); // vertical line
g.DrawLine(new Pen(Color.Black, 5.0F), new Point(0,13), new Point(25,13)); // horizontal line
g.Flush();
IntPtr ptr = b.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
Greet$
WitchHunter

up0down
link

Ok thanks for helping!
So if I load a bmp, do you know if this will work? When I specify the bmp location, will it keep it part of the program, so if I give it to a friend he can use it, too?
In the part where I create a crosshair with DrawLine, what do I change for sizes, length etc??
Sorry, but I am a very early beginner at this! Thanks.

up0down
link

I thought this would be obvious enough ...
g.DrawLine(new Pen(Color.Black, 5.0F), new Point(13,0), new Point(13,25));
g.DrawLine(
new Pen(Color.Black, _Width_), // define a Pen Colored black with a certain width
new Point(_X1_, _Y1_), // Starting Point
new Point(_X2_, _Y2_) // End Point
);
... By the meaning of loading a Bitmap. It does not matter what type of Image you load could be GIF, PNG, JPG, what ever ... beacuse Bitmap inherits Image so ...
and "give it to a friend - will he be able to " sure as long as you either load from relative paths or define the Image to be in a specific folder ... you could also put the image in you resources loding it from there ... there are so many ways to do this ...
Greet$
WitchHunter

up0down
link

Thank you so much WitchHunter. I really appreciate it.
I thought I would post the code I altered to show how I created a crosshair:
Bitmap b = new Bitmap(25, 25);
Graphics g = Graphics.FromImage(b);

g.DrawLine(new Pen(Color.Black, 1.0F), new Point(12, 0), new Point(12, 8)); // vertical line
g.DrawLine(new Pen(Color.Black, 1.0F), new Point(12, 17), new Point(12, 25)); // vertical line
g.DrawLine(new Pen(Color.Black, 1.0F), new Point( 0, 12), new Point( 8, 12)); // horizontal line
g.DrawLine(new Pen(Color.Black, 1.0F), new Point(16, 12), new Point(24, 12)); // horizontal line
g.DrawLine(new Pen(Color.Black, 1.0F), new Point(12, 12), new Point(12, 13)); // Middle dot
g.Flush();
This makes it so there is a space in the center with a . so it looks like a realy crosshair and not just a cross.
Thanks!
IntPtr ptr = b.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;

up0down
link

Believe it or not I am having a lot of trouble using a referenced image as the cursor....
So I added a picturebox and selected the image. That image is now a resource, correct? So what do I do in this following code to get it to use that?
Bitmap b = Bitmap.FromFile("c:\\myCursor.bmp");
IntPtr ptr = b.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
----
I know I will need to change the "From File" to FromResource, but then what syntax do I use after that? I looked up the code from the picture box and here is what I think I will need:
this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
So what do I take from that to put up in the bitmap part?
Thanks Witchhunter or whoever else can help me! I really want to learn this.

up0down
link

Use ResourceManager to access and control the resources.
private System.Resources.ResourceManager rm =
new System.Resources.ResourceManager("ResourceClient.Images",
System.Reflection.Assembly.GetExecutingAssembly());
Now you can easily retrieve your specific objects and strings by using the GetObject and GetString methods.
lblCancel.Image = (Image)rm.GetObject("cancel-on.png");
this.lblResource.Text = rm.GetString("MyResourceName");
Greet$
Witchhunter

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback