tablelayoutpanel 怎样合并单元格

uncleson88 2012-05-28 08:43:38
我知道控件的RowSpan和ColumnSpan属性和tablelayoutpanel.SetRowSpan(),但是这样只是把控件变大,单元格上的线还在,如果CellBorderStyle = None 当然没事,可如果想要有边框的话就太丑了。
怎样实现真正的单元格合并呢?
...全文
1441 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
uncleson88 2012-05-28
  • 打赏
  • 举报
回复
能教教我怎么反编译吗?.NET 在哪个文件里?
uncleson88 2012-05-28
  • 打赏
  • 举报
回复
谢谢!
uncleson88 2012-05-28
  • 打赏
  • 举报
回复
好晕哦~~~~没搞过啊~~~~您帮帮我
bdmh 2012-05-28
  • 打赏
  • 举报
回复
下载 Reflector.exe 去反编译.net,看看tablelayoutpanel的 OnPaintBackground 代码就清楚了

它里面调用 ControlPaint.PaintTableCellBorder

以下是ControlPaint.PaintTableCellBorder代码

internal static void PaintTableCellBorder(TableLayoutPanelCellBorderStyle borderStyle, Graphics g, Rectangle bound)
{
switch (borderStyle)
{
case TableLayoutPanelCellBorderStyle.None:
return;

case TableLayoutPanelCellBorderStyle.Single:
g.DrawRectangle(SystemPens.ControlDark, bound);
return;

case TableLayoutPanelCellBorderStyle.Inset:
using (Pen pen = new Pen(SystemColors.Window))
{
g.DrawLine(pen, bound.X, bound.Y, (bound.X + bound.Width) - 1, bound.Y);
g.DrawLine(pen, bound.X, bound.Y, bound.X, (bound.Y + bound.Height) - 1);
}
g.DrawLine(SystemPens.ControlDark, (bound.X + bound.Width) - 1, bound.Y, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
g.DrawLine(SystemPens.ControlDark, bound.X, (bound.Y + bound.Height) - 1, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
return;

case TableLayoutPanelCellBorderStyle.InsetDouble:
g.DrawRectangle(SystemPens.Control, bound);
bound = new Rectangle(bound.X + 1, bound.Y + 1, bound.Width - 1, bound.Height - 1);
using (Pen pen2 = new Pen(SystemColors.Window))
{
g.DrawLine(pen2, bound.X, bound.Y, (bound.X + bound.Width) - 1, bound.Y);
g.DrawLine(pen2, bound.X, bound.Y, bound.X, (bound.Y + bound.Height) - 1);
}
g.DrawLine(SystemPens.ControlDark, (bound.X + bound.Width) - 1, bound.Y, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
g.DrawLine(SystemPens.ControlDark, bound.X, (bound.Y + bound.Height) - 1, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
return;

case TableLayoutPanelCellBorderStyle.Outset:
{
g.DrawLine(SystemPens.ControlDark, bound.X, bound.Y, (bound.X + bound.Width) - 1, bound.Y);
g.DrawLine(SystemPens.ControlDark, bound.X, bound.Y, bound.X, (bound.Y + bound.Height) - 1);
using (Pen pen3 = new Pen(SystemColors.Window))
{
g.DrawLine(pen3, (bound.X + bound.Width) - 1, bound.Y, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
g.DrawLine(pen3, bound.X, (bound.Y + bound.Height) - 1, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
return;
}
}
case TableLayoutPanelCellBorderStyle.OutsetDouble:
case TableLayoutPanelCellBorderStyle.OutsetPartial:
g.DrawRectangle(SystemPens.Control, bound);
bound = new Rectangle(bound.X + 1, bound.Y + 1, bound.Width - 1, bound.Height - 1);
g.DrawLine(SystemPens.ControlDark, bound.X, bound.Y, (bound.X + bound.Width) - 1, bound.Y);
g.DrawLine(SystemPens.ControlDark, bound.X, bound.Y, bound.X, (bound.Y + bound.Height) - 1);
using (Pen pen4 = new Pen(SystemColors.Window))
{
g.DrawLine(pen4, (bound.X + bound.Width) - 1, bound.Y, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
g.DrawLine(pen4, bound.X, (bound.Y + bound.Height) - 1, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
}
return;

default:
return;
}
}



uncleson88 2012-05-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
自己继承一个,重载 OnPaintBackground 方法,在里面处理
[/Quote]

Single的边框我还能写出来,如果是其他的有3D效果的。。。能给点方法吗?
花痴 2012-05-28
  • 打赏
  • 举报
回复
边框可以不使用tablelayoutpanel的边框线,你在需要显示边框的时候,可以在一个单元格内放入一个Panel等容器,使用容易的边框线就可以了喔
bdmh 2012-05-28
  • 打赏
  • 举报
回复
自己继承一个,重载 OnPaintBackground 方法,在里面处理

110,533

社区成员

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

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

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