Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Created July 13, 2018 23:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjs3339/ab35655b0c8f6579887e8e2303c404ee to your computer and use it in GitHub Desktop.
Save mjs3339/ab35655b0c8f6579887e8e2303c404ee to your computer and use it in GitHub Desktop.
C# - Custom GroupBox Component
public class CustomGroupBox : Panel
{
public enum BorderStyles
{
GroupBox = 0,
Panel
}
public enum HeaderMode
{
None = 0,
Standard
}
/// <summary>
/// Specifies how the title should be justified
/// </summary>
public enum HeaderTextJustify
{
Right = 0,
Left,
Center
}
private Color _BorderColor = Color.Cyan;
private bool _BorderDraw;
private BorderStyles _BorderStyle = BorderStyles.Panel;
private int _BorderWidth = 1;
private string _HeaderText = "";
private Color _HeaderTextBackColor = Color.DodgerBlue;
private HeaderMode _HeaderTextDrawingMode = HeaderMode.None;
private Font _HeaderTextFont;
private HeaderTextJustify _HeaderTextJustify = HeaderTextJustify.Left;
private IContainer components;
public CustomGroupBox()
{
InitializeComponent();
SetStyle(
ControlStyles.UserPaint | ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer,
true);
_HeaderTextFont = Font;
}
/// <summary>
/// The border style
/// </summary>
[Description("The border style")]
public BorderStyles BorderStyleEx
{
get => _BorderStyle;
set
{
_BorderStyle = value;
Invalidate(true);
}
}
/// <summary>
/// The text(header) to display
/// </summary>
[Description("The text(header) to display")]
public string HeaderText
{
get => _HeaderText;
set
{
_HeaderText = value;
Invalidate(true);
}
}
/// <summary>
/// Gets or Sets a value determine how to display Percentage value
/// </summary>
[Category("Behavior")]
[Description("Specify if text is displayed")]
public HeaderMode HeaderTextMode
{
get => _HeaderTextDrawingMode;
set
{
_HeaderTextDrawingMode = value;
Invalidate(true);
}
}
/// <summary>
/// Get or Set the title justification
/// </summary>
[Category("Behavior")]
[Description("Get or Set the title justification")]
public HeaderTextJustify HeaderTextJustification
{
get => _HeaderTextJustify;
set
{
_HeaderTextJustify = value;
Invalidate(true);
}
}
/// <summary>
/// sets the background color for drawing text
/// </summary>
[Category("Behavior")]
[Description("sets the background color for drawing text")]
public Color HeaderTextBackColor
{
get => _HeaderTextBackColor;
set
{
_HeaderTextBackColor = value;
Invalidate(true);
}
}
/// <summary>
/// Sets the color of the border
/// </summary>
[Description("Sets the color of the border")]
public Color BorderColor
{
get => _BorderColor;
set
{
_BorderColor = value;
Invalidate();
}
}
/// <summary>
/// Enable or disable the border
/// </summary>
[Description("Enable or disable the border")]
public bool BorderDraw
{
get => _BorderDraw;
set
{
_BorderDraw = value;
Invalidate();
}
}
/// <summary>
/// Adjusts the border width. Maximum=4px
/// </summary>
[Description("Adjusts the border width. Maximum=4px")]
public int BorderWidth
{
get => _BorderWidth;
set
{
_BorderWidth = value;
if(_BorderWidth > 4)
_BorderWidth = 4;
Invalidate();
}
}
private void InitializeComponent()
{
components = new Container();
SuspendLayout();
BackColor = Color.DodgerBlue;
DoubleBuffered = true;
Name = "CustomGroupBox";
_HeaderText = Name;
Size = new Drawing.Size(100, 100);
ResumeLayout(false);
}
protected override void Dispose(bool disposing)
{
if(disposing && components != null) components.Dispose();
base.Dispose(disposing);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
_HeaderTextFont = Font;
e.Graphics.FillRectangle(new SolidBrush(BackColor), 0, 0, Width, Height);
if(_BorderDraw)
{
if(_BorderStyle == BorderStyles.Panel)
e.Graphics.DrawRectangle(
new Pen(new SolidBrush(_BorderColor), _BorderWidth),
0,
0,
Width - 1,
Height - 1);
else
try
{
var s = e.Graphics.MeasureString("A", _HeaderTextFont);
var XOffset = _BorderWidth * 2;
var YOffset = s.Height / 2 + _BorderWidth;
e.Graphics.DrawRectangle(
new Pen(new SolidBrush(_BorderColor), _BorderWidth),
XOffset,
YOffset,
Width - XOffset * 2 - 1,
Height - YOffset - _BorderWidth - 1);
}
catch
{
}
}
if(_HeaderTextDrawingMode == HeaderMode.Standard)
using(_HeaderTextFont = new Font(_HeaderTextFont.Name, _HeaderTextFont.Size, _HeaderTextFont.Style, GraphicsUnit.Point))
{
var WritingPen = new Pen(ForeColor) {Brush = new SolidBrush(Color.FromArgb(255, ForeColor))};
if(_HeaderTextDrawingMode == HeaderMode.None) return;
var s = e.Graphics.MeasureString(_HeaderText, _HeaderTextFont);
switch(_HeaderTextJustify)
{
case HeaderTextJustify.Right:
{
var XOffset = Width - s.Width - 8;
var rectr = new Rectangle((int) XOffset, _BorderWidth, (int) s.Width, (int) s.Height);
e.Graphics.FillRectangle(new SolidBrush(_HeaderTextBackColor), rectr);
e.Graphics.DrawString(_HeaderText, _HeaderTextFont, WritingPen.Brush, XOffset + 1, _BorderWidth);
break;
}
case HeaderTextJustify.Center:
{
var XOffset = (float) Width / 2 - s.Width / 2;
var rectc = new Rectangle((int) XOffset, _BorderWidth, (int) s.Width, (int) s.Height);
e.Graphics.FillRectangle(new SolidBrush(_HeaderTextBackColor), rectc);
e.Graphics.DrawString(_HeaderText, _HeaderTextFont, WritingPen.Brush, XOffset + 1, _BorderWidth);
break;
}
case HeaderTextJustify.Left:
{
var rectl = new Rectangle(8, _BorderWidth, (int) s.Width, (int) s.Height);
e.Graphics.FillRectangle(new SolidBrush(_HeaderTextBackColor), rectl);
e.Graphics.DrawString(_HeaderText, _HeaderTextFont, WritingPen.Brush, 10f, _BorderWidth);
break;
}
default:
var rectd = new Rectangle(8, _BorderWidth, (int) s.Width, (int) s.Height);
e.Graphics.FillRectangle(new SolidBrush(_HeaderTextBackColor), rectd);
e.Graphics.DrawString(_HeaderText, _HeaderTextFont, WritingPen.Brush, 10f, _BorderWidth);
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment