简体   繁体   中英

Get mouse co-ordinates continuously while mouse move onmousedown

I can get mouse co-ordinates when mouse is down and up as

  private void panel2_MouseDown(object sender, MouseEventArgs e)
{
    mouseClickedX = e.X;
    mouseClickedY = e.Y;
}

   private void panel2_MouseUp(object sender, MouseEventArgs e)
{
    mouseReleaseX = e.X;
    mouseReleaseY = e.Y;
}

But I need the mouse co-ordinates continuously when mouse is down and move until mouse is up. I don't need co-ordinates when mouse move only but I need co-ordinates when mouse is down and move. How to do that?

EDIT:

   private void panel2_MouseMove(object sender, MouseEventArgs e)
        {
            while (isDragging) {
                mouseMoveX = e.X;
                mouseMoveY = e.Y;
                label1.Text = mouseMoveX.ToString();
                label2.Text = mouseMoveY.ToString();
            }
        }

I am using isDragging true or false onmosueup and down but this just hang the application. Should I use timer or thread?

您需要处理MouseMove并检查鼠标是否按下。

There are a few things you should do:

  1. Add to your class a private boolean field called bool isDragging
  2. In the MouseDown handler, set isDragging = true and this.Capture = true
  3. In the MouseUp handler, set isDragging = false and this.Capture = false
  4. Add a MouseMove handler. In it, check if (isDragging) and if it is true, respond as you wish. Your MouseMove handler will be supplied with the current mouse coords.

The use of Capture is important, because otherwise you can lose MouseMove and MouseUp messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM