但问题是我的代码只有当我在TableLayoutPanel周围移动鼠标时才会失败。
由于鼠标快速移动,C#或Windows是否可能无序地报告/触发事件?
如果是的话,我该如何纠正呢?
谢谢你。我希望这不是双重发布。如果是的话,道歉。答案 0 :(得分:3)
鼠标不会通过它传递的每个像素报告其位置,报告之间的间隔为20毫秒。如果您设法在此时间间隔内跨越控件,则根本不会捕获任何鼠标事件。
答案 1 :(得分: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);
}
}
我以为我会为未来的知识寻求者发布解决方案。
感谢。