c# - 如何在 PictureBox 上捕获鼠标坐标?

标签 c# .net winforms picturebox

我是 C# 的新手。我在 picturebox 上有一个图像,我想在图像上绘制一个矩形以捕获我将绘制的矩形的 X、Y 坐标和宽度/高度(针对 picturebox 上的图像)。我知道我必须在 pictureBox1_MouseEnter..etc 上做点什么。但我不知道从哪里开始。

    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {

        Rectangle Rect = RectangleToScreen(this.ClientRectangle);
    }

如果有人能给我示例代码,我将不胜感激。

谢谢。

最佳答案

您根本不想使用 MouseEnter。您想要使用 MouseDown,因为您不想在用户单击鼠标按钮之前开始跟踪用户绘制的矩形。

所以在 MouseDown事件处理方法,保存当前光标坐标。这是矩形的起始位置,点(X,Y)。

private Point initialMousePos;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    this.initialMousePos = e.Location;
}

然后,在 MouseUp event 中, 当用户停止拖动并释放鼠标按钮时引发,您想要保存鼠标光标的最终终点,并将其与初始起点组合以构建一个矩形。构建这样一个矩形的最简单方法是使用 FromLTRB Rectangle class 的静态方法:

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    // Save the final position of the mouse
    Point finalMousePos = e.Location;

    // Create the rectangle from the two points
    Rectangle drawnRect = Rectangle.FromLTRB(
                                             this.initialMousePos.X,
                                             this.initialMousePos.Y,
                                             finalMousePos.X,
                                             finalMousePos.Y);

    // Do whatever you want with the rectangle here
    // ...
}

您可能希望将它与 MouseMove 中的一些绘画代码结合起来事件处理程序方法,在用户绘制矩形时向用户提供他们正在绘制的矩形的视觉指示。

正确的方法是处理 MouseMove event获取鼠标的当前位置,然后调用 Invalidate 方法,这将引发 Paint event .您所有的绘画代码都应该在 Paint 事件处理程序中。这是一种比您在网络上找到的许多示例所采用的方法更好的方法,在网络上您会看到在 MouseMove 事件处理程序方法内部调用类似 CreateGraphics 的方法.尽可能避免这种情况。

示例实现:

private Point currentMousePos;

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    // Save the current position of the mouse
    currentMousePos = e.Location;

    // Force the picture box to be repainted
    pictureBox1.Invalidate();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    // Create a pen object that we'll use to draw
    // (change these parameters to make it any color and size you want)
    using (Pen p = new Pen(Color.Blue, 3.0F))
    {
        // Create a rectangle with the initial cursor location as the upper-left
        // point, and the current cursor location as the bottom-right point
        Rectangle currentRect = Rectangle.FromLTRB(
                                                   this.initialMousePos.X,
                                                   this.initialMousePos.Y,
                                                   currentMousePos.X,
                                                   currentMousePos.Y);

        // Draw the rectangle
        e.Graphics.DrawRectangle(p, currentRect);
    }
}

如果您以前从未做过任何类型的绘图,请注意有关此代码的几件事。第一件事是我包装了一个 Pen object 的创建。 (我们用它来做实际的绘图)在 using statement 中.每当您创建一个实现 IDisposable interface 的对象时,这都是很好的通用做法。 ,例如画笔和钢笔,以防止应用程序中的内存泄漏。

此外,PaintEventArgs 为我们提供了 Graphics class 的实例。 ,它封装了 .NET 应用程序中的所有基本绘图功能。您所要做的就是对该类实例调用 DrawRectangleDrawImage 等方法,将适当的对象作为参数传递,所有绘图都会自动为您完成。很简单,对吧?

最后,请注意,我们在这里创建矩形的操作与我们最终在 MouseUp 事件处理程序方法中所做的完全相同。这是有道理的,因为我们只需要用户创建矩形时的瞬时大小。

关于c# - 如何在 PictureBox 上捕获鼠标坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6087320/

相关文章:

c# - ReliabilityContract 和 IComparer(或其他注入(inject)代码)

c# - Azure Functions - Host.json 不适用于 Azure 应用程序设置

c# - 加入其余的字符串数组

c# - 如何将运费从购物车发送到paypal

c# - 如何从另一个表单的线程将文本粘贴到表单的 RichTextBox?

c# - UserControl 的 BeginUpdate() EndUpdate

C# 按钮仅在第二次单击时触发

c# - ASP.NET WebHook 和 Web 服务之间的区别?

C# - 将 UI 与业务代码分开

c# - 可编辑标签控件