2016-11-20 41 views
0

我有一些元素(ZedGraph)託管在wpf窗口中。 我想獲得我的mousecursor的x和y座標。 它適用於窗口的其餘部分,但只要將鼠標懸停在Elementhost上,數字就會被凍結。 我已經發現Elementhost不會傳遞事件,但我沒有找到解決該問題的工作方案。WinForm主辦在WPF Mouse.getPosition不起作用

許多在此先感謝對這個問題

+0

歡迎來到SO Andre。不要害羞,分享你現在使用的代碼。 – Jim

回答

0

您可以使用鼠標移動事件爲您頁面窗口任何提示。例如,頁面窗口的名稱是mainWindow,而元素的名稱是myElement1。然後你就可以得到你的 元素的位置X-Y和與鼠標的比較使用它定位X-Y,就像在下面的例子中,

private void mainWindow_MouseMove(object sender, MouseEventArgs e) 
{ 
    System.Windows.Point thepnt = new System.Windows.Point(); 

    thepnt = e.GetPosition(myElement1); 
    if (((thepnt.X<=100)|| (thepnt.X > myElement1.Width)) || (thepnt.Y < 100)) 
    { 
     //do something... 
    } 
    else 
    { 
     //do something else.... 
    } 
} 

希望這些幫助。