Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting pointer X and Y positions 2

Status
Not open for further replies.

OrthoDocSoft

Programmer
May 7, 2004
291
US
Dear Folks,

Clapper62 provided a variation of the following in

thread222-1287592

Code:
Private Type PointAPI    
  X As Long    
  Y As Long

End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long

Private Sub Mouse_Position(Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim Pnt As PointAPI   ' Declare a variable to use UDT    

GetCursorPos Pnt      ' Call The API    

msgbox Pnt.X
msgbox Pnt.Y    

End Sub

Private Sub Form_Timer()

Call Mouse_Position(1, 0, 0, 0)    

End Sub

But when I use this, setting the timer speed to about 5 seconds, I only get the X position, while the Y position is always "0" no matter where the pointer is on the screen. Does anyone see the problem as to why I don't get a correct Y position?

Thanks,

Ortho


[lookaround] "you cain't fix 'stupid'...
 
I can't see anything wrong with your code.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Well, I can't see anything wrong with the API call. I can see plenty wrong with the design of the function (but nothing that would cause Y to be 0)
 
So this is a "ghost in the machine" kind of problem?....

Ortho

[lookaround] "you cain't fix 'stupid'...
 
I just put the code in a stand-alone app, even taking out the (what looked like to me) useless varibles passed to Sub Mouse_Position.

Same problem....

Ortho

[lookaround] "you cain't fix 'stupid'...
 
I've tried your code, even using a timer, and it correctly returns the X,Y of the mouse pointer on the screen.

Start afresh, stick a Timer on a form, set the Interval property to 100 and try this code (copy the GetCursorPos function and PointAPI declaration from your previous code):

Code:
Private Sub Timer1_Timer()

  Dim Pnt As PointAPI   ' Declare a variable to use UDT

  GetCursorPos Pnt      ' Call The API

  Caption = Pnt.X & "," & Pnt.Y

End Sub

As you move the mouse around, the caption of your application change to show the X,Y position whether it's over your VB form or not.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
AndyGroom,

did what you said, even retyped the function code (to make sure it wasn't a hidden corruption) and the same problem is there. The Y coordinate stays at "0".

I even disabled all other programs and antivirus to see if that would help. No avail.

Hmmmmmmmmm

Ghost in the machine.


Ortho

[lookaround] "you cain't fix 'stupid'...
 
OMG! I had the PointApi type like this:

Private Type PointApi

X as Integer
Y as Integer

End Type

X and Y are supposed to be "long". Problem solved. Sorry for wasting your time.

Ortho (living up to my tag line....)

[lookaround] "you cain't fix 'stupid'...
 
Should have gone to Specsavers...

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Ah - and now you know why it is generally best if you post the code that is causing the problem rather than ... well, something else, because your first post clearly shows PointAPI correctly declared (and was one of the first things that I checked - and thus discarded as a possible problem)
 
strongm

You know, you can fix bad knees with artificial ones, and you can fix bad hips with prosthetics, and you can repair bad hearing with cochlear implants and you can use lasic surgery to give yourself 20/20 vision, but you know what?
"You cain't fix 'stupid'..." Ron White

Ortho

[lookaround] "you cain't fix 'stupid'...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top