我刚刚完成了Windows-Forms中的扫雷游戏,我练习了这个游戏。
一切运行得很好,但我发现,如果你点击并按住一个按钮,它就会被展开,即使鼠标不再指向那个按钮。
你对我如何解决这个问题有一个简单的想法吗?
守则:
/// <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();
}
每个按钮在DrawGameField - Method中获取一个mouseUp事件。 每个按钮也会在该方法中获得一个Tag,因此可以识别它。 只要用户(mouseUp)点击其中一个游戏区域按钮,LeftMouseClick就是calles。
如果释放左鼠标按钮的按钮与实际点击的按钮不同,我想取消它。
这应该给用户提供改变主意的可能性......他可能实际上点击一个字段,然后意识到他并不想展开那个领域,但在我的解决方案中他并不是&#39能够撤消他的点击....
答案 0 :(得分:1)
好伙计们,有一点帮助,我有正确的问题解决办法,如下:
使用&#34; MouseClick&#34;而不是&#34; MouseUp&#34;或&#34; MouseDown&#34;。
为什么?
MouseClick在内部使用MouseDown然后执行MouseCapture(通知,该控件在鼠标指针下并检查,鼠标是否仍然指向该控件) 如果是真的 - &gt;点击事件 如果错误 - &gt;返回
工作代码:
/// <summary>
/// Build up a new Gamefield with the number of buttons, the user has selected
/// </summary>
private void DrawGameField()
{
// _xLength is the length of the field in x-direction
// _yLength is the length of the field in y-direction
var buttons = new Button[_xLength, _yLength];
// Filling the buttonArray
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)
};
// Apply a clickEvent to every button
btn.MouseClick += Button_MouseClick; //first change here: Use MouseClick!!!
buttons[xPos, yPos] = btn;
}
}
AddGameButtonsToPanel(buttons);
}
/// <summary>
/// Adds Buttons to the gamepanel
/// </summary>
private void AddGameButtonsToPanel(Button[,] buttons)
{
_buttons = buttons;
_gamePanel.SuspendLayout();
try
{
foreach (var btn in buttons)
_gamePanel.Controls.Add(btn);
}
finally
{
_gamePanel.ResumeLayout();
}
}
/// <summary>
/// Checks which mouse-button was clicked and calls the correct method for that button
/// </summary>
private void Button_MouseClick(object sender, MouseEventArgs e)
{
// If it is the firs time a button is clicked, the stopwatch is started
if (_firstClick)
StartStopWatch();
var btn = sender as Button;
Point pt = (Point)btn.Tag;
if (e.Button == MouseButtons.Left)
Button_LeftClick(btn, pt);
else
Button_RightClick(btn, pt);
}
答案 1 :(得分:0)
您可以在MouseDown事件上设置的表单上创建变量。因此,在MouseDown上,您将变量设置为单击按钮的按钮标记。
然后在MouseUp上,您可以将其设置值与当前发件人标记值进行比较。