C # WPF Application .NET 4.5. Setting the position of the mouse

The first time asking a question here, the solutions I found here don't work for some reason. My application needs to set the position of the mouse when the window becomes active, I have a function but cannot get the cursor properties to work. For some reason I can't seem to use Cursor.Position or anything really. I was hoping to visit the chats to find a solution, but apparently I cannot speak until I have 20 reputation.

So here I am asking how to change the cursor position to something like

this.Cursor.SetPosition (x, y);

Thanks for the help.

Edit: Tried this already as a test from here :

private void MoveCursor()
{
   // Set the Current cursor, move the cursor Position, 
   // and set its clipping rectangle to the form.  

   this.Cursor = new Cursor(Cursor.Current.Handle);
   Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
   Cursor.Clip = new Rectangle(this.Location, this.Size);
}

      

but the compiler is complaining about Current, Position, Clip, Location, Size

Final solution:

using System.Runtime.InteropServices;

...

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

...

Point relativePoint = MouseCaptureButton.TransformToAncestor(this)
                           .Transform(new Point(0, 0));
Point pt = new Point(relativePoint.X + MouseCaptureButton.ActualWidth / 2,
                     relativePoint.Y + MouseCaptureButton.ActualHeight / 2);
Point windowCenterPoint = pt;//new Point(125, 80);
Point centerPointRelativeToSCreen = this.PointToScreen(windowCenterPoint);
SetCursorPos((int)centerPointRelativeToSCreen.X, (int)centerPointRelativeToSCreen.Y);

      

+3


source to share


1 answer


You can use InteropServices

to accomplish this quite easily:

// Quick and dirty sample...
public partial class MainWindow : Window
{
    [DllImport("User32.dll")]
    private static extern bool SetCursorPos(int X, int Y);

    public MainWindow()
    {
        InitializeComponent();
        SetCursorPos(100, 100);
    }
}

      

Just make sure you include the namespace System.Runtime.InteropServices

. There are many other ways as well, such as those mentioned in the duplicate link above. Use what's best for you.

EDIT:

In a request in the comment, here's one way to make it the coordinate system of the application window rather than global:



public partial class MainWindow : Window
{
    [DllImport("User32.dll")]
    private static extern bool SetCursorPos(int X, int Y);

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        SetCursor(200, 200);
    }

    private static void SetCursor(int x, int y)
    {
        // Left boundary
        var xL = (int)App.Current.MainWindow.Left;
        // Top boundary
        var yT = (int)App.Current.MainWindow.Top;

        SetCursorPos(x + xL, y + yT);
    }
}

      

I don't think you will ... but just make sure you are not trying to get the coordinates Window

during the initialization phase (in the constructor). Wait for it to load, just like I did above; otherwise you can get NaN

for some values.

If you want to restrict it to a window constraint, an easy way would be to add System.Windows.Forms

to your links and use the code provided in the duplicate link. However, if you want to use my method (all about personal preference ... I use what I like ... and I like it PInvoke

), you can check the position x

and y

in the SetCursor(..)

earlier pass them SetCursorPos(..)

similar to this:

private static void SetCursor(int x, int y)
{
    // Left boundary
    var xL = (int)App.Current.MainWindow.Left;
    // Right boundary
    var xR = xL + (int)App.Current.MainWindow.Width;
    // Top boundary
    var yT = (int)App.Current.MainWindow.Top;
    // Bottom boundary
    var yB = yT + (int)App.Current.MainWindow.Height;

    x += xL;
    y += yT;

    if (x < xL)
    {
        x = xL;
    }
    else if (x > xR)
    {
        x = xR;
    }

    if (y < yT)
    {
        y = yT;
    }
    else if (y > yB)
    {
        y = yB;
    }

    SetCursorPos(x, y);
}

      

Note that you may need to accommodate the border if the application uses the Windows look and feel.

+3


source







All Articles