This documentation is archived and is not being maintained.

Mouse.GetPosition Method

Gets the position of the mouse relative to a specified element.

Namespace: System.Windows.Input
Assembly: PresentationCore (in presentationcore.dll)

public static Point GetPosition (
	IInputElement relativeTo
)
public static Point GetPosition (
	IInputElement relativeTo
)
public static function GetPosition (
	relativeTo : IInputElement
) : Point
You cannot use methods in XAML.

Parameters

relativeTo

The coordinate space in which to calculate the position of the mouse.

Return Value

The position of the mouse relative to the parameter relativeTo.

The position of the mouse pointer is calculated relative to the specified element with the upper-left corner of element being the point of origin, 0,0.

The following example shows how to use GetPosition to determine the position of the mouse pointer. The position of the mouse pointer is stored in a Point structure. The X and Y values of the Point object are displayed in a TextBox.

// displayArea is a StackPanel and txtBoxMousePosition is
// a TextBox used to display the position of the mouse pointer.
Point position = Mouse.GetPosition(displayArea);
txtBoxMousePosition.Text = "X: " + position.X +
    "\n" +
    "Y: " + position.Y;

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Show: