在我的tablelayoutpanel的单元格上移动鼠标 [英] Mouse Move on a cell of my tablelayoutpanel

查看:143
本文介绍了在我的tablelayoutpanel的单元格上移动鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的TLP有问题。我希望当鼠标移到单元格上方时更改单元格的颜色。我尝试了不同的事情,但没有任何效果。您知道如何解决这个问题吗?

I have a problem with my TLP. I would like the cell's color to be changed when the mouse is moving over the cells. I trieed different things but nothing works. Do you have an idea how I could get over this problem ?

推荐答案

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..

这篇关于在我的tablelayoutpanel的单元格上移动鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆