感觉Winform自带的GroupBox有点丑,而且用着有点呆,很难满足我们的需求,所以就在原来的基础上进行一点拓展。
先看效果
控件的自定义属性
实现了改变边框的颜色,标题颜色和内部颜色,以及标题文字的位置的功能。
主要实现:
在OnPaint事件内,先清空原来的,然后再绘制出边框线以及标题文字。
注意标题文字的位置,在上下两条边上还好,在左右两条边上时,可以进行坐标的旋转,这里注意一下旋转的方向即可。其他估计也没什么难点。
所有代码附上:
///
/// 拓展GroupBox
///
[ToolboxBitmap(typeof(System.Windows.Forms.GroupBox))]
[Description("拓展GroupBox, 可以设置边框、标题、字体等")]
public partial class yGroupBox : GroupBox
{
#region 构造函数
///
/// 拓展GroupBox
///
public yGroupBox()
{
InitializeComponent();
}
#endregion
#region 变量定义
private Color borderColor = Color.MediumSlateBlue;//边框颜色
private int borderSize = 2;//边框粗细
private Color contentColor = Color.Black;//标题颜色
private ContentPosition contentPosition = ContentPosition.Top_Left;//标题位置
private Font contentFont = new Font("微软雅黑", 9.5F);//标题字体
private string context = "标题文字";//标题文字
// 标志位 用来判断是初次将控件拖进窗体还是后面修改text
// 如果是初次拖入窗体 则 将text赋值为控件名 否则就是自己外部输入的text
private bool flag = true;
#endregion
#region 自定属性
///
/// 边框颜色
///
[Category("1、自定义属性"), Description("边框颜色")]
public Color BorderColor
{
get
{
return borderColor;
}
set
{
if (value.IsEmpty) value = Color.Black;
borderColor = value;
this.Invalidate();
}
}
///
/// 边框粗细(1-5)
///
[Category("1、自定义属性"), Description("边框粗细(1-5)")]
public int BorderSize
{
get
{
return borderSize;
}
set
{
if (value < 1) value = 1;
if (value > 5) value = 5;
borderSize = value;
this.Invalidate();
}
}
///
/// 标题颜色
///
[Category("1、自定义属性"), Description("标题颜色")]
public Color ContentColor
{
get
{
return contentColor;
}
set
{
if (value.IsEmpty)
value = Color.Black;
contentColor = value;
this.Invalidate();
}
}
///
/// 标题文字位置
///
[Category("1、自定义属性"), Description("标题文字位置")]
[Localizable(true)]
public ContentPosition ContentPosition
{
get
{
return contentPosition;
}
set
{
contentPosition = value;
this.Invalidate();
}
}
///
/// 标题字体
///
[Category("1、自定义属性"), Description("标题字体")]
public Font ContentFont
{
get
{
return contentFont;
}
set
{
value = value ?? new Font("微软雅黑", 9.5F);
contentFont = value;
this.Invalidate();
}
}
///
/// 背景颜色
///
[Category("1、自定义属性"), Description("背景颜色")]
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
///
/// 把原来的Text隐藏掉
///
[Browsable(false)]
public override string Text
{
get
{
return base.Text;
}
set
{
Text = value;
flag = false;
}
}
///
/// 标题
///
[Category("1、自定义属性"), Description("标题")]
public string Context
{
get
{
return context;
}
set
{
context = value;
flag = false;
this.Invalidate();
}
}
///
/// 背景字体颜色
///
[Category("1、自定义属性"), Description("背景字体颜色")]
public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
}
///
/// 背景字体
///
[Category("1、自定义属性"), Description("背景字体")]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
}
#endregion
#region 方法及重写
///
/// 重绘
///
///
protected override void OnPaint(PaintEventArgs e)
{
if (flag == true)
{
context = this.Name;
}
flag = false;
Graphics g = e.Graphics;
SizeF vSize = g.MeasureString(context, contentFont);
float textWidth = vSize.Width;
float textHeight = vSize.Height;
g.Clear(this.BackColor);
Matrix matrix = g.Transform;
using (SolidBrush contentBrush = new SolidBrush(contentColor))
using (Pen vPen = new Pen(borderColor, borderSize))
{
switch (contentPosition)
{
case ContentPosition.Top_Left:
g.DrawString(context, contentFont, contentBrush, 10, 0);
g.DrawLine(vPen, 1, textHeight / 2, 8, textHeight / 2);
g.DrawLine(vPen, textWidth + 8, textHeight / 2, this.Width - 1, textHeight / 2);
g.DrawLine(vPen, 2, textHeight / 2, 2, this.Height - 1);
g.DrawLine(vPen, 1, this.Height - 2, this.Width - 1, this.Height - 2);
g.DrawLine(vPen, this.Width - 2, textHeight / 2, this.Width - 2, this.Height - 1);
break;
case ContentPosition.Top_Center:
g.DrawString(context, contentFont, contentBrush, (this.Width - textWidth) / 2, 0);
g.DrawLine(vPen, 1, textHeight / 2, (this.Width - textWidth) / 2 - 2, textHeight / 2);
g.DrawLine(vPen, (this.Width + textWidth) / 2 + 2, textHeight / 2, this.Width - 1, textHeight / 2);
g.DrawLine(vPen, 2, textHeight / 2, 2, this.Height - 1);
g.DrawLine(vPen, 1, this.Height - 2, this.Width - 1, this.Height - 2);
g.DrawLine(vPen, this.Width - 2, textHeight / 2, this.Width - 2, this.Height - 1);
break;
case ContentPosition.Top_Right:
g.DrawString(context, contentFont, contentBrush, this.Width - textWidth - 10, 0);
g.DrawLine(vPen, 1, textHeight / 2, this.Width - textWidth - 12, textHeight / 2);
g.DrawLine(vPen, this.Width - 8, textHeight / 2, this.Width - 1, textHeight / 2);
g.DrawLine(vPen, 2, textHeight / 2, 2, this.Height - 1);
g.DrawLine(vPen, 1, this.Height - 2, this.Width - 1, this.Height - 2);
g.DrawLine(vPen, this.Width - 2, textHeight / 2, this.Width - 2, this.Height - 1);
break;
case ContentPosition.Left_Top:
matrix.RotateAt(-90, new PointF(0, 0));
g.Transform = matrix;
g.DrawString(context, contentFont, contentBrush, -textWidth - 8, 0);
matrix.RotateAt(90, new PointF(0, 0));
g.Transform = matrix;
g.DrawLine(vPen, textHeight / 2, 2, this.Width - 1, 2);
g.DrawLine(vPen, textHeight / 2, 1, textHeight / 2, 8);
g.DrawLine(vPen, textHeight / 2, textWidth + 12, textHeight / 2, this.Height - 1);
g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - 1);
g.DrawLine(vPen, textHeight / 2, this.Height - 2, this.Width - 1, this.Height - 2);
break;
case ContentPosition.Left_Center:
matrix.RotateAt(-90, new PointF(0, 0));
g.Transform = matrix;
g.DrawString(context, contentFont, contentBrush, -(this.Height + textWidth) / 2, 0);
matrix.RotateAt(90, new PointF(0, 0));
g.Transform = matrix;
g.DrawLine(vPen, textHeight / 2, 2, this.Width - 2, 2);
g.DrawLine(vPen, textHeight / 2, 1, textHeight / 2, (this.Height - textWidth) / 2 - 2);
g.DrawLine(vPen, textHeight / 2, (this.Height + textWidth) / 2 + 2, textHeight / 2, this.Height - 1);
g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - 1);
g.DrawLine(vPen, textHeight / 2, this.Height - 2, this.Width - 1, this.Height - 2);
break;
case ContentPosition.Left_Bottom:
matrix.RotateAt(-90, new PointF(0, 0));
g.Transform = matrix;
g.DrawString(context, contentFont, contentBrush, -this.Height + 10, 0);
matrix.RotateAt(90, new PointF(0, 0));
g.Transform = matrix;
g.DrawLine(vPen, textHeight / 2, 2, this.Width - 2, 2);
g.DrawLine(vPen, textHeight / 2, 1, textHeight / 2, this.Height - textWidth - 12);
g.DrawLine(vPen, textHeight / 2, this.Height - 8, textHeight / 2, this.Height - 1);
g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - 1);
g.DrawLine(vPen, textHeight / 2, this.Height - 2, this.Width - 1, this.Height - 2);
break;
case ContentPosition.Bottom_Left:
g.DrawString(context, contentFont, contentBrush, 10, this.Height - textHeight);
g.DrawLine(vPen, 1, 2, this.Width - 1, 2);
g.DrawLine(vPen, 2, 1, 2, this.Height - textHeight / 2);
g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - textHeight / 2);
g.DrawLine(vPen, 1, this.Height - textHeight / 2, 8, this.Height - textHeight / 2);
g.DrawLine(vPen, 12 + textWidth, this.Height - textHeight / 2, this.Width - 1, this.Height - textHeight / 2);
break;
case ContentPosition.Bottom_Center:
g.DrawString(context, contentFont, contentBrush, (this.Width - textWidth) / 2, this.Height - textHeight);
g.DrawLine(vPen, 1, 2, this.Width - 1, 2);
g.DrawLine(vPen, 2, 1, 2, this.Height - textHeight / 2);
g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - textHeight / 2);
g.DrawLine(vPen, 1, this.Height - textHeight / 2, (this.Width - textWidth) / 2 - 2, this.Height - textHeight / 2);
g.DrawLine(vPen, (this.Width + textWidth) / 2 + 2, this.Height - textHeight / 2, this.Width - 1, this.Height - textHeight / 2);
break;
case ContentPosition.Bottom_Right:
g.DrawString(context, contentFont, contentBrush, this.Width - textWidth - 10, this.Height - textHeight);
g.DrawLine(vPen, 1, 2, this.Width - 1, 2);
g.DrawLine(vPen, 2, 1, 2, this.Height - textHeight / 2);
g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - textHeight / 2);
g.DrawLine(vPen, 1, this.Height - textHeight / 2, this.Width - textWidth - 12, this.Height - textHeight / 2);
g.DrawLine(vPen, this.Width - 8, this.Height - textHeight / 2, this.Width - 1, this.Height - textHeight / 2);
break;
case ContentPosition.Right_Top:
matrix.RotateAt(90, new PointF(0, 0));
g.Transform = matrix;
g.DrawString(context, contentFont, contentBrush, 10, -this.Width);
matrix.RotateAt(-90, new PointF(0, 0));
g.Transform = matrix;
g.DrawLine(vPen, 1, 2, this.Width - textHeight / 2, 2);
g.DrawLine(vPen, 2, 1, 2, this.Height - 2);
g.DrawLine(vPen, this.Width - textHeight / 2, 1, this.Width - textHeight / 2, 8);
g.DrawLine(vPen, this.Width - textHeight / 2, textWidth + 12, this.Width - textHeight / 2, this.Height - 1);
g.DrawLine(vPen, 1, this.Height - 2, this.Width - textHeight / 2, this.Height - 2);
break;
case ContentPosition.Right_Center:
matrix.RotateAt(90, new PointF(0, 0));
g.Transform = matrix;
g.DrawString(context, contentFont, contentBrush, (this.Height - textWidth) / 2, -this.Width);
matrix.RotateAt(-90, new PointF(0, 0));
g.Transform = matrix;
g.DrawLine(vPen, 1, 2, this.Width - textHeight / 2, 2);
g.DrawLine(vPen, 2, 1, 2, this.Height - 2);
g.DrawLine(vPen, this.Width - textHeight / 2, 1, this.Width - textHeight / 2, (this.Height - textWidth) / 2 - 2);
g.DrawLine(vPen, this.Width - textHeight / 2, (this.Height + textWidth) / 2 + 2, this.Width - textHeight / 2, this.Height - 1);
g.DrawLine(vPen, 1, this.Height - 2, this.Width - textHeight / 2, this.Height - 2);
break;
case ContentPosition.Right_Bottom:
matrix.RotateAt(90, new PointF(0, 0));
g.Transform = matrix;
g.DrawString(context, contentFont, contentBrush, this.Height - textWidth - 10, -this.Width);
matrix.RotateAt(-90, new PointF(0, 0));
g.Transform = matrix;
g.DrawLine(vPen, 1, 2, this.Width - textHeight / 2, 2);
g.DrawLine(vPen, 2, 1, 2, this.Height - 2);
g.DrawLine(vPen, this.Width - textHeight / 2, 1, this.Width - textHeight / 2, this.Height - textWidth - 12);
g.DrawLine(vPen, this.Width - textHeight / 2, this.Height - 8, this.Width - textHeight / 2, this.Height - 1);
g.DrawLine(vPen, 1, this.Height - 2, this.Width - textHeight / 2, this.Height - 2);
break;
}
}
}
#endregion
}
///
/// 标题文字位置
///
public enum ContentPosition
{
///
/// 上边靠左
///
Top_Left = 0,
///
/// 上边中间
///
Top_Center,
///
/// 上边靠右
///
Top_Right,
///
/// 左边靠上
///
Left_Top,
///
/// 左边中间
///
Left_Center,
///
/// 左边靠下
///
Left_Bottom,
///
/// 下边靠左
///
Bottom_Left,
///
/// 下边中间
///
Bottom_Center,
///
/// 下边靠右
///
Bottom_Right,
///
/// 右边靠上
///
Right_Top,
///
/// 右边中间
///
Right_Center,
///
/// 右边靠下
///
Right_Bottom
}