page contents

C#关于重绘tableLayoutPanel单元格边框后的问题

本文讲述了C#关于重绘tableLayoutPanel单元格边框后的问题!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-09-5DmSZsxd633107a571340.png本文讲述了C#关于重绘tableLayoutPanel单元格边框后的问题!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

1.用label填充tableLayoutPanel1单元格,label的属性设置为:

anchor:Top, Bottom, Left, Right;

Dock:Fill;

margin:1,1,1,1

Rowspan:2;//根据需要

TextAline:MiddleCenter;

其他属性默认

tableLayoutPanel1的属性默认

得到的结果为:

为了显示表格边框线,在tableLayoutPanel1_CellPaint事件中绘制单元格边框:

        private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
        { //绘制单元格边框
            Pen cpen = new Pen(Color.Black);
            cpen.Width = 1F;
            Rectangle rectangle = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.X + this.Width, e.CellBounds.Y + this.Height);
            e.Graphics.DrawRectangle(cpen,rectangle);
        }

得到的结果为(tableLayoutPanel1的底边和右边边框线不显示,这是问题一):

补救办法一,在tableLayoutPanel1的四个边框处绘制矩形:

        private void SplitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {//绘制表格边框
            Pen tpen = new Pen(Color.Black);
            tpen.Width = 1F;
            int x = tableLayoutPanel1.Bounds.X;
            int y = tableLayoutPanel1.Bounds.Y;
            int width = tableLayoutPanel1.Width;
            int height = tableLayoutPanel1.Height;
            //绘制矩形
            Rectangle rectangle = new Rectangle(x, y, x + width, y + height);
            e.Graphics.DrawRectangle(tpen, rectangle);
        }

得到的结果为:tableLayoutPanel1右边框和下边框和单元格之间有缝隙,这是问题二(tableLayoutPanel1放在SplitContainer1_Panel1内):

补救办法二,单独绘制tableLayoutPanel1的下边框和右边框:

        private void SplitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {//绘制表格右、下边框
            Pen tpen = new Pen(Color.Black);
            tpen.Width = 1F;
            int x = tableLayoutPanel1.Bounds.X;
            int y = tableLayoutPanel1.Bounds.Y;
            int width = tableLayoutPanel1.Width;
            int height = tableLayoutPanel1.Height;
            //绘制矩形
            //Rectangle rectangle = new Rectangle(x, y, x + width, y + height);
            //e.Graphics.DrawRectangle(tpen, rectangle);
            //绘制底边
            e.Graphics.DrawLine(tpen, x, y + height, x+width, y + height);
            //绘制右边
            e.Graphics.DrawLine(tpen, x + width, y, x + width, y + height);
        }        

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg


  • 发表于 2022-09-26 10:00
  • 阅读 ( 1090 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. Pack 1131 文章
  3. 小柒 1046 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章