【问题标题】:Mouse moves too fast to capture events鼠标移动太快无法捕捉事件
【发布时间】:2010-02-15 18:33:59
【问题描述】:

这与:a previous question

但问题是,只有当我在 TableLayoutPanel 上和周围快速移动鼠标时,我的代码才会失败。

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

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

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

【问题讨论】:

    标签: c# events mouse


    【解决方案1】:

    鼠标不会通过它经过的每个像素来报告它的位置,报告之间有 20 毫秒的间隔。如果您设法在此时间间隔内越过您的控件,它根本不会捕获任何鼠标事件。

    【讨论】:

    • 这是很好的信息。知道非常有用。谢谢。有检查吗? IE。如果鼠标移动得太快,则禁用处理?
    • 您可以使用鼠标挂钩。这可能有用:blogs.msdn.com/toub/archive/2006/05/03/589468.aspx
    • @alemjerus 是否有说明间隔确实为 20 毫秒的文档?我问是因为我正在尝试解决一个问题,并且我想确保间隔是正确的。
    • @BryanJ 过去 6 年情况发生了变化。现在是 2-10 毫秒:wiki.archlinux.org/index.php/Mouse_polling_rate
    【解决方案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);
            }
        }
    

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

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      相关资源
      最近更新 更多