C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法
1.新建组件这里可以自定义一个Panel控件起名为PanelEx
2.增加一个BoderColor属性和BoderSize属性
private Color _BorderColor = Color.Black; [Browsable(true), Description("边框颜色"), Category("自定义分组")]
public Color BorderColor
{
get { return _BorderColor; }
set
{
_BorderColor = value;
this.Invalidate();
}
} private int _BorderSize = ; [Browsable(true), Description("边框粗细"), Category("自定义分组")]
public int BorderSize
{
get { return _BorderSize; }
set
{
_BorderSize = value;
this.Invalidate();
}
}
3.控件代码继承Panel并重写OnPaint方法
/// <summary>
/// 重写OnPaint方法
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics,
this.ClientRectangle,
this._BorderColor,
this._BorderSize,
ButtonBorderStyle.Solid,
this._BorderColor,
this._BorderSize,
ButtonBorderStyle.Solid,
this._BorderColor,
this._BorderSize,
ButtonBorderStyle.Solid,
this._BorderColor,
this._BorderSize,
ButtonBorderStyle.Solid);
}
4.添加测试窗体并添加PanelEx控件
编译运行测试