关于tableLayoutPanel的布局问题

yilunduyue 2009-04-14 02:07:15
当tableLayoutPanel列单元格或行单元格合并后中间的线条如何才能去除掉了。。。
急。。。 在线等!!!
...全文
902 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
gomoku 2009-04-14
  • 打赏
  • 举报
回复
还是苦力活 :)

public class BorderedTableLayoutPanel : TableLayoutPanel
{
Pen borderPen;
public Pen BorderPen { get { return this.borderPen; } set { this.borderPen = value; } }

protected override void OnCreateControl()
{
base.OnCreateControl();

this.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;
this.borderPen = this.borderPen == null ? Pens.DarkGray : this.borderPen;
this.ControlAdded += delegate { this.cellBorders = null; };
this.ControlRemoved += delegate { this.cellBorders = null; };
}

protected override void OnCellPaint(TableLayoutCellPaintEventArgs e)
{
base.OnCellPaint(e);

Pen pen = this.BorderPen;
Borders borders = this[e.Row, e.Column];
Point topleft = new Point(e.CellBounds.Left, e.CellBounds.Top);
Point topright = new Point(e.CellBounds.Right - 1, e.CellBounds.Top);
Point bottomleft = new Point(e.CellBounds.Left, e.CellBounds.Bottom - 1);
Point bottomright = new Point(e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

if ((borders & Borders.Left) == Borders.Left) e.Graphics.DrawLine(pen, topleft, bottomleft);
if ((borders & Borders.Right) == Borders.Right) e.Graphics.DrawLine(pen, topright, bottomright);
if ((borders & Borders.Top) == Borders.Top) e.Graphics.DrawLine(pen, topleft, topright);
if ((borders & Borders.Bottom) == Borders.Bottom) e.Graphics.DrawLine(pen, bottomleft, bottomright);
}

[Flags]
enum Borders
{
None = 0, Left = 1, Right = 2, Top = 4, Bottom = 8,
}

Borders[,] cellBorders;
Borders this[int row, int col]
{
get
{
if (this.cellBorders == null)
{
PrepareCellBorders();
}
if (row < this.cellBorders.GetLength(0) && col < this.cellBorders.GetLength(1))
{
return this.cellBorders[row, col];
}
return Borders.None;
}
set
{
if (this.cellBorders == null)
{
PrepareCellBorders();
}
if (row < this.cellBorders.GetLength(0) && col < this.cellBorders.GetLength(1))
{
this.cellBorders[row, col] = value;
}
}
}

private void PrepareCellBorders()
{
this.cellBorders = new Borders[this.RowCount, this.ColumnCount];
for (int y = 0; y < cellBorders.GetLength(0); y++)
{
for (int x = 0; x < cellBorders.GetLength(1); x++)
{
cellBorders[y, x] = Borders.Top | Borders.Left;
if (y == cellBorders.GetLength(0) - 1) cellBorders[y, x] |= Borders.Bottom;
if (x == cellBorders.GetLength(1) - 1) cellBorders[y, x] |= Borders.Right;
}
}

foreach (Control c in this.Controls)
{
int rowspan = this.GetRowSpan(c);
int colspan = this.GetColumnSpan(c);
TableLayoutPanelCellPosition pos = this.GetCellPosition(c);

for (int y = 0; y < rowspan; y++)
{
for (int x = 0; x < colspan; x++)
{
if (y > 0) this[pos.Row + y, pos.Column + x] &= ~Borders.Top;
if (x > 0) this[pos.Row + y, pos.Column + x] &= ~Borders.Left;
}
}
}
}

}
yilunduyue 2009-04-14
  • 打赏
  • 举报
回复
高手们..
yilunduyue 2009-04-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gomoku 的回复:]
可能你要把边框去掉
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;

(如果一定要边框)然后在相应CellPaint中自己画合并后的边框,可以充分用参数中的Row,Column以及CellBounds信息。
[/Quote]
先谢谢了
恩,是需要边框的.
能不能给点代码提示了!对重绘一块很陌生。。。谢谢啊
yilunduyue 2009-04-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 llwinnner 的回复:]
在开始设计的时候,先画好,有拆分的就嵌套
[/Quote]
能说得详细一点么,我现在想实现的就是合并后去除掉中间的线条 谢谢
yilunduyue 2009-04-14
  • 打赏
  • 举报
回复
恩,是需要边框的.
能不能给点代码提示了!对重绘一块很陌生。。。谢谢啊
深海之蓝 2009-04-14
  • 打赏
  • 举报
回复
如果您希望控件跨多个行或多个列,可设置控件上的 RowSpan 和 ColumnSpan 属性。有关更多信息,请参见演练:使用 TableLayoutPanel 在 Windows 窗体上排列控件。

若要在单元格中对齐控件,或在单元格内拉伸控件,请使用控件的 Anchor 属性。有关更多信息,请参见演练:使用 TableLayoutPanel 在 Windows 窗体上排列控件。

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置。
深海之蓝 2009-04-14
  • 打赏
  • 举报
回复
在开始设计的时候,先画好,有拆分的就嵌套
gomoku 2009-04-14
  • 打赏
  • 举报
回复
可能你要把边框去掉
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;

(如果一定要边框)然后在相应CellPaint中自己画合并后的边框,可以充分用参数中的Row,Column以及CellBounds信息。
yilunduyue 2009-04-14
  • 打赏
  • 举报
回复
没人,大侠们帮帮忙啊!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧