本文讲述了关于C#tablelayoutpanel 鼠标定位!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:
TableLayoutPanel是VS的原生控件,可以按下快捷键打开工具箱(ctre+alt+x)然后找到TableLayoutPanel拖到页面上
然后就可以看到是这样的,因为TableLayoutPanel多数用来动态添加删除控件,所以在winform项目中如果要用的动态的话,这个是最好了。
右上角有一个三角符号,你可以点击看看。
好了 废话不多说了 我们来说说 给它美化一下边框和单元格:
//在单元格需要重绘时发生,这个是吧单元格的线条画成红色的
private void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
// 单元格重绘
Pen pp = new Pen(Color.Red);
e.Graphics.DrawRectangle(pp, e.CellBounds.X, e.CellBounds.Y, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
}
///当控件需要重绘时发生,就是把边框颜色画成红色
private void tableLayoutPanel_Paint(object sender, PaintEventArgs e)
{
Pen pp = new Pen(Color.Red);
e.Graphics.DrawRectangle(pp, e.ClipRectangle.X - 1, e.ClipRectangle.Y - 1, e.ClipRectangle.X + e.ClipRectangle.Width - 0, e.ClipRectangle.Y + e.ClipRectangle.Height - 0);
}
当你放大或者动态添加删除数据时,控件会闪烁是吧,下面我们就来解决
这个必须放在窗体事件中 就是为了防止控件闪烁
tableLayoutPanel1.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(tableLayoutPanel1, true, null);
或者你觉得麻烦了 那可以自己写一个控件:
//TableLayoutPanel 绘制边框,防闪屏
public class HSkinTableLayoutPanel : TableLayoutPanel
{
public HSkinTableLayoutPanel()
{
// 防止闪屏
this.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(this, true, null);
}
private Color borderColor = Color.Red;
public Color BorderColor
{
get { return borderColor; }
set { borderColor = value; }
}
protected override void OnCellPaint(TableLayoutCellPaintEventArgs e)
{
//绘制边框
base.OnCellPaint(e);
Pen pp = new Pen(BorderColor);
e.Graphics.DrawRectangle(pp, e.CellBounds.X, e.CellBounds.Y, e.CellBounds.X + this.Width - 1, e.CellBounds.Y + this.Height - 1);
}
}
更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。
想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!