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