如何检测拖动事件? [英] How to detect a dragging event?

查看:75
本文介绍了如何检测拖动事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要我的程序来检测 PictureBox 中的水平/垂直滑动/拖动事件。



我完全了解 MouseUp MouseDown MouseMove ,但我不确定如何实现它们,以及如何检测光标坐标以验证其位置,是否在<$ c $内拖动c> PictureBox 是否向右或向左,向上或向下。



任何人都可以提供一些提示吗?谢谢!



- 在这个问题上滑动和拖动是同义词。



- 是的,因为游戏规则是用户应将光标拖动到所示箭头的同一方向。关于标准,这是我不确定的事情;你在谈论我将为游戏设定的标准坐​​标是什么?简单地说,垂直方向是用户向上或向下拖动箭头,向右或向左拖动,具体取决于显示的箭头。



- 拖动来自用户的动作不必是完全直线,但仍然必须始终朝正确的方向前进。我不认为我真的需要跟踪光标占据的每个点,只要它被拖到正确的方向,如果这是有意义的(虽然现在我正在尝试构建代码而我是想如果使用 MouseMove 会更容易确保用户拖动到正确的方向。)

解决方案

< blockquote>使用HitTest类检查原始点击是否在图片框内。使用PointToScreen和/或PointToClient来检测鼠标指针相对于控件或屏幕的确切位置。



对于一般移动检测,您需要三点:

1st:原始点击点 - 保存在MouseDown中,用于轮询位置开始的计时器



第二个:最后一个轮询点(每隔几毫秒更改一次 - 如果您希望鼠标移动速度慢,请将计时器设置为大约50-100毫秒,快速移动时间更短。



第3个点是mousemove中的当前点,您可以将此位置与最后保存的(第二个) - 这决定了移动的方向 - 将它与原始点进行比较,看看你的鼠标离原点有多远。





计时器的替代方案:确定一些固定距离(比如5px),每当你的鼠标移动点(第3个,当前的)改变该距离时,保存新点(第2个)。





没有具体的代码,因为你没有指定你需要的东西,但这些是提示:)


我的目标是给你一些想法和代码的smidgen,我希望能帮助你自己写游戏。



在游戏过程中监控鼠标移动的策略的快速草图。我假设:



0.你定义的方差阈值决定了在玩游戏时鼠标在水平轴和垂直轴上的变化距离。在int类型的变量中:'threshold



1.你将在变量mdX和mdY以及最终鼠标中记录初始鼠标向下x,y位置 - 在muX中鼠标向上位置,以及muY。



1.a.你将创建一个布尔标志来指示当前游戏是垂直播放还是水平播放:isUpDown



1.b.你将创建一个布尔标志来指示当前游戏是否结束:isGameOver



1.c.你将创建一个布尔标志来指示鼠标是否已启动:isMouseUp



1.d.你将在变量'pictureRectangle中记录PictureBox的DisplayRectangle



2.你将编写一个名为'gameOver的游戏结束函数(boolean completionState,它被调用)每当游戏结束时,如果'completionState为真,你将做你需要做的事情,弄清楚如果用户赢了;如果'completionState为假,游戏就会丢失。



3.我假设用户可以通过三种方式输掉游戏:



a。鼠标停止时将鼠标移到PictureBox外部游戏正在进行中。



b。当游戏正在向下或向上播放时,将鼠标向右或向左移动太远当b =左右或左右,

  private   void  pictureBox1_MouseMove( object  sender,MouseEventArgs e)
{
// 忽略鼠标如果游戏没有进行游戏,或已结束
如果(isMouseUp ||) isGameOver) return ;

// 鼠标现在在PictureBox之外?
if (!pictureRectangle.Contains(e.Location))gameOver( false );

// 公差测试代码在这里......
if (isUpDown)
{
// 水平方差测试
如果(Math.Abs​​(mdX - eX)> ; threshold)gameOver( false );
}
其他
{
// < span class =code-comment>测试垂直方差
// 给你写
}
}

这是一个快速(再次,故意不完整)的草图,描述了如何构建'gameOver函数:

  private   void  gameOver( bool  completionState)
{
isGameComplete = true ;
isGameOver = true ;

bool isAWin = false ;

if (completionState)
{
// 留给您编写
// 可能获胜
// isAWin = true ... if ... for you to figure out
}

// 状态报告
if (isAWin)
{
// 留给你写
// 明确胜利
}
else
{
// 留给你写
// 明确损失
}
}


每个鼠标事件都会传递一个MouseEventArgs参数,该参数为您提供Location属性,以查找触发事件时鼠标的位置。如果你将它与之前的位置进行比较,你知道它移动的方向。



如果鼠标离开PictureBox,你将不再获得PictureBox处理程序......


So I need my program to detect a horizontal/vertical swiping/dragging event inside a PictureBox.

I quite know about MouseUp, MouseDown and MouseMove, but I'm not sure how I will implement them, and how to detect the coordinates of the cursor to validate its position, whether it's dragging inside the PictureBox or not, whether to the right or to the left, upwards or downwards.

Can anyone drop a few hints? Thank you!

- Swiping and dragging in this question are synonymous.

- Yes, because the rule of the game is the user should drag the cursor to the same direction of the arrow shown. With regards to the criteria, that's the thing I'm not sure about; are you talking about what are the standard coordinates I will set for the game? Simply put, vertical would be the user dragging inside the arrow either up or down, and horizontal, either right or left, depending on the arrow shown.

- The dragging action from the user doesn't have to be in a perfectly straight line, but still it must be consistently going in the correct direction. I don't think I really have much need for keeping track of every point the cursor occupies, as long as it's being dragged to the correct direction, if that makes sense (Although now I'm trying to construct the code and I'm thinking if using the MouseMove would make it easier to make sure the user is dragging to the correct direction).

解决方案

Use HitTest class to check if original click is within the picturebox. Use PointToScreen and/or PointToClient to detect where exactly is your Mouse pointer relative to the control or the screen.

For the general movement detection you need three points:
1st: original click point - saved in MouseDown, timer for polling the position start

2nd: last poll point (changing every couple of milliseconds - set the timer to around 50-100 ms if you expect slow mouse movement, less for quick movements)

3rd point is current point in mousemove where you compare this position with the last saved (2nd) - this determines the direction of the movement - compare it with the original point to see how far from the original point your mouse is.


Alternative to the timer: determine some fixed distance (say 5px) and every time your mouse movement point (3rd, current one) changes for that distance, save new point (2nd).


No concrete code as you didn't specify what you need, but these are hints :)


My goal here is to give you some ideas, and a smidgen of code, that I hope will help you write the game yourself.

A quick sketch of strategies for monitoring the mouse movement during the game. I'll assume:

0. you define the variance threshold the determines how far the mouse can vary on the horizontal and vertical axes when the game is being played. in a variable of type int: 'threshold

1. you'll record the initial mouse down x,y position in variables mdX, and mdY, and the final mouse-position on mouse-up in muX, and muY.

1.a. you'll create a boolean flag to indicate if the current game is being played vertically, or horizontally: isUpDown

1.b. you'll create a boolean flag to indicate if the current game is over: isGameOver

1.c. you'll create a boolean flag to indicate if mouse is up: isMouseUp

1.d. you'll record the DisplayRectangle of the PictureBox in the variable 'pictureRectangle

2. you'll write an end of game function called 'gameOver(boolean completionState that gets called whenever the game ends, and, if 'completionState is true, you'll do what you need to do figure out if the user won; if 'completionState is false, the game is lost.

3. I assume there are three ways the user can lose the game:

a. moving the mouse outside the PictureBox while the Mouse is down and the game is in play.

b. moving the mouse too far right or left in the horizontal axis when the game is being played down-up, or up-down

c. moving the mouse too far up or down in the vertical axis when the game is being played left-right, or right, left

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    // ignore mouse events if the game is not in play, or is over
    if (isMouseUp || isGameOver) return;

    // is the mouse now outside the PictureBox ?
    if (! pictureRectangle.Contains(e.Location)) gameOver(false);

    // tolerance testing code goes here ...
    if (isUpDown)
    {
        // test for horizontal variance
        if (Math.Abs(mdX - e.X) > threshold) gameOver(false);
    }
    else
    {
        // test for vertical variance
        // for you to write
    }
}

Here's a quick (again, deliberately incomplete) sketch of how you might structure the 'gameOver function:

private void gameOver(bool completionState)
{
    isGameComplete = true;
    isGameOver = true;

    bool isAWin = false;

    if (completionState)
    {
        // left for you to write
        // possible win
        // isAWin = true ... if ... for you to figure out
    }

    // status report
    if(isAWin)
    {
        // left for you to write
        // definite win
    }
    else
    {
       // left for you to write
       // definite loss
    }
}


Each of the mouse events passes you a MouseEventArgs parameter which provides you the Location property to find where the mouse was when the event was fired. If you compare this to teh previous location, you know in which direction it has moved.

If the mouse leaves the PictureBox, you will no longer get the events in the PictureBox handlers...


这篇关于如何检测拖动事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆