#729 – Mouse.GetPosition Doesn’t Work While Dragging

If you are handling the DragOver event during a drag-and-drop operation and you want to find the current mouse position, you need to use DragEventArgs.GetPosition, rather than the static Mouse.GetPosition method.

In the example below, we initiate a drag-and-drop operation in a window and then try reporting the mouse’s position in the window’s DragOver handler.  We try using both methods to get the mouse position, but only the DragEventArgs.GetPosition method works.

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Application 2" Height="350" Width="325"
        MouseDown="Window_MouseDown"
        AllowDrop="True" DragOver="Window_DragOver">
    <StackPanel>
        <Label Name="lblInfo1" Content="Info 1"/>
        <Label Name="lblInfo2" Content="Info 2"/>
    </StackPanel>
</Window>

 

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DragDrop.DoDragDrop((DependencyObject)e.Source, "Sample", DragDropEffects.Copy);
        }

        private void Window_DragOver(object sender, DragEventArgs e)
        {
            System.Windows.Point p1 = Mouse.GetPosition(this);
            lblInfo1.Content = string.Format("Mouse.GetPosition: {0}, {1}", p1.X, p1.Y);

            System.Windows.Point p2 = e.GetPosition(this);
            lblInfo2.Content = string.Format("DragEventArgs.GetPosition: {0}, {1}", p2.X, p2.Y);
        }

729-001

Advertisement

#672 – Mouse Coordinates Are in Device Independent Units

As with other screen position values in WPF, the Mouse.GetPosition function returns coordinates that are in device independent units, rather than in pixels.

1 device independet unit = 1/96 in.  This means that on a 96 dpi device, the value will actually map to pixels, but on other devices, the value will not be the same as the # pixels.

 

 

#671 – Mouse.GetPosition Only Works When Mouse Is in Window

You typically only use the Mouse.GetPosition method when the mouse pointer is within the boundaries of your application’s main window.  When the mouse is located outside of the window, calling GetPosition with a null value will return 0 values for the mouses X and Y position, regardless of where the mouse is located on the screen.

In the example below, we are updating a label to dump out the value returned by Mouse.GetPosition when a timer fires, ever second.

 

#670 – Getting the Mouse Position Relative to a Specific Element

When you use the Mouse.GetPosition method or MouseButtonEventsArgs.GetPosition method to get the mouse position, you specify that you want the mouse position relative to a particular element.

If you pass a null value into the GetPosition method, you indicate that you want the mouse position relative to the containing window.    However, you can also pass a reference to a control that implements the IInputElement interface.  Since UIElement implements IInputElement, you can pass in a reference to any user interface element.

In the example below, on mouse movement, we display the coordinates of the mouse position relative to the main window, as well as relative to a Button.

        private void win1_MouseMove_1(object sender, MouseEventArgs e)
        {
            Point p = e.GetPosition(null);
            MousePosText = string.Format("GetPosition(null): X = {0}, Y = {1}", p.X, p.Y);

            p = e.GetPosition(btn1);
            MousePosText2 = string.Format("GetPosition(btn1): X = {0}, Y = {1}", p.X, p.Y);
        }



#669 – Retrieving the Mouse’s Current Position in an Event Handler

You can use the static Mouse.GetPosition method anywhere in your code to retrieve the current position of your mouse.

You can also access the mouse position through either the MouseButtonEventArgs or MouseEventArgs object passed in to an event handler for any of the mouse-related events.

In the example below, we use the MouseButtonEventArgs.GetPosition and MouseEventArgs.GetPosition methods in the handlers to get the mouse’s position and to update a related label.

        private void win1_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(null);
            MousePosTextLastClick = string.Format("Last click at X = {0}, Y = {1}", p.X, p.Y);
        }

        private void win1_MouseMove_1(object sender, MouseEventArgs e)
        {
            Point p = e.GetPosition(null);
            MousePosText = string.Format("X = {0}, Y = {1}", p.X, p.Y);
        }


#668 – Retrieving the Mouse’s Current Position

You can retrieve the current position of the mouse at any time using the Mouse.GetPosition method.  Mouse is a static class that provides information about the mouse through a variety of static methods.

The GetPosition method returns a Point object, which contains the and Y position of the mouse, relative to a specified user interface element.

In the example below, we call the GetPosition method and then set a property to report the mouse’s position.  (We’ll bind a label’s Content to this property).  Passing null to GetPosition means that we want the position relative to the top-level window.

        private void win1_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            ReportMousePosition();
        }

        // Can be called at any time (not just from event handler)
        public void ReportMousePosition()
        {
            Point p = Mouse.GetPosition(null);

            MousePosText = string.Format("X = {0}, Y = {1}", p.X, p.Y);
        }