2014-12-19 4 views
3

방금 ​​연습 한 Windows-Forms에서 지뢰 찾기 게임을 끝냈습니다.컨트롤이 변경된 경우 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(); 
} 

모든 버튼을 DrawGameField에와 mouseUp 이벤트를 얻을 수 - 방법. 모든 단추는 해당 메서드에서 태그를 가져 오므로 식별 할 수 있습니다. LeftMouseClick은 사용자 (mouseUp)가 gamefield 버튼 중 하나를 클릭하는 즉시 호출됩니다.

왼쪽 마우스 단추를 놓은 단추가 실제로 클릭 한 단추와 다른 경우 취소하고 싶습니다.

이것은 사용자에게 마음을 바꿀 가능성을 주어야한다. 그는 실제로 들판을 클릭 할 수도 있지만 그 분야를 펼치고 싶지는 않다는 것을 깨닫는다. 그러나 내 해결책에서는 그는 할 수 없다. 아직 그의 클릭을 취소하려면 ...

+0

'LeftMouseClick'을 어떤 이벤트에 구독하셨습니까? –

+0

'LeftMouseClick' 어떤 이벤트를 처리합니까? 그것은'Click' 또는'MouseUp' (MouseDown이 아니라) 중 하나 여야합니다. – Sinatr

답변

2

그럼 얘들 아, 약간의 도움으로, 나는 다음과 같다 문제에 대한 올바른 해결책을 가지고 있습니다.

왜? > 이벤트 false의 경우 클릭 - -

MouseClick과는 내부적으로 true의 경우 MouseDown 다음 (마우스가 여전히 제어를 가리키는 여부는 MousePointer의 및 검사의 아래에 있었다 제어 통지) MouseCapture 을 수행합니다> 반환

작업 코드 :

/// <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에서 설정 값을 현재 발신자 태그 값과 비교할 수 있습니다.

사용 "MouseClick과"가 아니라 "이는 MouseUp"또는 "MouseDown"

+0

이것은 아주 간단한 방법처럼 들리지만, 곧 해결책을 게시 할 것입니다. 더 많은 아이디어는 환영받습니다.) – christian

+0

모든 mouseUp 이벤트가 마우스 다운 될 때마다 작동하지 않습니다. 활성화 된 컨트롤 이 해결책으로 변하지 않을 것이다. 필자는 내 컨트롤에 mousecapture를 사용해야하며 어떻게 할 수 있는지 아이디어가 있습니까? – christian

+0

아마도이 도움이됩니다. http://stackoverflow.com/questions/2411062/how-to-get-control-under-mouse-cursor – Brendan