Mouse Cursor position changes

10,260

I understand you have a drag & drop code you want to execute only if the mouse has moved a certain distance. If so:

You can hook to the mouse move event once the user has performed the initial action (mouse down on the item to drag ?). Then compare the mouse coordinates on the mouse move event and trigger the "dragdrop code" once the coordinates difference is higher then the arbitrary value you set.

private int difference = 10;
private int xPosition;
private int yPosition;

private void item_MouseDown(object sender, MouseEventArgs e) 
{
    this.MouseMove += new MouseEventHandler(Form_MouseMove);
    xPosition = e.X;
    yPosition = e.Y;
}

private void Form_MouseMove(object sender, MouseEventArgs e) 
{
    if (e.X < xPosition - difference
        || e.X > xPosition + difference
        || e.Y < yPosition - difference
        || e.Y > yPosition + difference) 
    {
        //Execute "dragdrop" code
        this.MouseMove -= Form_MouseMove;
    }
}

This would execute dragdrop when the cursor moves out of a virtual 10x10 square.

Share:
10,260

Related videos on Youtube

pumphandle
Author by

pumphandle

Updated on June 04, 2022

Comments

  • pumphandle
    pumphandle almost 2 years

    Hi I have a windows form application which i want to move my mouse then dragdrop will function but i have tried using mousemove mouse event to do it , but seems like dragdrop is very sensitive.So what i'm asking is whether is it possible to detect if the mouse cursor moves at least a certain distance away from the current cursor then it does the dragdrop code.

  • pumphandle
    pumphandle over 13 years
    i'll try implementing that code snippet you shared thanks for answering my question i think it'll work