增强的十字光标可能吗?

时间:2016-05-31 10:04:51

标签: c# winforms cursor

我发现了这个问题(和其他几个一样),但这是我迄今为止实施的问题:

Crosshair cursor with additional lines in C#

正如它所述,我可以使用股票光标"交叉"直接在IDE中。这是一个非常好的做事方式。上面答案中指定的答案在给定宽度/高度的屏幕上绘制十字。例如:

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;
}

我的问题是我希望我的十字线延伸到观看区域的 Bounds 。为了提供上下文,我有:

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

那么如何调整光标以使线条延伸(就像CAD pacakages一样)?

感谢。

更新:我试过从这里调用方法:

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

但它并不好,因为在这个时间点,Bounds返回的维度为150乘150,而不是TableLayoutPanel的大小。

更新:我已将其调整为使用调整大小处理程序,它确实改进了:

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

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

现在唯一的问题(我认为这是有道理的)是​​当光标中心到视图时,光标只会占据视图的整个宽度和高度。一旦我在视图中移动光标不调整。我总是想要通过鼠标位置的水平/垂直线(而不仅仅是初始交叉)。

请参阅:

Crosshairs

十字准线需要延伸(较粗的红线)。要么我需要在鼠标移动时不断创建光标,要么以另一种方式构造两条线。怎么办?

我遇到了这个:

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

因此,我不是更改光标对象,而是在控件MouseMove处理程序中绘制线条:

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;

它可以工作,但是我这样做会让屏幕闪烁变得明显。

1 个答案:

答案 0 :(得分:1)

您不需要创建游标。您可以创建双缓冲控件并绘制交叉控件。

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

相关问题