Windows API函数BOOL ClipCursor( CONST RECT *lpRect );
参数RECT为指向个矩形区域的指针,坐标为屏幕坐标,如果设置为0,可以在整个屏幕移动

解决方案 »

  1.   

    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End TypePrivate Declare Function ClipCursor& Lib "user32" (lpRect As RECT)
    Private Declare Function GetClipCursor& Lib "user32" (lprc As RECT)
    Private Declare Function GetWindowRect& Lib "user32" (ByVal hwnd As Long, lpRect As RECT)Private MyRect As RECT
    Private isClip As BooleanPrivate Sub Command1_Click()
        Dim dl&
        Dim WindowRect As RECT
        
        If isClip Then
            dl& = ClipCursor(MyRect)       '恢复原来的剪切区域
            Command1.Caption = "开始"
        Else
            dl& = GetClipCursor(MyRect)    '保存当前的指针剪切区域
            dl& = GetWindowRect(Picture1.hwnd, WindowRect)     '获取picture控件在屏目中的位置(RECT)
            dl& = ClipCursor(WindowRect)   '指定新的剪切
            Command1.Caption = "结束"
        End If
        isClip = Not isClip            '记载是否是剪切了
        Label1.Visible = Not Label1.Visible
    End SubPrivate Sub Form_Load()
        isClip = False                  '程序启动时尚未剪切
    End Sub
      

  2.   

    Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function GetClipCursor Lib "user32" (lprc As RECT) As Long
    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As LongPrivate Type RECT
        left As Long
        top As Long
        right As Long
        bottom As Long
    End Type'将鼠标限制在ctl区域内,改为control同样有效
    Sub ClipToControl(ctl As Form)
    Dim hwnd As Long
    Dim t As RECT
    hwnd = ctl.hwnd
    GetWindowRect hwnd, t
    SetCursorPos t.left + (t.right - t.left) / 2, t.top + (t.bottom - t.top) / 2
    ClipCursor t
    End Sub'解除对鼠标移动的限制
    Sub ClipToDesktop()
       Dim t As RECT
       GetWindowRect GetDesktopWindow(), t
       ClipCursor t
    End Sub