Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set mouse position in WPF?

Tags:

c#

wpf

I have a two dimensional array with coordinates and i want to make the mouse move with the specific pattern those coordinates create in a WPF application. Can you help me? I've tried the Cursor class but it will not work. Obviously I am doing something wrong.

private void SetPosition( int a, int b)
{
    this.Cursor = new Cursor(Cursor.Current.Handle);
    Cursor.Position = new Point(a, b);
}

This is the method i use the a and b are from the array. thanks in advance!

PS that method is inside an event that fires 20 times a second.

like image 884
Kwstas Avatar asked Nov 18 '11 16:11

Kwstas


1 Answers

I'm not entirely sure if there is a better way to do it in WPF (It seems the code you are using is targeted at WinForms), but using Platform Invoke on SetCursorPos seems to do the trick:

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

[DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
like image 179
vcsjones Avatar answered Oct 13 '22 12:10

vcsjones