鼠标在我的表格布局面板的单元格上移动

本文关键字:单元格 移动 我的 表格 布局 鼠标 | 更新日期: 2023-09-27 18:34:46

我的TLP有问题。我希望当鼠标在单元格上移动时更改单元格的颜色。我尝试了不同的东西,但没有任何效果。你知道我如何克服这个问题吗?

鼠标在我的表格布局面板的单元格上移动

TLP不是很好用。

您可以使用TableLayoutCellPaintEventArgs在绘制单元格时了解单元格,并将光标的屏幕位置转换为带有 PointToClient 的相对位置。

下面是一个示例,但我不确定它对大型 TLP 的效果如何:

private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
    tableLayoutPanel1.Invalidate();
}
private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    Point pt = tableLayoutPanel1.PointToClient(Cursor.Position);
    using (SolidBrush brush = new SolidBrush(e.CellBounds.Contains(pt) ? 
                                             Color.Red : tableLayoutPanel1.BackColor))
        e.Graphics.FillRectangle(brush, e.CellBounds);
}

这将绘制光标所在的单元格,并在光标离开时重置。如果要保留更改的颜色,则需要将其存储在2d数组中并将其用作替代颜色。细节将取决于您想要实现的目标。

您可能还想学习这篇文章以了解有关使用 TLP 的更多信息。