【发布时间】: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) 和期望点之间的偏移量始终相同。
【问题讨论】:
-
查看PointToClient 和PointToScreen 方法。不确定这是否是您的问题,但请尝试一下
标签: vb.net