Поделиться через


GetPosition

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the x-coordinate and y-coordinate of the mouse pointer position.

retval = eventargs.GetPosition(null)
-or-
retval = eventargs.GetPosition(element)

Arguments

element

UIElement

Any UIElement-derived object that is contained by the Silverlight plug-in. To specify the object relative to the overall plug-in coordinate system, set the element parameter to null.

Return Value

Type: Point

A point that represents the current x-coordinate and y-coordinate of the mouse pointer position. If the element parameter is null, this coordinate is for the overall Silverlight plug-in area. If element is not null, this coordinate is relative to element.

Managed Equivalent

GetPosition

Remarks

The most common usage is to pass element as null, which returns a coordinate position of the mouse within the overall Silverlight plug-in area.

The element parameter can also be set to any UIElement-derived object that is contained by the Silverlight plug-in. The returned mouse pointer position is relative to element. Depending on the location of element relative to the object, the coordinate values of the mouse position may be negative values.

If you are handling the event from the root Canvas, passing the root Canvas as element or passing null are equivalent.

The UIElement that is passed as element must be connected to the current object hierarchy in such a way that successive resolution of parents is connected to the root element. Passing a UIElement that is disconnected will generate an error.

Because some mouse events bubble, the sender parameter of the event handler that also received the event data may or may not be suitable for use as a frame of reference for the element parameter of GetPosition. Make sure that your handler for a mouse event that attempts to use sender for a GetPosition call is being handled by the expected object and has not bubbled upwards (perhaps being handled by the root Canvas if you also have a handler there).

Silverlight can provide information about the mouse only while the mouse pointer is over the Silverlight content area . A captured mouse will continue to update its position even when the pointer is no longer over the area that raised the mouse event. However, the mouse will stop updating its position (and lose capture) if it exits the Silverlight area. The position reported by GetPosition is not the position of the client user's overall screen, or even of the hosting browser. Depending on browser capabilities, it may be possible to obtain a browser positional offset for the Silverlight content area from the HTML Document Object Model (DOM), and to add that offset to the x and y values from the Silverlight mouse position to get a browser-relative coordinate. This might be useful if you are working with HTML overlays.

For more information on basic concepts, see Mouse Support. Note that the Mouse Support topic is written primarily for users of the managed API, and may not have code examples or specific information that address the JavaScript API scenarios.

Example

The following JavaScript example shows how to retrieve the current mouse pointer position for the object that received the MouseMove event. In this case, the element parameter is set to null, which means that the returned Point value represents the mouse pointer position of the object relative to the upper-left corner of the plug-in area.

function onMouseMove(sender, mouseEventArgs)
{
    // Return a Point object representing the x- and y-coordinates of the current mouse position.
    var pt = mouseEventArgs.getPosition(null);

    // Display the current mouse position.
    sender.findName("Status").text = pt.x + " : " + pt.y;
}

The following illustration shows how the green Rectangle object uses the red Rectangle object as the value of element, in order to return mouse position values that are relative to the coordinate space of element. This means that clicking near the upper-left corner of the green Rectangle generates mouse position values that are negative with respect to the red Rectangle.

Relative coordinates

Get position of upper left-hand corner.

The following JavaScript example shows how to retrieve the current mouse pointer position for the object relative to element.

function onMouseLeftButtonUp(sender, mouseEventArgs)
{
    // Return a Point object representing the x- and y-coordinates of the current mouse position
    // relative to the reference object.
    var pt = mouseEventArgs.getPosition(sender.findName("referenceObject"));

    // Display the current mouse position.
    sender.findName("Status").text = pt.x + " : " + pt.y;
}

Applies To

MouseEventArgs