Option Explicit
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim MouseOver As Boolean
MouseOver = (0 <= X) And (X <= Command1.Width) And (0 <= Y) And (Y <= Command1.Height)
If MouseOver Then
SetCapture Command1.hWnd
'command1.BackColor = RGB(141, 150, 206)
Command1.Caption = "鼠标移进"
Else
'command1.BackColor = &H80000004
Command1.Caption = "鼠标移出"
ReleaseCapture
End If
End Sub
自己写的,不是从那抄的。很简单。有事再问:)
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim MouseOver As Boolean
MouseOver = (0 <= X) And (X <= Command1.Width) And (0 <= Y) And (Y <= Command1.Height)
If MouseOver Then
SetCapture Command1.hWnd
'command1.BackColor = RGB(141, 150, 206)
Command1.Caption = "鼠标移进"
Else
'command1.BackColor = &H80000004
Command1.Caption = "鼠标移出"
ReleaseCapture
End If
End Sub
自己写的,不是从那抄的。很简单。有事再问:)
Declare Function SetCapture Lib "user32" Alias "SetCapture" (ByVal hwnd As Long) As Long
说明
将鼠标捕获设置到指定的窗口。在鼠标按钮按下的时候,这个窗口会为当前应用程序或整个系统接收所有鼠标输入
返回值
Long,之前拥有鼠标捕获的窗口的句柄
参数表
参数 类型及说明
hwnd Long,要接收所有鼠标输入的窗口的句柄
注解
我的理解:与ReleaseCapture函数一起使用,用于判断鼠标离开(mouseleave)事件
ReleaseCapture VB声明
Declare Function ReleaseCapture Lib "user32" Alias "ReleaseCapture" () As Long
说明
为当前的应用程序释放鼠标捕获
返回值
Long,TRUE(非零)表示成功,零表示失败
注解
我的理解:与SetCapture函数一起使用,用于判断鼠标离开(mouseleave)事件
移出(就移到form上了)可以用 form_mousemove
比较简单 我一般是这样做的
'抄过来的一个例子 判断 移进 移出 Picture1 的
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As LongPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim MouseEnter As Boolean
MouseEnter = (0 <= X) And (X <= Picture1.Width) And (0 <= Y) And (Y <= Picture1.Height)
If MouseEnter Then
'
SetCapture Picture1.hwnd
Text1.Text = "Mouse in picuture!"
Else
Text1.Text = "Mouse out!"
ReleaseCapture
End If
End Sub
sub command_mousemove
command.caption="标题二"
end sub
sub form_mouse
command.caption="标题一"
end sub
把以下代码添加如模块:
Option Explicit Type RECTLeft As LongTop As LongRight As LongBottom As LongEnd Type Declare Function ClipCursor Lib "user32" _(lpRect As Any) As LongPublic Sub DisableTrap(CurForm As Form)Dim erg As Long'声明过程变量'设置新坐标Dim NewRect As RECTCurForm.Caption = "释放鼠标"With NewRect.Left = 0&.Top = 0&.Right = Screen.Width / Screen.TwipsPerPixelX.Bottom = Screen.Height / Screen.TwipsPerPixelYEnd Witherg& = ClipCursor(NewRect)End Sub Public Sub EnableTrap(CurForm As Form)Dim x As Long, y As Long, erg As Long'声明过程变量'设置新坐标Dim NewRect As RECT'得到TwipsperPixel'窗体的ScaleMode必须设为Twips!!!x& = Screen.TwipsPerPixelXy& = Screen.TwipsPerPixelYCurForm.Caption = "捕捉鼠标"'设置光标的范围With NewRect.Left = CurForm.Left / x&.Top = CurForm.Top / y&.Right = .Left + CurForm.Width / x&.Bottom = .Top + CurForm.Height / y&End Witherg& = ClipCursor(NewRect)End Sub 2、在窗体上添加两个命令按钮(Command Button)。3、把以下代码添加如Form1。Private Sub Command1_Click()EnableTrap Form1End Sub Private Sub Command2_Click()DisableTrap Form1End Sub Private Sub Form_Unload(Cancel As Integer)'程序结束时释放鼠标。DisableTrap Form1End Sub
有用。例如在一个射击游戏中,需要限制鼠标在射击区内移动。
操作步骤
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 BooleanPublic Sub SetCursor(ClipObject As Object, Setting As Boolean)
' used to clip the cursor into the viewport and
' turn off the default windows cursorDim CurrentPoint As POINTAPI
Dim ClipRect As RECTIf 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 Sub4、添加一个图片框控件(PICTURE1)到窗体(FORM1)
5、设置PICTURE1的尺寸和FORM1的一样大
6、在PICTURE1的CLICK事件中添加以下代码:Private Sub Picture1_Click()
ClipMode = Not ClipMode
SetCursor Picture1, ClipMode
End Sub7、保存工程项目
8、运行程序。在图片框单击鼠标,鼠标将被包含在图片框控件的区域内。要释放限制状态只需再次单击鼠标。
注意:如果释放限制状态失败,鼠标将被永久限制,只能用重新启动机器来解决。
另一个限制鼠标活动范围的方法是关闭鼠标,用其他图象代替光标,例如手枪。