我刚刚在Windows窗体中完成了扫雷游戏,这是我为实践而完成的。如何控制已经改变,如何中断mouseUp事件
一切运行良好,但我发现,如果您单击并按住一个按钮,它将展开,即使鼠标不再指向该按钮。
你有一个简单的想法,我该如何解决这个问题?
验证码:
/// <summary>
/// Build up a new Gamefield with the number of buttons, the user has selected
/// </summary>
private void DrawGameField()
{
_buttons = new Button[_xLength, _yLength];
for (int yPos = 0; yPos < _yLength; yPos++)
{
for (int xPos = 0; xPos < _xLength; xPos++)
{
var btn = new Button()
{
Tag = new Point(xPos, yPos),
Location = new Point(xPos * 30, yPos * 30),
Size = new Size(30, 30),
};
_buttons[xPos, yPos] = (Button)btn;
btn.MouseUp += btn_Click;
_gamePanel.Controls.Add(btn);
}
}
}
/// <summary>
/// Is called when a field is left-clicked
/// </summary>
private void LeftMouseClick(object sender, MouseEventArgs e)
{
var btn = sender as Button;
Point pt = (Point)btn.Tag;
// If there's already a flag on the field make it unclickable for left mouseclick
if (btn.Image != null)
return;
_game.UnfoldAutomatically(pt.X, pt.Y);
btn.Text = (_game.ReturnNumberInField(pt.X, pt.Y)).ToString();
// If clicked field was not a bombfield
if (btn.Text != "-1")
UnfoldFields();
else
LooseGame();
}
每一个按键都得到了mouseUp事件事件在DrawGameField - 方法。 每个按钮在该方法中也会得到一个标签,所以它可以被识别。 只要用户(mouseUp)在游戏区按钮上点击一次,LeftMouseClick就是calles。
我想取消,如果释放鼠标左键的按钮是不同的按钮,实际上点击。
这应该给用户改变主意的可能性......他可能实际上点击了一个字段,然后意识到他不想展开该字段,但在我的解决方案中,他不能撤消他的点击呢......
您订阅了什么事件'LeftMouseClick'? – 2014-12-19 09:38:22
LeftMouseClick处理哪个事件?它应该是'Click'或'MouseUp'(不是'MouseDown')。 – Sinatr 2014-12-19 09:38:24