Enables mouse capture for the object.
| XAML | Cannot use methods in XAML. |
| Scripting | retval = object.CaptureMouse()
|
Return Value
Boolean
Returns true if the object has enabled mouse capture; otherwise, returns false.
Examples
To understand how mouse capture works, consider the following XAML example in which two Rectangle objects share the same MouseMove event handler function:
| XAML |
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="180" Width="300"
Background="PowderBlue">
<Rectangle
x:Name="Green"
MouseMove="onMouseMove"
MouseLeftButtonDown="onLeftButtonDown"
MouseLeftButtonUp="onLeftButtonUp"
Width="100" Height="100"
Fill="Green" />
<!-- The MouseMove event handler function is shared with the first rectangle -->
<Rectangle
x:Name="Red"
MouseMove="onMouseMove"
Canvas.Left="120"
Width="100" Height="100"
Fill="Maroon" />
<TextBlock
x:Name="statusTextBlock"
FontSize="18"
Canvas.Top="120" />
</Canvas>
|
The corresponding event handler functions for the previous XAML content example are defined in the following JavaScript example. Notice that the green Rectangle object captures all Silverlight application mouse input from the time the left mouse button is depressed until the left mouse button is released.
| JavaScript |
// Display the current mouse position for the Rectangle object.
function onMouseMove(sender, mouseEventArgs)
{
var msg = "x:y = " + mouseEventArgs.getPosition(null).x + ", " + mouseEventArgs.getPosition(null).y;
sender.findName("statusTextBlock").Text = sender.Name + ": " + msg;
}
// Enable mouse capture when the mouse is depressed over the green Rectangle object.
function onLeftButtonDown(greenRect, mouseEventArgs)
{
greenRect.captureMouse();
}
// Disable mouse capture when the mouse is released over the green Rectangle object.
function onLeftButtonUp(greenRect, mouseEventArgs)
{
greenRect.releaseMouseCapture();
}
|
When the Silverlight is displayed and the mouse moves over the rectangles, each rectangle displays its mouse position by using the shared onMouseMove event handler function. For example, when the mouse is over the red rectangle, the status displays "Red: x:y = 171, 55".
Each rectangle receives mouse input
When the mouse is depressed over the green rectangle and held down, moving the mouse over any part of the Silverlight application results in the green rectangle receiving all mouse input events. For example, when the green rectangle enables mouse capture and the mouse moves over the red rectangle, "Green: x:y = 140, 76". This is because all events are being redirected to the green rectangle even though the mouse is outside the green rectangle's bounding area.
Input goes to element with capture
Applies To
Canvas,
Ellipse,
Glyphs,
Image,
InkPresenter,
Line,
MediaElement,
Path,
Polygon,
Polyline,
Rectangle,
TextBlock
See Also
Silverlight Events
Silverlight Mouse Support
MouseLeftButtonDown
MouseLeftButtonUp
MouseMove
ReleaseMouseCapture