Click to Rate and Give Feedback
MSDN
MSDN Library
User Interface
User Input
Mouse Input
Functions
 SetCapture Function
SetCapture Function

The 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 SetCapture(      
    HWND hWnd );

Parameters

hWnd
[in] 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.

Windows 95/98/Me: Calling SetCapture causes the window that is losing the mouse capture to receive a WM_CAPTURECHANGED message.

Example

For an example, see Drawing Lines with the Mouse.

Function Information

Minimum DLL Versionuser32.dll
HeaderDeclared in Winuser.h, include Windows.h
Import libraryUser32.lib
Minimum operating systems Windows 95, Windows NT 3.1
UnicodeImplemented as Unicode version.

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
SetCapture and Non-Client Area events      battlegoat   |   Edit   |   Show History
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.

vb.net syntax      dmex   |   Edit   |   Show History
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function SetCapture(ByVal hwnd As IntPtr) As IntPtr
End Function
Flag as ContentBug
C# syntax      dmex   |   Edit   |   Show History
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern IntPtr SetCapture(IntPtr hwnd);
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker