简体   繁体   English

鼠标向下移动

[英]mouse down and moves down

What's wrong with it? 它出什么问题了?

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        position_x = e.Location.X;
        position_y = e.Location.Y;
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
     int position_x_next = e.Location.X;
     int position_y_next = e.Location.Y;

     if (position_x_next < position_x)
     {
         MessageBox.Show("it moved left");
     }
    }

You need to keep track of the previous position of the mouse and the new position. 您需要跟踪鼠标的先前位置和新位置。

Create 2 new variables, mouseIsDown and previousMouseX . 创建两个新变量mouseIsDownpreviousMouseX

In your MouseDown handler, set mouseIsDown to true and previousMouseX to mouse.x and set it to false in a MouseUp handler. 在你MouseDown处理程序中,设置mouseIsDowntruepreviousMouseXmouse.x并将其设置为falseMouseUp处理程序。

Then, add a MouseMove handler that checks if previousMouseX is greater than mouse.x . 然后,添加一个MouseMove处理程序,以检查previousMouseX是否大于mouse.x If so, the mouse is moving left. 如果是这样,则鼠标向左移动。 Make sure to update previousMouseX here as well. 确保在这里也更新previousMouseX

您应该每次都检查MouseEventArgs以查看按钮何时按下并捕获X和Y坐标-如果X坐标小于最后一次,则可以告诉它向左移动。

It's pretty simple actually. 实际上,这非常简单。 Store the position it was in the last move event (it's in the e parameter). 存储上一次移动事件中的位置(在e参数中)。 Then compare the current position with the last one. 然后将当前位置与最后一个位置进行比较。 If its X coordinate is to the left it's moving left. 如果其X坐标在左侧,则向左移动。

If you want to distinguish moving exactly left from moving diagonally left-up for instance, you should add a condition to check if the variation in the Y axis is small enough to account for user imprecision. 例如,如果要区分完全向左移动和对角向左移动,则应添加一个条件,以检查Y轴的变化是否足够小以解决用户不精确的问题。

The cheesy way: 俗气的方式:

Set a flag in the mouse down event IsMouseDown = true. 在鼠标按下事件IsMouseDown = true中设置一个标志。 In the mouse move event check if IsMouseDown == true In MouseUp set IsMouseDown = true 在鼠标移动事件中检查IsMouseDown == true在MouseUp中设置IsMouseDown = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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