0

I'm trying to draw a custom crosshair cursor.

This is my function to create the "cross cursor".

The problem is that it only draw the positive coordinates. So I get the half side of the cross.

enter image description here

So the red in this cross isn't visible when I draw it. A solution would be to make the cross all with positive coordinates but I don't want this.

Any solutions?

private Cursor crossCursor(System.Drawing.Pen pen, int x, int y)
{
    var pic = new Bitmap(x, y);
    var gr = Graphics.FromImage(pic);

    //gr.Clip = new Region(new RectangleF(-x / 2, -y / 2, x, y));

    var pathX = new GraphicsPath();
    var pathY = new GraphicsPath();
    pathY.AddLine(new Point(0, -y), new Point(0, y));
    pathX.AddLine(new Point(-x, 0), new Point(x, 0));
    gr.DrawPath(pen, pathX);
    gr.DrawPath(pen, pathY);

    var stream = new MemoryStream();
    Icon.FromHandle(pic.GetHicon()).Save(stream);

    // Convert saved file into .cur format
    stream.Seek(2, SeekOrigin.Begin);
    stream.WriteByte(2);
    stream.Seek(10, SeekOrigin.Begin);
    stream.Seek(0, SeekOrigin.Begin);

    // Construct Cursor
    return new Cursor(stream);
}
John Willemse
  • 6,608
  • 7
  • 31
  • 45
dg90
  • 1,243
  • 3
  • 17
  • 30
  • 1
    Why can't you use positive coords? – Sayse Jul 03 '14 at 06:56
  • If I work with positive coordinates the problem is the actual hitPoint of my cursor would be the TOP LEFT of the cross and not the middle. – dg90 Jul 03 '14 at 07:09
  • the hit point has nothing to do with your drawing code, [See this question](http://stackoverflow.com/q/11526351/1324033), or [msdn](http://msdn.microsoft.com/en-us/library/0b1674x8.aspx) – Sayse Jul 03 '14 at 07:11
  • If I draw the cross correct that problem is fixed. Do you have another suggestion fixing my hitPoint problem ? – dg90 Jul 03 '14 at 07:13

1 Answers1

1

If the parameters x and y are, say, 10 each, you would create a bitmap of 10x10 in your code.

So this line:

pathY.AddLine(new Point(0, -y), new Point(0, y));

Translates to:

pathY.AddLine(new Point(0, -10), new Point(0, 10));

The point (0,-10) lies outside of your bitmap and therefore will be invisible.

There's no way you can draw inside the bitmap and show everything if you insist on using negative values...

John Willemse
  • 6,608
  • 7
  • 31
  • 45
  • Original my code was -y /2 and y / 2 so that would be the same. And that doesn't solves the problem – dg90 Jul 03 '14 at 06:59
  • If I work with positive coordinates the problem is the actual hitPoint of my cursor would be the TOP LEFT of the cross and not the middle. – dg90 Jul 03 '14 at 07:02
  • 1
    You can change the hotspot of a custom cursor in .NET. See [the accepted answer to this question](http://stackoverflow.com/questions/550918/change-cursor-hotspot-in-winforms-net). – John Willemse Jul 03 '14 at 07:12