Question

I saw a thread on an MSDN forum where there was an issue with 32-bit vs. 64-bit integers. I'm not sure if that is my issue, but it seems as though this code should work, so I'm a bit confused.

I'm running VB6 in compatiblity mode (XP SP2) in Windows 7 64-bit.

Type POINTAPI ' This holds the logical cursor information
    x As Integer
    y As Integer
End Type

Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

In Timer1_Timer()...

Dim mousePos As POINTAPI
Call GetCursorPos(mousePos)
MsgBox mousePos.x & " " & mousePos.y

This message box shows the correct value for the x coordinate of the mouse, but it shows "0" for y, no matter where the mouse is on the screen. Also, GetCursorPos() is returning 1.

Était-ce utile?

La solution

In VB6 the Integer data type is a 16-bit number. You have to use Long as this is a 32-bit number.

Type POINTAPI ' This holds the logical cursor information
  x As Long
  y As Long
End Type

Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

or use:

Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINT_TYPE) As Long 

Autres conseils

If your are running in VB6 your POINTAPI declaration needs to use a Long for your point declaration:

Type POINTAPI ' This holds the logical cursor information 
    x As Long
    y As Long 
End Type 

As far as returning a 1, that means you were successful:

Return Value Long -- NonZero on success, zero on failure. Sets GetLastError

"From Visual Basic Programmer's Guide to the Win32 API"

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top