Source

SharpHammer / UIcomp / HeaderBox.cs

Full commit
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace HmrMControls
{
	public partial class HeaderBox : UserControl
	{
		private static StringFormat text_format = new StringFormat();
		private Pen border_pen = new Pen(Color.Black, 1);
		private Color color1 = Color.Silver;
		private Color color2 = Color.Gray;
		private string header_text = "";
		private Image image;

		public HeaderBox()
		{
			InitializeComponent();
			DoubleBuffered = true;
			if (text_format != null) {
				text_format = new StringFormat();
				text_format.LineAlignment = StringAlignment.Center;
			}
		}

		public Color BorderColor
		{
			get
			{
				return border_pen.Color;
			}
			set
			{
				border_pen.Color = value;
				Invalidate();
			}
		}

		public Color Color1
		{
			get
			{
				return color1;
			}
			set
			{
				color1 = value;
				Invalidate();
			}
		}

		public Color Color2
		{
			get
			{
				return color2;
			}
			set
			{
				color2 = value;
				Invalidate();
			}
		}

		public string HeaderText
		{
			get
			{
				return header_text;
			}
			set
			{
				header_text = value;
				Invalidate();
			}
		}

		public Image Image
		{
			get
			{
				return image;
			}
			set
			{
				image = value;
				Invalidate();
			}
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);

			var backBrush = new LinearGradientBrush(ClientRectangle, color1, color2, 90f);
			e.Graphics.FillRectangle(backBrush, ClientRectangle);
			Rectangle tmp_rect = ClientRectangle;
			tmp_rect.Width -= 1;
			tmp_rect.Height -= 1;
			e.Graphics.DrawRectangle(border_pen, tmp_rect);
			int text_x = 3;
			if (image != null) {
				e.Graphics.DrawImage(image, 3, 3, image.Width, image.Height);
				text_x += image.Width;
			}
			var text_brush = new SolidBrush(ForeColor);
			e.Graphics.DrawString(header_text, Font, text_brush, text_x, Height / 2, text_format);

			text_brush.Dispose();
			backBrush.Dispose();
		}
	}
}