[英]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
. 创建两个新变量
mouseIsDown
和previousMouseX
。
In your MouseDown
handler, set mouseIsDown
to true
and previousMouseX
to mouse.x
and set it to false
in a MouseUp
handler. 在你
MouseDown
处理程序中,设置mouseIsDown
到true
和previousMouseX
到mouse.x
并将其设置为false
的MouseUp
处理程序。
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
What about the mouse move event and passing the X and Y cordinates 鼠标移动事件并传递X和Y坐标怎么办
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(VS.71).aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(VS.71).aspx
http://www.java2s.com/Code/CSharp/Event/MouseMovement.htm http://www.java2s.com/Code/CSharp/Event/MouseMovement.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.