Mouse Cursor position changes

pumphandle picture pumphandle · Nov 10, 2010 · Viewed 10k times · Source

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.

Answer

Jla picture Jla · Nov 10, 2010

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.