绘制矩形时获取正确的坐标

时间:2014-05-10 00:28:32

标签: vb.net

我正在绘制一个设置x,y,宽度,高度的矩形,但似乎矩形是在不同于set的位置绘制的。恼人的问题是,在鼠标点击时,我获得光标位置并从中设置新的鼠标位置。然后,鼠标保持在预期的相同位置......但是,矩形不会在同一个位置开始!它被放置在离集合很远的另一个位置!

    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)和预期点之间的偏移量始终相同。

1 个答案:

答案 0 :(得分: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方法是Control Class的一部分,因此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
相关问题