I am drawing a rectangle setting x,y,width,height but seems that rectangle is drawn at different position than set. Annoying issue is that, on mouse click, I get cursor position and set a new mouse position from that. Then, mouse remains at same position like expected... However, rectangle doesn't start at same one! It is placed at another position quite far from 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()
UPDATE: tested different methods using pointToClient and pointToScreen but not a solution... Focused on mouse, when I set
Windows.Forms.Cursor.Position = New Point(Control.MousePosition)
cursor remains at same position. Then, pointToScreen should do the same, right? and is not! cursor is been repostioned at another coordinates. Weird...
Windows.Forms.Cursor.Position = New Point(Me.PointToScreen(Control.MousePosition))
UPDATE 2: I made a simple exercise to explain the problem...
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
then I'd got from first MsgBox: "367 265 367 265" second MsgBox: "525 347 209 143"
so, pointToScreen or pointToClient should be the same as Cursor.Position and returns different coordinates. Getting more points, y can assume that offset is always the same between pointToScreen (113 50), pointToClient (-113 -50) and expected point.