2010-02-15 33 views
1

这是关系到:a previous question鼠标移动过快,以捕获事件

但问题是我的代码只有当我真的快上方和周围TableLayoutPanel中真正移动鼠标失败。

由于鼠标快速移动,C#或窗口是否可能报告/触发事件?

如果是这样,我该如何纠正它?

谢谢。我希望这不被视为双重发布。如果是这样,道歉。

回答

2

我解决了这个问题。这是一个重新排序逻辑流程的问题。

该解决方案跨越3个鼠标事件MouseEnter,MouseMove,MouseLeave。

private PictureBox HomeLastPicBox = null; 

    private TableLayoutPanelCellPosition HomeLastPosition = new TableLayoutPanelCellPosition(0, 0); 

    private void HomeTableLayoutPanel_MouseMove(object sender, MouseEventArgs e) 
    { 
     PictureBox HomeCurrentPicBox = (PictureBox)(HomeTableLayoutPanel.GetChildAtPoint(e.Location)); 

     if ((HomeCurrentPicBox != HomeLastPicBox) && (HomeCurrentPicBox != null)) 
     { 

      HomeLastPicBox = (PictureBox)HomeTableLayoutPanel.GetControlFromPosition(HomeLastPosition.Column, HomeLastPosition.Row); 

      if (GameModel.HomeCellStatus(HomeLastPosition.Column, HomeLastPosition.Row) == Cell.cellState.WATER) 
      { 
       HomeLastPicBox.Image = Properties.Resources.water; 

      } 

      TableLayoutPanelCellPosition HomeCurrentPosition = HomeTableLayoutPanel.GetCellPosition(HomeCurrentPicBox); 

      if (GameModel.HomeCellStatus(HomeCurrentPosition.Column, HomeCurrentPosition.Row) == Cell.cellState.WATER) 
      { 
       HomeCurrentPicBox.Image = Properties.Resources.scan; 

       HomeLastPosition = HomeCurrentPosition; 
      } 

      gameFormToolTip.SetToolTip(HomeTableLayoutPanel, GameModel.alphaCoords(HomeTableLayoutPanel.GetCellPosition(HomeCurrentPicBox).Column) + "," + HomeTableLayoutPanel.GetCellPosition(HomeCurrentPicBox).Row); 
     } 
    } 

    private void HomeTableLayoutPanel_MouseEnter(object sender, EventArgs e) 
    { 
     Point p = HomeTableLayoutPanel.PointToClient(Control.MousePosition); 
     PictureBox HomeCurrentPicBox = (PictureBox)(HomeTableLayoutPanel.GetChildAtPoint(p)); 

     if (HomeCurrentPicBox != null) 
     { 
      TableLayoutPanelCellPosition HomeCurrentPosition = HomeTableLayoutPanel.GetCellPosition(HomeCurrentPicBox); 

      if (GameModel.HomeCellStatus(HomeCurrentPosition.Column, HomeCurrentPosition.Row) == Cell.cellState.WATER) 
      { 
       HomeCurrentPicBox.Image = Properties.Resources.scan; 
      } 

      gameFormToolTip.SetToolTip(HomeTableLayoutPanel, GameModel.alphaCoords(HomeTableLayoutPanel.GetCellPosition(HomeCurrentPicBox).Column) + "," + HomeTableLayoutPanel.GetCellPosition(HomeCurrentPicBox).Row); 
     } 
    } 

    private void HomeTableLayoutPanel_MouseLeave(object sender, EventArgs e) 
    { 
     if (GameModel.HomeCellStatus(HomeLastPosition.Column, HomeLastPosition.Row) == Cell.cellState.WATER) 
     { 
      HomeLastPicBox = (PictureBox)HomeTableLayoutPanel.GetControlFromPosition(HomeLastPosition.Column, HomeLastPosition.Row); 

      HomeLastPicBox.Image = Properties.Resources.water; 

      gameFormToolTip.SetToolTip(HomeTableLayoutPanel, GameModel.alphaCoords(HomeTableLayoutPanel.GetCellPosition(HomeLastPicBox).Column) + "," + HomeTableLayoutPanel.GetCellPosition(HomeLastPicBox).Row); 
     } 
    } 

我想我会为未来的知识求职者发布解决方案。

谢谢。

3

鼠标不会通过每个像素报告其位置,报告之间有20ms的间隔。如果你在这个时间间隔内控制了你的控制权,它将根本没有捕捉到鼠标事件。

+0

这是很好的信息。非常有用的知道。谢谢。 有没有检查这个?即。如果鼠标移动太快,禁用处理? – iTEgg 2010-02-15 18:45:04

+0

你可以使用鼠标钩子。这可能是有用的:http://blogs.msdn.com/toub/archive/2006/05/03/589468.aspx – alemjerus 2010-02-15 18:49:33

+0

@alemjerus是否有文件说间隔确实是20ms?我问,因为我试图解决一个问题,我想确保间隔是正确的。 – BryanJ 2016-12-15 17:22:37