2个回答
展开全部
一楼的方法执行后,鼠标移动到窗体边上,变成箭头形(即改变大小),一点,那个限制范围就失效了,算是个Bug吧。下面这个代码是经过修正的,防止这一点出现。关闭程序后鼠标恢复正常。
=================
Option Explicit
Private Const lBorder As Long = 4 '边框大小,经测试,至少为4才能不出现用户点击窗体边缘导致失效。
Dim R As RECT
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function ClipCursor Lib "user32" (lpRect As RECT) As Long
'lpRect-鼠标光标限制到的矩形
Private Declare Function ClipCursorByNum Lib "user32" Alias "ClipCursor" (lpRect As Long) As Long
'lpRect-传0,取消鼠标光标限制
Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Sub Form_Load() '窗体载入后,鼠标只能在窗体范围内移动
SetRect R, Left / Screen.TwipsPerPixelX + lBorder, Top / Screen.TwipsPerPixelY + lBorder, (Left + Width) / Screen.TwipsPerPixelX - lBorder, (Top + Height) / Screen.TwipsPerPixelY - lBorder
ClipCursor R
End Sub
Private Sub Form_Unload(Cancel As Integer) '窗体退出时,取消鼠标光标限制范围
ClipCursorByNum 0
End Sub
=================
Option Explicit
Private Const lBorder As Long = 4 '边框大小,经测试,至少为4才能不出现用户点击窗体边缘导致失效。
Dim R As RECT
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function ClipCursor Lib "user32" (lpRect As RECT) As Long
'lpRect-鼠标光标限制到的矩形
Private Declare Function ClipCursorByNum Lib "user32" Alias "ClipCursor" (lpRect As Long) As Long
'lpRect-传0,取消鼠标光标限制
Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Sub Form_Load() '窗体载入后,鼠标只能在窗体范围内移动
SetRect R, Left / Screen.TwipsPerPixelX + lBorder, Top / Screen.TwipsPerPixelY + lBorder, (Left + Width) / Screen.TwipsPerPixelX - lBorder, (Top + Height) / Screen.TwipsPerPixelY - lBorder
ClipCursor R
End Sub
Private Sub Form_Unload(Cancel As Integer) '窗体退出时,取消鼠标光标限制范围
ClipCursorByNum 0
End Sub
参考资料: 我的空间http://hi.baidu.com/arteriosclerosis/blog/item/857b87d53c1b5109a18bb77e.html
展开全部
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long '用来限定当前鼠标的活动范围
Private Sub Form_Load()
Dim rectRect As RECT
rectRect.Left = Me.Left / 15
rectRect.Top = (Me.Top + Me.Height - Me.ScaleHeight) / 15
rectRect.Right = (Me.Left + Me.Width) / 15
rectRect.Bottom = (Me.Top + Me.Height) / 15
ClipCursor rectRect
End Sub
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long '用来限定当前鼠标的活动范围
Private Sub Form_Load()
Dim rectRect As RECT
rectRect.Left = Me.Left / 15
rectRect.Top = (Me.Top + Me.Height - Me.ScaleHeight) / 15
rectRect.Right = (Me.Left + Me.Width) / 15
rectRect.Bottom = (Me.Top + Me.Height) / 15
ClipCursor rectRect
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询