2012-06-05 50 views
1

我有一個WPF和C#項目,我想以編程方式測試我從用戶的角度來看一些懸停事件。我可以繼續,只是手動發佈用戶懸停時發生的事件,但在此之前,我想知道是否可以模擬鼠標移動並將它放在屏幕上的代碼中,然後繼續前進並觸發在視覺樹上冒泡的MouseMove(或任何適當的)事件。在測試中模擬WPF中的鼠標移動

回答

0

我使用SendInput函數來合成擊鍵和鼠標事件,我覺得它太棒了!詳情請查看MSDN link

0

我不知道是否有模擬鼠標移動的方式,但您可以通過編程移動鼠標:

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

或者,如果你想讓它去控制中間:

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

只需用您想要的控件替換button1即可。

希望這會有所幫助!

+0

不幸的是,這隻能在WinForms中實現。問題是關於WPF。 – gehho

1

我知道這是一個相當古老的問題,但希望它可以幫助任何未來碰到它的人。

爲了把它放在任何控制的可點擊的位置(主要是中間):

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

或者,類似於什麼@3aw5TZetdf does with Cursor.Position,你可以把它或者相對於當前位置或指定設置的特定位置一個新的:

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