Declare Function SetCapture Lib "user32.dll" (ByVal hWnd As Long) As Long
В успешном случае функция возвращает дескриптор предыдущего окна, захватившего мышь или 0, если таких окон не было. В случае ошибки также возвращается 0
' The following example assumes that there is a picture box control, named Picture1, on the form window Form1.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function SetCapture Lib "user32.dll" (ByVal hWnd As Long) As Long
Public Declare Function ReleaseCapture Lib "user32.dll" () As Long
' Whenever the mouse moves, draw a line connecting the cursor's hot spot
' to the center of Picture1. Of course, the line will be clipped withing the boundaries
' of the picture box. To do this, Picture1 captures the mouse input when the form
' loads, and releases it when the user clicks the mouse. For simplicity, the
' picture box's methods are used for drawing the line instead of using the
' proper API functions.
Private Sub Form1_Load()
' Have Picture1 capture mouse input. Also make sure that
' Picture1's scale mode is set to "Pixel".
Dim retval As Long ' return value
retval = SetCapture(Picture1.hWnd)
Picture1.ScaleMode = 3
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
' Erase the previous line and draw a line connecting Picture1's center
' to the mouse cursor. The line will be clipped at the boundary of the picture box.
Static oldX As Long, oldY As Long ' the previous mouse coordinates
' Erase the old line by drawing over it in the background color.
Picture1.Line (Picture1.ScaleWidth / 2, Picture1.ScaleHeight / 2)-(oldX, oldY), _
Picture1.BackColor
' Now draw the new line.
Picture1.Line (Picture1.ScaleWidth / 2, Picture1.ScaleHeight / 2)-(X, Y)
' Save the mouse coordinates -- they'll be needed next time.
oldX = X: oldY = Y
End Sub
Private Sub Picture1_Click()
' When the mouse is clicked, release the mouse capture.
Dim retval As Long ' return value
retval = ReleaseCapture()
End Sub