Wednesday, November 21, 2007

Outlook Style Groupbox Control

Introduction
This article explains very simple implementation of a outlook style groupbox. This control also supports transparent background.


Features
Following are the customizable things in this control
  • Groupbox border color
  • Back image or icon

Transparent background

This is achieved by using follwing code.
bool _isTransparent = false;
[Browsable(true), Category("Appearance")]
public bool IsTransparent
{
get { return _isTransparent; }
set
{
_isTransparent = value;
if (_isTransparent == true)
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
}
Invalidate();
}
}

Source Code (Download here)

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Owf.Controls
{
public partial class OutlookGroupBox : Panel
{
[Browsable(true)]
public new string Text
{
get { return base.Text; }
set
{
base.Text = value;
Invalidate();
}
}

private Color _lineColor = SystemColors.Highlight;
[Browsable(true), Category("Appearance")]
public Color LineColor
{
get { return _lineColor; }
set
{
_lineColor = value;
Invalidate();
}
}

private Image _image;
[Browsable(true), Category("Appearance")]
public Image Image
{
get { return _image; }
set
{
if (value != null)
{
_image = value;
_icon = null;
Invalidate();
}
}
}

private Icon _icon;
[Browsable(true), Category("Appearance")]
public Icon Icon
{
get { return _icon; }
set
{
if (value != null)
{
_icon = value;
_image = null;
Invalidate();
}
}
}

bool _isTransparent = false;
[Browsable(true), Category("Appearance")]
public bool IsTransparent
{
get { return _isTransparent; }
set
{
_isTransparent = value;
if (_isTransparent == true)
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
}
Invalidate();
}
}

public OutlookGroupBox()
{
InitializeComponent();
SetStyles();
}

public OutlookGroupBox(IContainer container)
{
container.Add(this);
InitializeComponent();
SetStyles();
}

void SetStyles()
{
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}

private void OutlookGroupBox_Paint(object sender, PaintEventArgs e)
{
Font font = this.Font;
SizeF size = e.Graphics.MeasureString(this.Text, font);

e.Graphics.DrawString(this.Text, font, new SolidBrush(this.ForeColor), 1, 1);
e.Graphics.DrawLine(new Pen(_lineColor), size.Width + 3, (size.Height + 3) / 2,
this.Width - 5, (size.Height + 3) / 2);

if (_image != null)
{
e.Graphics.DrawImageUnscaled(_image, this.Padding.Left, this.Padding.Top);
}
else if (_icon != null)
{
e.Graphics.DrawIconUnstretched(_icon, new Rectangle(this.Padding.Left,
this.Padding.Top, _icon.Width, _icon.Height));
}
}
}
}

No comments:

Post a Comment