1

Thanks for taking a look.

In my desktop application I'd like to move the mouse to the centre of the application window whenever a particular button is pressed. To this end I take the position, width and height of the main window, then set the mouse cursor position based on these values with User32.dll .

The mouse cursor is moved, however it is always significantly off center. Its position is influenced by the pos, width and height, but there seems to be some weird scaling issue between measuring the desired position and setting. Based on this question and its duplicate this is what I am using:

  static public double GetWindowLeft(Window window)
    {
        if (window.WindowState == WindowState.Maximized)
        {
            var leftField = typeof(Window).GetField("_actualLeft", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            return (double)leftField.GetValue(window);
        }
        else
            return window.Left;
    }

    static public double GetWindowTop(Window window)
    {
        if (window.WindowState == WindowState.Maximized)
        {
            var leftField = typeof(Window).GetField("_actualTop", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            return (double)leftField.GetValue(window);
        }
        else
            return window.Top;

    }


    static public void SetPosition(int a, int b)
    {
        SetCursorPos(a, b);
    }

    [DllImport("User32.dll")]
    private static extern bool SetCursorPos(int X, int Y);

}

and in a MainWindow method:

 private void CenterMouse(object sender, RoutedEventArgs e)
    {

        double top = UCMethods.GetWindowTop(this);
        double left = UCMethods.GetWindowLeft(this);

        SetPosition((int)(left+left+ActualWidth) / 2, (int)(top + top + ActualHeight) / 2);
    }

Edit: This seems to run fine on my friend's computer, but has the offset on mine. The further I am from the top left corner of the screen (0,0), the more the mouse cursor lags behind in that direction (mouse cursor is offset in the top left direction). How could this problem be device specific?

B-H
  • 57
  • 1
  • 9

2 Answers2

0

Here is the solution :

    [DllImport("User32.dll")]
    private static extern bool SetCursorPos(int X, int Y);
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var left = Convert.ToInt32(GetActualLeft() + this.ActualWidth / 2);
        var top = Convert.ToInt32(GetActuaTop() + this.ActualHeight / 2);
        SetCursorPos(left, top);
    }

    double GetActualLeft()
    {
        if (this.WindowState == WindowState.Maximized)
        {
            var leftField = typeof(Window).GetField("_actualLeft", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            return (double)leftField.GetValue(this);
        }
        else
            return this.Left;
    }

    double GetActuaTop()
    {
        if (this.WindowState == WindowState.Maximized)
        {
            var topField = typeof(Window).GetField("_actualTop", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            return (double)topField.GetValue(this);
        }
        else
            return this.Top;
    }
Coskun Ozogul
  • 2,389
  • 1
  • 20
  • 32
  • Hello, I ran your app (with a button in xaml) but the mouse cursor was still moved offset. The farther from (0,0) (top left corner of screen) the window was, the greater the offset into the top left direction. Could this problem be specific to my computer? – B-H Jul 24 '18 at 14:54
  • I tried it out on my friend's computer and it ran fine there (positioned the mouse correctly). Do you know why it wouldn't work exactly on my laptop? Am I using the wrong dll? – B-H Jul 24 '18 at 14:59
  • Did you look at the Text Size setting of your laptop is it 100% ? Or graphic card properties ? Is there any weird property ? – Coskun Ozogul Jul 24 '18 at 15:08
  • I have two graphic cards, an AMD and an Intel one. Changing the scaling options didn't change the behaviour. Not sure what property should I be wary of. – B-H Jul 24 '18 at 15:13
  • could you post a screenshot of what is happening after clicking the button – Coskun Ozogul Jul 24 '18 at 15:20
  • Unfortunately screenshots don't capture the mouse cursor. When clicking a button in the center of the screen, the mouse is moved in the top left direction. The further the window is from the top left corner of the screen, the greater the distance. – B-H Jul 24 '18 at 15:22
0

I asked this question somewhere else too, and managed to resolve the issue with help from there. Link to solution

B-H
  • 57
  • 1
  • 9