【问题标题】:getting proper cordinates when drawing rectangle绘制矩形时获取正确的坐标
【发布时间】:2014-05-10 00:28:32
【问题描述】:

我正在绘制一个矩形设置 x,y,width,height 但似乎矩形绘制在与设置不同的位置。烦人的问题是,在鼠标点击时,我得到光标位置并从中设置一个新的鼠标位置。然后,鼠标保持在预期的相同位置......但是,矩形不是从同一个开始!它被放置在离设置很远的另一个位置!

    pictureBoxImageViewer.Invalidate()

    Using g As Graphics = Graphics.FromImage(pictureBoxImageViewer.Image)

        Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
        myPen.DashStyle = Drawing2D.DashStyle.DashDot

        Dim testPoint As Point = New Point(Control.MousePosition)

        g.DrawRectangle(myPen, testPoint.X, testPoint.Y, 50, 50)
        'here rectangle is drawn quite far from testPoint  

        myPen.Dispose()

        Windows.Forms.Cursor.Position = New Point(Control.MousePosition)
        'here mouse has no position change like expected. Why rectangle not if is same point??

    End Using

    pictureBoxImageViewer.Refresh()

更新:使用 pointToClient 和 pointToScreen 测试了不同的方法,但不是解决方案......当我设置时,专注于鼠标

Windows.Forms.Cursor.Position = New Point(Control.MousePosition)

光标保持在同一位置。那么,pointToScreen 也应该这样做,对吧?而不是!光标被重新定位到另一个坐标。奇怪...

Windows.Forms.Cursor.Position = New Point(Me.PointToScreen(Control.MousePosition))

更新 2:我做了一个简单的练习来解释问题...

Private Sub PictureBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

        Dim testPoint As Point = New Point(Cursor.Position)

        Dim testPointScreen As Point = New Point(Me.PointToScreen(Cursor.Position))

        Dim testPointClient As Point = New Point(Me.PointToClient(Cursor.Position))

        MsgBox(testPoint.X.ToString + " " + testPoint.Y.ToString + " " + Control.MousePosition.X.ToString + " " + Control.MousePosition.Y.ToString)

        MsgBox(testPointScreen.X.ToString + " " + testPointScreen.Y.ToString + " " + testPointClient.X.ToString + " " + testPointClient.Y.ToString)

    End Sub

然后我从第一个 MsgBox 得到:“367 265 367 265” 第二个 MsgBox:“525 347 209 143”

所以,pointToScreen 或 pointToClient 应该与 Cursor.Position 相同,并返回不同的坐标。获得更多点,y 可以假设 pointToScreen (113 50)、pointToClient (-113 -50) 和期望点之间的偏移量始终相同。

【问题讨论】:

标签: vb.net


【解决方案1】:

在查看您的代码时,我发现您正在使用 Control.MousePosition 这将为您提供屏幕坐标中的鼠标位置

来自 MSDN 文档

MousePosition 属性返回一个 Point,表示引用该属性时鼠标光标的位置。坐标指示屏幕上的位置,而不是相对于控件,并且无论光标是否位于控件上都会返回。屏幕左上角坐标为0,0。

为了在正确的位置绘制矩形,您需要使用 PictureBox 的 Control.PointToClient 方法,因此您需要执行以下操作

Dim testPoint As Point = New Point(pictureBoxImageViewer.PointToClient(Control.MousePosition))
g.DrawRectangle(myPen, testPoint.X, testPoint.Y, 50, 50)

根据我在下面的评论,您正在使用表单的 PointToClient 方法,该方法仍然是错误的,因为它使用的是表单中的 x,y 坐标。 PointToClient 方法是控制类的一部分,因此 PictureBox 也有一个。那是您需要使用的。

基于 OP 更新的修改示例。

Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    Dim testPoint As Point = New Point(Cursor.Position)
    Dim testPointScreen As Point = New Point(Me.PointToScreen(Cursor.Position))
    'Note the fact that I am using the PictureBox's PointToClient Method since that is the
    'Control that you are wanting to draw in.
    Dim testPointClient As Point = New Point(PictureBox1.PointToClient(Cursor.Position))


    MsgBox(testPoint.X.ToString + " " + testPoint.Y.ToString + " " + Control.MousePosition.X.ToString + " " + Control.MousePosition.Y.ToString)

    MsgBox(testPointScreen.X.ToString + " " + testPointScreen.Y.ToString + " " + testPointClient.X.ToString + " " + testPointClient.Y.ToString)
End Sub

【讨论】:

  • 请发布您的整个方法。这段代码是否被定位,哪个事件处理程序?如果您在 drawrectangle 方法上放置断点,您应该会看到点值是正确的。
  • 所以我又做了一次更新。单击时我放置了一个图片框和捕获事件。然后,尝试获取正确的坐标。
  • 查看您的第二次更新,您正在使用表单的 PointToclient 方法,您的图片框也有一个,使用那个。如果您仔细查看我回答中的示例,那就是我所做的。
猜你喜欢
  • 2015-07-03
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多