下面这段代码放到一个窗体上,可以实现你说的效果,但不理想,因为热键会影响. 你看看吧.Option ExplicitPrivate Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function ReleaseCapture Lib "user32" () As Long Private Declare Function GetCapture Lib "user32" () As Long Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPrivate Type POINTAPI x As Long y As Long End Type Private blnMouseIsIn As BooleanPrivate Sub cursorManager(x As Single, y As Single) Dim pointXY As POINTAPI Dim hWnd As Long Call GetCursorPos(pointXY) hWnd = WindowFromPoint(pointXY.x, pointXY.y) If (x > 0) And (x < Me.Width) And (y > 0) And (y < Me.Height) And (Me.hWnd = hWnd) Then If Not blnMouseIsIn Then blnMouseIsIn = True Debug.Print "In" End If If GetCapture <> Me.hWnd Then SetCapture Me.hWnd End If Else If blnMouseIsIn Then Debug.Print "out" ReleaseCapture End If blnMouseIsIn = False End If End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) Call cursorManager(x, y) End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) Call cursorManager(x, y) End Sub Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) Call cursorManager(x, y) End Sub
你看看吧.Option ExplicitPrivate Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function GetCapture Lib "user32" () As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPrivate Type POINTAPI
x As Long
y As Long
End Type
Private blnMouseIsIn As BooleanPrivate Sub cursorManager(x As Single, y As Single)
Dim pointXY As POINTAPI
Dim hWnd As Long
Call GetCursorPos(pointXY)
hWnd = WindowFromPoint(pointXY.x, pointXY.y)
If (x > 0) And (x < Me.Width) And (y > 0) And (y < Me.Height) And (Me.hWnd = hWnd) Then
If Not blnMouseIsIn Then
blnMouseIsIn = True
Debug.Print "In"
End If
If GetCapture <> Me.hWnd Then
SetCapture Me.hWnd
End If
Else
If blnMouseIsIn Then
Debug.Print "out"
ReleaseCapture
End If
blnMouseIsIn = False
End If
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Call cursorManager(x, y)
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Call cursorManager(x, y)
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Call cursorManager(x, y)
End Sub