鼠标在控件上右键,鼠标光标移动到该控件右下角,但Label1控件时  Call GetWindowRect(objControl.hwnd, rect5)这句错误。如何解决?代码如下,贴上即可运行:
Form文件:Private Sub Check1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Check1
End SubPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Command1
End SubPrivate Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Label1
End SubPrivate Sub Option1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Option1
End SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Picture1
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Text1
End Sub
Module1文件:Type RECT
        Left As Long
        ToP As Long
        Right As Long
        Bottom As Long
End Type
Type POINTAPI
        X As Long
        Y As Long
End TypeDeclare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPublic Sub MoveCursor(FromP As POINTAPI, ToP As POINTAPI) '移动Mouse
    Dim stepx As Long, stepy As Long, k As Long
    Dim i As Long, j As Long, sDelay As Long
    stepx = 1
    stepy = 1
    i = (ToP.X - FromP.X)
    If i < 0 Then stepx = -1
    i = (ToP.Y - FromP.Y)
    If i < 0 Then stepy = -1
    For i = FromP.X To ToP.X Step stepx
        Call SetCursorPos(i, FromP.Y)
    Next i
    For i = FromP.Y To ToP.Y Step stepy
        Call SetCursorPos(ToP.X, i)
    Next i
End Sub
Public Sub moveMouseOn_Control(ByVal objControl As VB.Control)
    If objControl Is Nothing Then Exit Sub
    Dim rect5 As RECT
    Dim p1 As POINTAPI, p2 As POINTAPI
    Call GetWindowRect(objControl.hwnd, rect5) '注释:取得objControl相对Screen的座标
    p1.X = (rect5.Left + rect5.Right) \ 2
    p1.Y = (rect5.ToP + rect5.Bottom) \ 2
    Call GetWindowRect(objControl.hwnd, rect5)
    p2.X = rect5.Right
    p2.Y = rect5.Bottom
    Call MoveCursor(p1, p2)
End Sub

解决方案 »

  1.   

    Label1控件是没有hwnd属性的.所以错了.
      

  2.   

    GetWindowRect这类函数不能用来对轻量控件进行操作,因为label、image等这些轻量控件没有窗口句柄(hwnd),当然会错误!
      

  3.   

    Label1 没有 hwnd 属性 建议使用 TextBox 代替
      

  4.   

    可以采取“曲线救国”的战略, 为了减少代码,请将窗体的ScaleMode 属性设为 vbPixels,否则你可以在代码中进行变换。在你的代码中加入下面的API:
    Public Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long把你的 moveMouseOn_Control 改为下面的就行了:
    Public Sub moveMouseOn_Control(ByVal objControl As VB.Control)
        Dim p As POINTAPI
        
        ClientToScreen objControl.Parent.hwnd, p
        
        p.X = p.X + objControl.Left + objControl.Width
        p.Y = p.Y + objControl.Top + objControl.Height
            
        SetCursorPos p.X, p.Y
    End Sub
    ^_^ 是不是简单多了?