Mouse.Capture Method

Definition

Captures mouse input to the specified element.

Overloads

Capture(IInputElement)

Captures mouse input to the specified element.

Capture(IInputElement, CaptureMode)

Captures mouse input to the specified element using the specified CaptureMode.

Remarks

When an element captures the mouse, it receives mouse input whether or not the cursor is within its borders.

If a CaptureMode is not specified, the default CaptureMode is Element.

To release mouse capture, call Capture passing null as the element to capture.

If the mouse is captured when a MouseDown or MouseUp event is raised and the input is not going to the element underneath the mouse, PreviewMouseDownOutsideCapturedElement and PreviewMouseUpOutsideCapturedElement are raised first. This enables the captured element a chance to release capture before the MouseDown and MouseUp events are routed.

Capture(IInputElement)

Captures mouse input to the specified element.

C#
public static bool Capture(System.Windows.IInputElement element);

Parameters

element
IInputElement

The element to capture the mouse.

Returns

true if the element was able to capture the mouse; otherwise, false.

Examples

The following example shows how to capture the mouse to a specific element by using the Capture method.

C#
private void OnCaptureMouseRequest(object sender, RoutedEventArgs e)
{
    Mouse.Capture(_elementToCapture);
}

Remarks

When an element captures the mouse, it receives mouse input whether or not the cursor is within its borders.

If a CaptureMode is not specified, the default CaptureMode is Element.

To release mouse capture, call Capture passing null as the element to capture.

If the mouse is captured when a MouseDown or MouseUp event is raised and the input is not going to the element underneath the mouse, PreviewMouseDownOutsideCapturedElement and PreviewMouseUpOutsideCapturedElement are raised first. This enables the captured element a chance to release capture before the MouseDown and MouseUp events are routed.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Capture(IInputElement, CaptureMode)

Captures mouse input to the specified element using the specified CaptureMode.

C#
public static bool Capture(System.Windows.IInputElement element, System.Windows.Input.CaptureMode captureMode);

Parameters

element
IInputElement

The element to capture the mouse.

captureMode
CaptureMode

The capture policy to use.

Returns

true if the element was able to capture the mouse; otherwise, false.

Examples

The following example shows how to capture the mouse to a specific element by using the Capture method.

C#
private void OnCaptureMouseRequest(object sender, RoutedEventArgs e)
{
    Mouse.Capture(_elementToCapture);
}

Remarks

When an element captures the mouse, it receives mouse input whether the cursor is within its borders.

To release mouse capture, call Capture passing null as the element to capture.

If the mouse is captured when a MouseDown or MouseUp event is raised and the input is not going to the element underneath the mouse, PreviewMouseDownOutsideCapturedElement and PreviewMouseUpOutsideCapturedElement are raised first. This enables the captured element a chance to release capture before the MouseDown and MouseUp events are routed.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9