画周围的一些细胞边界在一个TableLayoutPanel边界、细胞、TableLayoutPanel

2023-09-04 00:30:29 作者:┏(^ω^)=?长腿欧巴

不要问为什么,但我有要求周围绘制某些单元格边框在的TableLayoutPanel

Don't ask why but I have the requirement to draw a border around certain cells in a TableLayoutPanel.

例如,为了简单起见,可以说我有一个1行5列的TableLayoutPanel 。每个单元中有一个按钮。我想提请各地的前3个单元的框,然后另一个框周围的最后2个细胞。因此,两箱合计。

For example, for simplicity, lets say I have a 1 row, 5 column TableLayoutPanel. Each cell has a button in it. I would like to draw a box around the first 3 cells and then another box around the last 2 cells. So two boxes total.

在如何实现这一目标有什么建议?

Any suggestions on how to accomplish that?

感谢。

推荐答案

您可以使用 CellPaint 事件,并在需要时绘制边框的矩形:

You could use CellPaint event and draw the border rectangle when needed:

tableLayoutPanel1.CellPaint += tableLayoutPanel1_CellPaint;

的处理程序:

The handler:

void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    if (e.Column == 1 && e.Row == 0)
        e.Graphics.DrawRectangle(new Pen(Color.Blue), e.CellBounds);
}

您可以用绘制任何边界 ControlPaint

You can draw any kind of border using ControlPaint:

if (e.Column == 1 && e.Row == 0)
{
    var rectangle = e.CellBounds;
    rectangle.Inflate(-1, -1);

    ControlPaint.DrawBorder3D(e.Graphics, rectangle, Border3DStyle.Raised, Border3DSide.All); // 3D border
    ControlPaint.DrawBorder(e.Graphics, rectangle, Color.Red, ButtonBorderStyle.Dotted); // dotted border
}