3

how to make a crosshair cursor with help lines like this on screenshots:

enter image description here

I know how to make crosshire cursor:

 this.Cursor = System.Windows.Forms.Cursors.Cross;

can be also something like that:

enter image description here

like in CAD software.

Elfoc
  • 3,649
  • 15
  • 46
  • 57
  • 1
    possible duplicate of [Using Custom Cursor WinForms](http://stackoverflow.com/questions/2797084/using-custom-cursor-winforms) – Emond Aug 24 '11 at 10:51
  • You need to make a .cur file first. Then you may embed it to the assembly. As it mentioned above. – PythEch Aug 24 '11 at 10:55

2 Answers2

5

This is the code I use. x and y are the dimensions. In my case I can have some text on the cursor and this is name. If you want dots or dashes then you need to do that with the pen.

   private Cursor crossCursor(Pen pen, Brush brush, string name, int x, int y) {
            var pic = new Bitmap(x, y);
            Graphics gr = Graphics.FromImage(pic);

            var pathX = new GraphicsPath();
            var pathY = new GraphicsPath();
            pathX.AddLine(0, y / 2, x, y / 2);
            pathY.AddLine(x / 2, 0, x / 2, y);
            gr.DrawPath(pen, pathX);
            gr.DrawPath(pen, pathY);
            gr.DrawString(name, Font, brush, x / 2 + 5, y - 35);

            IntPtr ptr = pic.GetHicon();
            var c = new Cursor(ptr);
            return c;
        }
ScruffyDuck
  • 2,606
  • 3
  • 34
  • 50
  • Nice job! ;) But if I were him/her I would use .cur (cursor) file. – PythEch Aug 24 '11 at 10:58
  • Thankyou ! Fair enough. I do it this way because the name value can change. I have them different sizes and also have another overload that will put a bulls-eye on the cursor – ScruffyDuck Aug 24 '11 at 11:00
  • I don't understand how to call this code inside my winform. – Andrew Truckle May 31 '16 at 09:39
  • 1
    Something like Cursor = crossCursor(......) where Cursor is the cursor for the form or for some control within the form – ScruffyDuck May 31 '16 at 12:06
  • Yep, got that now thanks. I have progressed this question though, here: http://stackoverflow.com/questions/37542669/enhanced-crosshair-cursor-possible as I need things to work a bit differently. – Andrew Truckle May 31 '16 at 12:30
0

Just create two label box as lab_X_Axis and lab_Y_Axis. In chart mousemove function code as shown below..

private void chart1_MouseMove(object sender, MouseEventArgs e)
{
    lab_X_Axis.Location = new Point((e.X), 21);
    lab_Y_Axis.Location = new Point(76, e.Y);
}

private void Form1_Load(object sender, EventArgs e)
{
    lab_X_Axis.AutoSize = false;
    lab_Y_Axis.AutoSize = false;
    lab_X_Axis.Text="";
    lab_Y_Axis.Text="";
    lab_X_Axes.Size = new Size(1, 300);
    lab_Y_Axes.Size = new Size(300, 1);
}