c# - 可以增强十字光标吗?

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

我的问题是我希望十字准线延伸到查看区域的边界。为了在这里提供上下文,我有:

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

那么如何调整光标以使线条延伸(就像在 CAD 软件包中一样)?

谢谢。

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

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

但是这不行,因为此时 Bounds 返回的尺寸为 150 x 150,这不是 TableLayoutPanel 的大小。

更新:我已将其调整为使用 Resize 处理程序,它确实有所改进:

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

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

现在唯一的问题(我认为这有点有意义)是,当光标位于 View 的中心时,光标只会占据 View 的完整宽度和高度。一旦我在 View 中移动,光标就不会调整。我总是想要一条穿过鼠标位置的水平/垂直线(不仅仅是初始十字)。

参见:

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;

它可以工作,但是我这样做时会出现明显的屏幕闪烁。

最佳答案

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

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

关于c# - 可以增强十字光标吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37542669/

相关文章:

java - 获取错误 java.lang.IllegalStateException : get field slot from row 0 col -1 failed

C#类自动调用它的方法

c# - 将 CancellationToken 与异步方法的任务相关联

带有对象列表的 C# if 语句语法

VB.net 开闭表格

C# Windows 窗体 - 从局部变量分配文本框值(数字)

javascript - 使用 next() 或 prev() 遍历 MeteorJS 游标

c# - 错误的日期格式 C#、Oracle

c# - 在 WinForms c# 中获得集中控制的最快方法?

sql-server - 使用游标的优缺点(在SQL Server中)