鼠标按下并向下移动
有什么问题吗?
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");
}
}
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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要跟踪鼠标的先前位置和新位置。
创建 2 个新变量:
mouseIsDown
和previousMouseX
。在
MouseDown
处理程序中,将mouseIsDown
设置为true
,将previousMouseX
设置为mouse.x
并在MouseUp
处理程序中将其设置为false
。然后,添加一个
MouseMove
处理程序来检查previousMouseX
是否大于mouse.x
。如果是这样,则鼠标向左移动。请务必在此处更新previousMouseX
。You need to keep track of the previous position of the mouse and the new position.
Create 2 new variables,
mouseIsDown
andpreviousMouseX
.In your
MouseDown
handler, setmouseIsDown
totrue
andpreviousMouseX
tomouse.x
and set it tofalse
in aMouseUp
handler.Then, add a
MouseMove
handler that checks ifpreviousMouseX
is greater thanmouse.x
. If so, the mouse is moving left. Make sure to updatepreviousMouseX
here as well.您应该检查
MouseEventArgs
每次查看按钮何时按下并捕获 X 和 Y 坐标 - 如果 X 坐标小于上次的值,您可以判断它正在向左移动。You should be checking the
MouseEventArgs
every time to see when the button is down and to capture the X and Y coordinates - you can tell it is moving left if the X coordinate is less than it was the last time.实际上很简单。存储它在上次移动事件中的位置(位于
e
参数中)。然后将当前位置与上一个位置进行比较。如果它的 X 坐标位于左侧,则它会向左移动。例如,如果您想区分精确向左移动和对角线左上移动,您应该添加一个条件来检查 Y 轴的变化是否足够小以解决用户不精确的问题。
It's pretty simple actually. Store the position it was in the last move event (it's in the
e
parameter). Then compare the current position with the last one. If its X coordinate is to the left it's moving left.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.
俗气的方法:
在鼠标按下事件中设置一个标志 IsMouseDown = true。
在鼠标移动事件中检查 IsMouseDown == true
在 MouseUp 中设置 IsMouseDown = true
The cheesy way:
Set a flag in the mouse down event IsMouseDown = true.
In the mouse move event check if IsMouseDown == true
In MouseUp set IsMouseDown = true
鼠标移动事件并传递 X 和 Y 坐标怎么样
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(VS.71).aspx
http://www.java2s.com/Code/CSharp/Event/MouseMovement.htm
What about the mouse move event and passing the X and Y cordinates
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(VS.71).aspx
http://www.java2s.com/Code/CSharp/Event/MouseMovement.htm