Mouse moves too fast to capture events

不想你离开。 提交于 2019-12-02 12:56:13

问题


this is related to: a previous question

BUT the question is my code only fails when i move the mouse really really fast over and around the TableLayoutPanel.

is it possible that C# or windows are reporting/triggering events out of order due to the fast mouse movement?

if so, how do i correct it?

thank you. i hope this is not considered double posting. if so, apologies.


回答1:


Mouse does not report its position by every pixel it passes, there are 20ms intervals between reports. If you manage to cross your control within this interval, it will catch no mouse events at all.




回答2:


i solved the issue. it was a matter of reordering the logic flow.

the solution spans 3 mouse events 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);
        }
    }

i thought i would post the solution for future knowledge seekers.

thanks.



来源:https://stackoverflow.com/questions/2268114/mouse-moves-too-fast-to-capture-events

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!