sohum
sohum

Reputation: 3227

Simulating mouse movement in WPF in a test

I have a WPF and C# project and I want to programmatically test a few hover events I have from a user's perspective. I could go ahead and just fire the event that occurs on user hover manually, but before I did that, I was wondering if it was possible to simulate mouse movement and put it on a location on the screen from code that then went ahead and fired the MouseMove (or whatever appropriate) events that bubbled down the visual tree.

Upvotes: 1

Views: 3426

Answers (2)

RuthlessKnight
RuthlessKnight

Reputation: 39

I know this is quite an old question but hopefully it could help anyone that come across it in future.

To put it in a clickable position (mostly the middle) of any control :

window.Mouse.Location = item.ClickablePoint; //using specific item

Or, similarly to what @3aw5TZetdf does with Cursor.Position, you can set it to a specific location either relative to the current location or specifying a new one:

var point = window.Mouse.Location;
window.Mouse.Location = new Point(point.X - 200, p.Y - 200); // New location using current one. Replace 200 with your desire value
window.Mouse.Location = new Point(200, 200) // new location

Upvotes: 2

matthewr
matthewr

Reputation: 4749

I am not sure if there is a way to simulate mouse movement, but you can move the mouse programmatically:

Cursor.Position = new Point(x, y); // x and y are integers that form a point

Or if you want it to go to the middle of a control:

Cursor.Position = new Point(this.Location.X + button1.Location.X + button1.Width / 2,this.Location.Y + button1.Location.Y + button1.Height);

Just replace button1 with your desired control.

Hope this helps!

Upvotes: 0

Related Questions