Cursor Positon

Rogy

Member
Joined
Nov 4, 2010
Messages
6
Programming Experience
Beginner
Hello, I'm having a diffcult time getting cursor's position within the window

I tried using : Cursor.Position.X and Cursor.Position.Y but it returns the position of the cursor on the screen and not in the window...
 
Ok, when I try to use Me.PointToClient I get the following error:
VB.NET:
Error	1	Argument not specified for parameter 'p' of 'Public Function PointToClient(p As System.Drawing.Point) As System.Drawing.Point'.

I tried Imports System.Drawing.Point but still get the same error
 
You have to supply the p Point parameter to the method, that is your screen point that you got from Cursors Position property.
 
How do I do that?

Sorry for all the questions, I'm completely new to this

Do you perhaps have some code I can look at so I can get an idea on how it should "look" like?
 
Even thou JohnH answer is the correct way of doing it, and I'm sure that it gives more options...
You could have solved your problem LONG time ago if you simply subtract the cursor x and y from the form x and y.
 
You could have solved your problem LONG time ago if you simply subtract the cursor x and y from the form x and y.
No, that is not correct, try it and you will see why. (for example compare with the client coordinate provided by MouseMove event)
and I'm sure that it gives more options...
PointToClient is a single purpose method and it has no options, but since it is a Control instance method it is quite flexible to what context it can be used. For example, what if you want the client coordinate for a picturebox in a panel in a form (and only has the screen coordinate) ? Simply call pb.PointToClient.
 
No, that is not correct, try it and you will see why. (for example compare with the client coordinate provided by MouseMove event)

Assuming you grab the mouse position into mX and mY and the Form position into fX and fY, the metacode is:
VB.NET:
X = mX - fX
Y = mY - fY

If (X < 0) OR _
   (Y < 0) OR _
   (X > Form.Size.Width) OR _
   (Y > Form.Size.Height) Then
           Debug.Print("Mouse is outside form")
Else
           Debug.Print("Mouse position is " & X & " pixels horizontal and " & Y & " pixels vertical from the window")
End If

As you said, PointToClient is more flexible (I used the wrong word when I said it got more options), and for sure it's easier to use, specially for non-engineers; but screen position is just a simple Cartesian axis and simple maths works.
 
I repeat, subtracting "the cursor x and y from the form x and y" does not give the correct client point in form. Hook up one of the mouse event handlers for the form and compare with the client point that is provided there by MouseEventArgs.
 
Back
Top