Declare Function SetCapture Lib "user32.dll" (ByVal hWnd As Long) As Long
SetCapture captures the mouse for the specified window. When a window captures the mouse, it receives all mouse input messages, including those which would otherwise be sent to other windows. Capturing the mouse input allows a window to keep track of all mouse actions via mouse messages, but no other windows will receive the mouse input. When the capture is no longer needed, the mouse capture should be ended by calling ReleaseCapture.
If successful, the function returns a handle to the window that had previously captured the mouse, or zero if no window had captured the mouse. If an error occured, the function also returns 0.
None.
The following example assumes that there is a picture box control, named Picture1, on the form window Form1.
' This code is licensed according to the terms and conditions listed here.
' 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
Back to the Function list.
Back to the Reference section.
Last Modified: May 21, 2000
This page is copyright © 2000 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/s/setcapture.html