ReleaseCapture Function

Declare Function ReleaseCapture Lib "user32.dll" () As Long

Platforms

Description & Usage

ReleaseCapture ends whatever mouse capture may be in effect, regardless of which window actually captured the mouse. Once ReleaseCapture is called, mouse messages are immediately routed back to the windows that would normally receive them. This function should be used as soon as a window no longer needs to capture the mouse after it had called SetCapture.

Return Value

If successful, the function returns a non-zero value. If an error occured, the function returns 0 (use GetLastError to get the error code).

Visual Basic-Specific Issues

None.

Parameters

None.

Example

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

See Also

GetCapture, SetCapture

Category

Mouse

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/r/releasecapture.html