5 out of 10 rated this helpful Rate this topic

SetCapture function

Sets the mouse capture to the specified window belonging to the current thread.SetCapture captures mouse input either when the mouse is over the capturing window, or when the mouse button was pressed while the mouse was over the capturing window and the button is still down. Only one window at a time can capture the mouse.

If the mouse cursor is over a window created by another thread, the system will direct mouse input to the specified window only if a mouse button is down.

Syntax

HWND WINAPI SetCapture(
  __in  HWND hWnd
);

Parameters

hWnd [in]

Type: HWND

A handle to the window in the current thread that is to capture the mouse.

Return value

The return value is a handle to the window that had previously captured the mouse. If there is no such window, the return value is NULL.

Remarks

Only the foreground window can capture the mouse. When a background window attempts to do so, the window receives messages only for mouse events that occur when the cursor hot spot is within the visible portion of the window. Also, even if the foreground window has captured the mouse, the user can still click another window, bringing it to the foreground.

When the window no longer requires all mouse input, the thread that created the window should call the ReleaseCapture function to release the mouse.

This function cannot be used to capture mouse input meant for another process.

When the mouse is captured, menu hotkeys and other keyboard accelerators do not work.

Examples

For an example, see Drawing Lines with the Mouse.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

See also

Reference
GetCapture
ReleaseCapture
WM_CAPTURECHANGED
Conceptual
Mouse Input

 

 

Send comments about this topic to Microsoft

Build date: 11/16/2011

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
C# syntax
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern IntPtr SetCapture(IntPtr hwnd);
vb.net syntax
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function SetCapture(ByVal hwnd As IntPtr) As IntPtr
End Function
SetCapture and Non-Client Area events
When SetCapture is active, the docs should also mention that all Non-Client area interactions (title bar drag, minimize or close buttons, etc) will not be processed by Windows. In fact, no WM_NCHITTEST messages are generated at all while SetCapture is active.

I would recommend the use of SetCapture for full-screen DirectX applications to prevent 'click throughs' to things like taskbar notifications, but for Windowed apps it should not be the standard operating mode.