Mouse.GetPosition(IInputElement) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the position of the mouse relative to a specified element.
public:
static System::Windows::Point GetPosition(System::Windows::IInputElement ^ relativeTo);
public static System.Windows.Point GetPosition (System.Windows.IInputElement relativeTo);
static member GetPosition : System.Windows.IInputElement -> System.Windows.Point
Public Shared Function GetPosition (relativeTo As IInputElement) As Point
Parameters
- relativeTo
- IInputElement
The coordinate space in which to calculate the position of the mouse.
Returns
The position of the mouse relative to the parameter relativeTo
.
Examples
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;
' 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
Remarks
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:
Call the GetPosition method of the DragEventArgs that is passed to the drag events (DragEnter, DragOver, DragLeave).
Call GetCursorPos, using P/Invoke.