Enhanced crosshair cursor possible?

2019-07-22 05:57发布

I have found this question (as a few others), but this is the one I have implemented so far:

Crosshair cursor with additional lines in C#

As it states, I can use a stock cursor "cross" directly in the IDE. This is a really good way to do things. The answer specified in the answer above draws a cross on the screen at the given width / height. Eg:

private Cursor crossCursor(Pen pen, Brush brush, 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);

    IntPtr ptr = pic.GetHicon();
    var c = new Cursor(ptr);
    return c;
}

My issue is that I want my cross hairs to extend to the Bounds of the viewing area. To provide context here, I have:

//Form
  //TableLayoutPanel
      //UserControl (fills the TableLayoutPanel visible area)

So how can I adjust my cursor so that the lines extend (much like in CAD pacakages)?

Thanks.

Update: I have tried calling the method from here:

protected override void OnLoad(System.EventArgs e)
{
    Cursor = crossCursor(Pens.WhiteSmoke, Brushes.WhiteSmoke, Bounds.Width, Bounds.Height);
}

But it is not Ok because at this point in time Bounds is returning a dimension of 150 by 150 which is not the size of the TableLayoutPanel.

Update: I have adjuted it to use the Resize handler instead and it does improve things:

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);

    Cursor = crossCursor(Pens.WhiteSmoke, Brushes.WhiteSmoke, Bounds.Width, Bounds.Height);
}

The only problem now (and it kind of makes sense I suppose) is that the cursor will only take the full width and height of the view when it is central to the view. As soon as I move about in the view that cursor does not adjust. I always want a horizontal/vertical line through the mouse position (not just the initial cross).

See:

Crosshairs

The crosshairs need extending (the thicker red lines). Either I need to constantly create the cursor as the mouse moves or construct the two lines another way. What to do?

I came across this:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/7bdbad6d-1f65-461b-8f0c-6ef4f243fa6b/crosshair-cursor-using-c?forum=csharpgeneral

So, instead of changing the cursor object I now draw lines in the controls MouseMove handler:

Region r = new Region();
r.Union(new Rectangle(0, lastY, this.Width, 1));
r.Union(new Rectangle(lastX, 0, 1, this.Height));
this.Invalidate(r);
this.Update();
Graphics g = Graphics.FromHwnd(this.Handle);
g.DrawLine(Pens.White, 0, e.Y, this.Width, e.Y);
g.DrawLine(Pens.White, e.X, 0, e.X, this.Height);
int intDiameter = 20;//the diameter of this circle
g.DrawEllipse(Pens.White, e.X - intDiameter / 2, e.Y - intDiameter / 2, 20, 20);
//to draw the circle
lastX = e.X;
lastY = e.Y;

It works, but I get noticiable screen flicker doing it this way.

1条回答
唯我独甜
2楼-- · 2019-07-22 06:34

You don't need to create a cursor. You can create a double buffered control and draw cross over control.

using System;
using System.Drawing;
using System.Windows.Forms;
public class DrawingSurface : Control
{
    Pen crossPen;
    Pen rectanglePen;
    Brush rectangleBrush;

    public DrawingSurface()
    {
        this.DoubleBuffered = true;
        this.ResizeRedraw = true;
        crossPen = new Pen(Color.Red, 2);
        rectangleBrush = new SolidBrush(Color.FromArgb(50, Color.Blue));
        rectanglePen = new Pen(Color.Blue, 1);
    }
    bool mouseDown = false;
    Point startPoint = Point.Empty;
    Point endPoint = Point.Empty;
    protected override void OnMouseDown(MouseEventArgs e)
    {
        startPoint = e.Location;
        mouseDown = true;
        base.OnMouseDown(e);
    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
        mouseDown = false;
        base.OnMouseUp(e);
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        endPoint = e.Location;
        this.Invalidate();
        base.OnMouseMove(e);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        var g = e.Graphics;

        if (this.ClientRectangle.Contains(endPoint))
            DrawCross(e.Graphics, endPoint);

        if (mouseDown)
            DrawRectangle(e.Graphics, startPoint, endPoint);
    }

    void DrawCross(Graphics g, Point point)
    {
        g.DrawLine(crossPen, new Point(0, point.Y), new Point(Width, point.Y));
        g.DrawLine(crossPen, new Point(point.X, 0), new Point(point.X, Height));
    }
    void DrawRectangle(Graphics g, Point point1, Point point2)
    {
        var rectangle = new Rectangle(
            Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y),
            Math.Abs(point1.X - point2.X), Math.Abs(point1.Y - point2.Y));

        g.FillRectangle(rectangleBrush, rectangle);
        g.DrawRectangle(rectanglePen, rectangle);
    }
    protected override void Dispose(bool disposing)
    {
        crossPen.Dispose();
        rectanglePen.Dispose();
        rectangleBrush.Dispose();
        base.Dispose(disposing);
    }
}

enter image description here

查看更多
登录 后发表回答