0

How to receive a path of relocation of a mouse between the 1st clicking and the 2nd?

    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;

    }
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40

3 Answers3

1

If you just want the distance between the two points use pythagora. Example:

    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));
    }
MrApnea
  • 1,776
  • 1
  • 9
  • 17
  • This is a shortest distanse, but i need a real distanse on a path of movement of a mouse – Marat Kaliamov Apr 29 '16 at 07:51
  • Then you need to track each time the mouse slightly moves, try looking at using the [`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/how-do-i-capture-the-mouse-move-event) When the mouse moves, you need to do the pythagora distance and add it to the current running total – Draken Apr 29 '16 at 07:56
  • Ok i think i still dont quite get it. Do you want to move the mouse from A to B and know how far away you have come on each mouse move event on the way there? If so just calculate the distance from A to B and then from each mouse move from that point to B and you know how far you have left. – MrApnea Apr 29 '16 at 08:21
  • How to share MouseDown and MouseMove? As I need to write these relocation only in the course of clicking of the mouse button – Marat Kaliamov Apr 29 '16 at 09:41
1

You may try something like

// 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);
    }
}

However, the MouseMove event will be triggered only above the form. If the mouse is outside of it - distance is not taken into account. It also doesn't work when moving over other controls, but we can add to them MouseMove handlers too.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
0

I could make

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

flavio.donze
  • 7,432
  • 9
  • 58
  • 91