且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在我的tablelayoutpanel的单元格上移动鼠标

更新时间:2023-12-06 10:32:46

TLP不太适合使用。

TLPs are not very nice to work with.

您可以使用 TableLayoutCellPaintEventArgs 了解正在绘制的单元格,并将光标的屏幕位置转换为 PointToClient ..

You can use the TableLayoutCellPaintEventArgs to learn about a cell while is being painted and convert the cursor's screen position to the relative one with PointToClient..

这里是一个示例,但我不确定它对于较大的TLP是否有效:

Here is an example, but I'm not sure how well it will work for larger TLPs:

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数组中,并将其用作替代颜色。详细信息将取决于您要实现的目标。

This paints the cell the cursor is over and resets when it leaves. If you want to keep the changed color you will need to store it in an 2d-array and use that as the alternative color. The details will depend on just what you want to achieve.

您可能还需要研究此帖子以了解有关使用TLP的更多信息。

You may also want to study this post to learn more about working with TLPs..