限制鼠标在窗体内(不包括标题栏)请问如何才能做到呢?参考网上的代码都是限制在自己的窗体内,包括了标题栏.....

解决方案 »

  1.   

    怎样限制鼠标移动 操作步骤
    1、建立一个新工程项目,缺省建立窗体FORM1
    2、添加一个新模体
    3、粘贴下面代码到新模体Option ExplicitDeclare Function ClipCursor Lib "user32" (lpRect As Any) As Long
    Declare Function ClipCursorClear Lib "user32" Alias "ClipCursor" (ByVal lpRect As Long) As Long
    Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    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 Type
    Public RetValue As Long
    Public ClipMode As Boolean
    Public Sub SetCursor(ClipObject As Object, Setting As Boolean)
    ' used to clip the cursor into the viewport and
    ' turn off the default windows cursor
    Dim CurrentPoint As POINTAPI
    Dim ClipRect As RECT
    If Setting = False Then
    ' set clip state back to normal
    RetValue = ClipCursorClear(0)
    Exit Sub
    End If
    ' set current position
    With CurrentPoint
    .X = 0
    .Y = 0
    End With
    ' find position on the screen (not the window)
    RetValue = ClientToScreen(ClipObject.hwnd, CurrentPoint)
    ' designate clip area
    With ClipRect
    .Top = CurrentPoint.Y
    .Left = CurrentPoint.X
    .Right = .Left + ClipObject.ScaleWidth
    .Bottom = .Top + ClipObject.ScaleHeight
    End With ' clip it
    RetValue = ClipCursor(ClipRect)
    End Sub
    4、添加一个图片框控件(PICTURE1)到窗体(FORM1)
    5、设置PICTURE1的尺寸和FORM1的一样大
    6、在PICTURE1的CLICK事件中添加以下代码:
    Private Sub Picture1_Click()
    ClipMode = Not ClipMode
    SetCursor Picture1, ClipMode
    End Sub
    7、保存工程项目
    8、运行程序。
      

  2.   

    Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long 
    Declare Function ClipCursorClear Lib "user32" Alias "ClipCursor" (ByVal lpRect As Long) As Long 
    Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long 
    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 Type 
    Public RetValue As Long 
    Public ClipMode As Boolean 
    Public Sub SetCursor(ClipObject As Object, Setting As Boolean) 
    ' used to clip the cursor into the viewport and 
    ' turn off the default windows cursor 
    Dim CurrentPoint As POINTAPI 
    Dim ClipRect As RECT 
    If Setting = False Then 
    ' set clip state back to normal 
    RetValue = ClipCursorClear(0) 
    Exit Sub 
    End If 
      

  3.   

    我是参照巫师的代码修改的(这个代码当然不够完善),你具体应用时,可以再修改一下。
    感谢巫师提供代码~~~~~~~  ' 新建一个标准EXE工程
    ' 在窗口内添加一个 PictureBox 控件
    ' 调整大小使它刚好充满整个客户区
    Option ExplicitPrivate Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
    Private Declare Function ClipCursorClear Lib "user32" Alias "ClipCursor" (ByVal lpRect As Long) As Long
    Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) 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 TypePublic Sub SetCursor(ClipObject As Object, SetClip As Boolean)    Dim stcCliLT As POINTAPI
        Dim stcCliRB As POINTAPI
        Dim ClipRect As RECT
        
        If (Not SetClip) Then Call ClipCursorClear(0): Exit Sub     '清除剪切矩形    stcCliLT.X = 0
        stcCliLT.Y = 0
        stcCliRB.X = ClipObject.Width \ Screen.TwipsPerPixelX
        stcCliRB.Y = ClipObject.Height \ Screen.TwipsPerPixelY
        Call ClientToScreen(hWnd, stcCliLT) '左上角转换成屏幕坐标
        Call ClientToScreen(hWnd, stcCliRB) '右下角转换成屏幕坐标
        
        With ClipRect       '设置剪切矩形
            .Top = stcCliLT.Y
            .Left = stcCliLT.X
            .Right = stcCliRB.X
            .Bottom = stcCliRB.Y
        End With
        Call ClipCursor(ClipRect)   '设置剪切矩形End SubPrivate Sub Form_Load()
        '载入窗体后限制鼠标移动区域
        Call SetCursor(Picture1, True)End SubPrivate Sub Form_Unload(Cancel As Integer)
        '卸载窗体时解除限制
        Call SetCursor(Picture1, False)
    End SubPrivate Sub Picture1_Click()
        ' 左键单击结束程序
        Unload MeEnd Sub
      

  4.   

    Public Sub SetCursor()
    改成:
    Private Sub SetCursor()当然,不改也可以运行.......  Public Sub SetCursor(
      

  5.   

    我试过,还是能起到作用。
    只不过,我用一个简单的操作就可以解除限制了 
      在你那儿,鼠标的可移动区域有没有变化呀?
      你要注意的是: PictureBox1 的区域就是鼠标可移动区域,你要把它的左上角移到客户区左上角,大小比窗口小一点点.........  ^_^
      

  6.   

    我把picture1,改成了form1,确实是不包括标题栏了
    但是,鼠标却不是限制在窗体内啊...虽然鼠标的范围是窗体那么大....但是鼠标可以出窗体下面....
      

  7.   

    你不按要求做,当然不能达到效果了。
    在窗口内添加一个 PictureBox 控件
    调整大小使它刚好充满整个客户区
    在这段代码中,它是非常重要的操作!
    要不然,你就得用其它的方法计算出窗口客户区的屏幕位置。
    要想在不同的系统下都能正确得到客户区的大小和位置,至少要用 API 获取标题栏高度、边框大小。
    然后还有其它的转换运算过程。
      

  8.   

    你的其它控件可以放到这个 PicturBox 中。  如果你的程序界面已经设计好了,觉得重新调整到 PictureBox 中比较麻烦,可以在最后才加入一个图片框,把它按要求进行大小和位置调整。然后把它的 .Visible 属性设置成 False ,或者在窗体的 Load() 事件中用代码把它设置成 False 。当然我给你写的代码中的 Picture1 也要换成相应的控件名称才行。  还有一个要注意的问题是:你要在窗口内提供结束程序的途径(比如退出按钮)。否则别人只有按 Alt+F4 了。