2014-12-19 65 views
3

我刚刚在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。

我想取消,如果释放鼠标左键的按钮是不同的按钮,实际上点击。

这应该给用户改变主意的可能性......他可能实际上点击了一个字段,然后意识到他不想展开该字段,但在我的解决方案中,他不能撤消他的点击呢......

+0

您订阅了什么事件'LeftMouseClick'? – 2014-12-19 09:38:22

+0

LeftMouseClick处理哪个事件?它应该是'Click'或'MouseUp'(不是'MouseDown')。 – Sinatr 2014-12-19 09:38:24

回答

2

好,大家好,有一点点帮助,我有正确的解决问题的办法,主要内容如下:

使用“鼠标点击”而不是“的MouseUp”或“的MouseDown”。

为什么?

鼠标点击在内部使用的MouseDown然后执行MouseCapture(通知,其控制是的MousePointer和检查下,鼠标是否仍然指向对照) 如果为true - >单击事件 如果假 - >返回

工作代码:

/// <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

您可以在MouseDown事件上设置的窗体上创建一个变量。因此,在MouseDown中,您将变量设置为单击按钮的按钮标签。

然后在MouseUp上,您可以将他设置的值与当前发件人标记值进行比较。

+0

这听起来像一个非常简单的方法,我会尽快发布我的解决方案..更多的想法是stimm欢迎;) – christian 2014-12-19 10:13:41

+0

好吧,这将无法正常工作,因为每个mouseUp事件后来mousedown ... 激活的控制不会改变这个解决方案。 我不知何故需要在我的控件上使用mousecapture,有没有人有一个想法如何做到这一点? – christian 2014-12-19 10:57:05

+0

也许这个协助:http://stackoverflow.com/questions/2411062/how-to-get-control-under-mouse-cursor – Brendan 2014-12-19 11:43:58

相关问题