以前用delphi开发软件的时候,如果需要在界面上放置一个分隔线,其自带的tbevel可以很好地满足这一要求。现在转到了.net平台,竟找不到对应的控件(毕竟每次在paint事件中画太麻烦)。而如果用panel、groupbox、label来间接实现,效果不够理想,并且感觉怪怪的。因此决定自己实现一个分隔线控件,效果如下图所示:
在这里我只将核心代码贴出来,关于创建自定义控件的相关知识可参考msdn:
ms-help://ms.vscc.2003/ms.msdnqtr.2003feb.2052/vbcon/html/vbconcontrolcreation.htm
/// <summary>
/// 水平分隔线
/// </summary>
[
toolboxbitmap(typeof(lineh), "res.lineh.ico"),
designer(typeof(linehdesigner))
]
public class lineh : system.windows.forms.usercontrol
{
//... ...
private void lineh_paint(object sender, system.windows.forms.painteventargs e)
{
graphics g = e.graphics;
rectangle r = this.clientrectangle;
pen darkpen = new pen(systemcolors.controldark, 1);
pen lightpen = new pen(color.white);
//用暗色调处理上边缘
g.drawline(darkpen, r.left, r.top, r.right, r.top);
//用亮色调处理下边缘
g.drawline(lightpen, r.left, r.top + 1, r.right, r.top + 1);
}
//... ...
}
/// <summary>
/// 垂直分隔线
/// </summary>
[
toolboxbitmap(typeof(linev), "res.linev.ico"),
designer(typeof(linevdesigner))
]
public class linev : system.windows.forms.usercontrol
{
//... ...
private void linev_paint(object sender, system.windows.forms.painteventargs e)
{
graphics g = e.graphics;
rectangle r = this.clientrectangle;
pen darkpen = new pen(systemcolors.controldark, 1);
pen lightpen = new pen(color.white);
//用暗色调处理左边缘
g.drawline(darkpen, r.left, r.top, r.left, r.bottom);
//用亮色调处理右边缘
g.drawline(lightpen, r.left + 1, r.top, r.left + 1, r.bottom);
}
//... ...
}
欲完全实现上图所示效果,可参阅http://blog.csdn.net/doubon/archive/2005/01/18/258314.aspx。