Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function PtInRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Boolean
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetCapture Lib "user32" () As Long
Private Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Private Type POINTAPI
        x As Long
        y As Long
End Type
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
        Dim pt     As POINTAPI
        Dim Rc     As RECT
        
        pt.x = Me.ScaleX(x, vbTwips, vbPixels)
        pt.y = Me.ScaleY(y, vbTwips, vbPixels)
        
        
        Call GetWindowRect(Me.Command1.hwnd, Rc)        If Not PtInRect(Rc, pt.x, pt.y) Then
            Me.Command1.Caption = "Error"
            ReleaseCapture
        Else
            Me.Command1.Caption = "OK"
            If GetCapture <> Me.Command1.hwnd Then SetCapture Me.Command1.hwnd
        End If
  
End Sub注:
当PtInRect声明为以下格式时总返回0
Private Declare Function PtInRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
当PtInRect声明为以下格式时总报Bad DLL calling convention错误
Private Declare Function PtInRect Lib "user32" (lpRect As RECT, pt As POINTAPI) As Long
请大家帮忙解释一下

解决方案 »

  1.   

    MouseMove 中 (x,y) 为鼠标在 Command1 内部的坐标,GetWindowRect() 返回窗口在屏幕上的坐标,当然不符合了。
    应当用 GetClientRect() 代替 GetWindowRect()。
      

  2.   

    还有 PtInRect() 的返回值:
    ·C 中的 BOOL 是 4 字节,VB 中不是用 Boolean 而是用 Long 对应。
    ·而且 C 中用 {0,1} 表示 {false,true} ,在 C 中取 Not 为 {1,0};但是到 VB 中取 Not 为 {-1,-2} 都为 True,所以对 API 的 BOOL 返回值 VB 中应该用是否为 0 进行判断,不能等同于 Boolean。