Getting Mouse Position over an Object
I am using VB.NET and I am currently having a problem accurately reading the mouse position. In my code, whenever the user moves the mouse, I want to have two variables store the mouse position's X and Y value, however when I move the mouse over an object such as a picturebox or a label, it will no longer give me the value until I move the pointer off the object onto the form. Are there any methods that will give me the absolute mouse coordinates regardless of whether the mouse is over an object or not?
Re: Getting Mouse Position over an Object
you could try this:
vb.net Code:
Dim mousePos As Point = MousePosition()
Dim mousex As Integer = mousePos.X
Dim mousey As Integer = mousePos.Y
Re: Getting Mouse Position over an Object
Nope, sorry still doesn't work. I've tried:
e.x ; e.y
Cursor.Position.X - (Me.Location.X + 28) ; Cursor.Position.Y - (Me.Location.Y + 28)
and something else with PointToClient
Re: Getting Mouse Position over an Object
That makes it sound like you are getting something, just not what you want. I haven't specifically looked at this, but I would expect that you are getting the position in screen coordinates. Does that sound right? What happened when you used PointToClient from the form?
Re: Getting Mouse Position over an Object
In every case I got the same result. The program correctly returns the mouse coordinates UNLESS the pointer is hovering over an object, such as a button, label, picturebox, etc.
Re: Getting Mouse Position over an Object
It sounds like you're handling the MouseMove event of the form but not of the controls on the form. If you want to be able to track the mouse pointer over any control then you must handle the appropriate event of every control. You can use a single method and just add every control and the form to the Handles clause, which you can do in the Properties window.
Re: Getting Mouse Position over an Object
Thank you. I have almost solved my problem now. I know have the mouse moving correctly over the picture boxes, however I am still encountering one problem. Since I have initialized an array of pictureboxes at runtime, how would I go about adding those to the Handle clause? It does not simply let me add Grid().MouseMove, or anything like that.
Re: Getting Mouse Position over an Object
When you add items to the list, you will have to add the handler directly with:
AddHandler <event you want to handle>,AddressOf <Sub that handles the event>
Re: Getting Mouse Position over an Object
Just be sure that, for every AddHandler, there's a corresponding RemoveHandler when you're done with the object.
Re: Getting Mouse Position over an Object
All right then, one more thing and then I'll be all good. Now it successfully detects mouse movement over all parts of the image, however, when it returns the mouse coordinates, it does it relative to whatever object the mouse is hovering over. Is there anyway for it to consistently return the mouse coordinates relative to the frame all the time?
edit: Shell yeah, never mind I gots this one, I just replaced that e.X thing I had with the other thing involving Cursor.Position.X that I had earlier, awesum then, thanks SHOO MUCHHH guys :DDDD