1 out of 5 rated this helpful - Rate this topic

Mouse.GetPosition Method

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

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)
'Declaration
Public Shared Function GetPosition ( _
	relativeTo As IInputElement _
) As Point

Parameters

relativeTo
Type: System.Windows.IInputElement

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

Return Value

Type: System.Windows.Point
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.

During drag-and-drop operations, the position of the mouse cannot be reliably determined through GetPosition. This is because control of the mouse (possibly including capture) is held by the originating element of the drag until the drop is completed, with much of the behavior controlled by underlying Win32 calls. Try the following approaches instead:

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.
				Dim position As Point = Mouse.GetPosition(displayArea)
				txtBoxMousePosition.Text = "X: " & position.X & vbLf & "Y: " & position.Y

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.