2016-04-29 28 views
0

如何在第1次點擊和第2次之間接收鼠標重定位路徑?MouseKeyHook c#鼠標重定位路徑

private void OnMouseDown(object sender, MouseEventArgs e) 
    { 

     Log(string.Format("MouseDown \t\t {0}\n", e.Button)); 
     LogMousePosition(string.Format("\n\nx={0:0000}\ny={1:0000}", e.X, e.Y)); 
     if (lastX != -100 && lastY != -100) 
     { 
      shortestDistanse = Convert.ToInt64(Math.Sqrt((Math.Pow(e.X - lastX, 2)) + (Math.Pow(e.Y - lastY, 2)))); 
      LogMousePosition(string.Format("\nshortDistanse\t\t {0}\n", shortestDistanse)); 
     } 
     lastX = e.X; 
     lastY = e.Y; 

    } 

回答

0

如果你只是想要兩點之間的距離使用畢達哥拉。 例子:

private double GetDistance(Point p1, Point p2) { 
     int x = Math.Abs(p1.X - p2.X); 
     int y = Math.Abs(p1.Y - p2.Y); 

     return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)); 
    } 
+0

這是一個最短的距離,但我需要一個真正的鼠標移動路徑上的餘暉 –

+0

然後,您需要跟蹤每次鼠標稍微移動時,嘗試使用['MouseMove'](https:/ /msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(v=vs.110).aspx)event or:[This answer](http://stackoverflow.com/questions/ 2063974 /如何捕獲鼠標移動事件) 當鼠標移動時,您需要執行畢達哥拉距離並將其添加到當前正在運行的總數 – Draken

+0

好吧,我認爲我仍然沒有完全理解它。你是否想將鼠標從A移動到B,並且知道在途中每個鼠標移動事件有多遠?如果是這樣,只需計算從A到B的距離,然後從每個鼠標從該點移動到B,並且知道離開了多遠。 – MrApnea

0

我可以讓

string pathMList = "C:\\logs/testMList.txt";` 


    private void HookManager_MouseMove(object sender, MouseEventArgs e) 
    { 
     labelMousePosition.Text = string.Format("x={0:0000}; y={1:0000}", e.X, e.Y); 
     if (mouseDownMove == 2) 
     { 
      LogMList(string.Format("\nx={0:0000} y={1:0000}", e.X, e.Y)); 
     } 


    } 

    private void OnMouseDown(object sender, MouseEventArgs e) 
    { 

     Log(string.Format("MouseDown \t\t {0}\n", e.Button)); 
     LogMousePosition(string.Format("\n\nx={0:0000}\ny={1:0000}", e.X, e.Y)); 
     if (lastX != -100 && lastY != -100) 
     { 
      shortestDistanse = Convert.ToInt64(Math.Sqrt((Math.Pow(e.X - lastX, 2)) + (Math.Pow(e.Y - lastY, 2)))); 
      LogMousePosition(string.Format("\nshortDistanse\t\t {0}\n", shortestDistanse)); 
      LogMList(string.Format("\n\n    NEW CLICK\n\nx={0:0000} y={1:0000}", e.X, e.Y)); 

     } 
     lastX = e.X; 
     lastY = e.Y; 
     mouseDownMove = 2; 

    } 

enter image description here

+0

的過程中編寫這些重定位,您是否可以將響應標記爲有用,以便我可以加載圖像?)) –

0

你可以嘗試像

// form fields 
bool pressed; 
List<Point> path; 

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (!pressed) 
    { 
     pressed = true; 
     path = new List<Point>(); 
     path.Add(e.Location); 
    } 
    else 
    { 
     pressed = false; 
     // calculate distance from List 
     // log distance 
    } 
} 

private void Form1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (pressed) 
    { 
     path.Add(e.Location); 
    } 
} 

然而,MouseMove事件只以上的形式來觸發。如果鼠標在它之外 - 距離不被考慮在內。在移動其他控件時它也不起作用,但我們也可以將其添加到MouseMove處理程序中。