Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

GetPosition Method

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

XAML
Cannot use methods in XAML.
Scripting
retval = eventargs.GetPosition(element)

Parameters

element UIElement

Any UIElement-derived object that is contained by the Silverlight plug-in. The returned mouse pointer position is relative to element. To specify the object relative to the plug-in, set the element parameter to null.

Return Value

Point

A point that represents the current x- and y-coordinates of the mouse pointer position relative to element.

Remarks

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.

Examples

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 the returned Point value represents the mouse pointer position of the object relative to the upper-left corner of the plug-in area.

JavaScript
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

Relative coordinates

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

JavaScript
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

See Also

Silverlight Mouse Support
MouseEnter
MouseLeftButtonDown
MouseLeftButtonUp
MouseMove

Community Additions

Show:
© 2015 Microsoft