GetCursorPos仅返回x值

时间:2012-07-02 03:38:44

标签: winapi vb6 cursor-position

我在MSDN forum上看到一个线程,其中存在32位与64位整数的问题。我不确定这是不是我的问题,但好像这段代码应该有用,所以我有点困惑。

我在Windows 7 64位的兼容模式(XP SP2)中运行VB6。

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

Timer1_Timer() ...

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

此消息框显示鼠标x坐标的正确值,但无论鼠标在屏幕上的哪个位置,它都会显示"0"的{​​{1}}。此外,y正在返回GetCursorPos()

2 个答案:

答案 0 :(得分:7)

在VB6中,整数数据类型是一个16位数字。你必须使用Long,因为这是一个32位的数字。

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

或使用:

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

答案 1 :(得分:4)

如果你在VB6中运行,你的POINTAPI声明需要使用Long来表示你的点声明:

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

至于返回1,这意味着你成功了:

返回值长 - NonZero成功,零失败。设置GetLastError

"来自Visual Basic程序员的Win32 API指南"