65.9K
CodeProject is changing. Read more.
Home

Office 2003 Line Control

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.90/5 (10 votes)

Aug 18, 2003

CPOL

1 min read

viewsIcon

104827

downloadIcon

2512

A GroupBox-like single line similar to that found in Office 2003.

Offce 2003 Line Control

Introduction

These controls mimic that of Office 2003 where they have a Label or GroupBox with a line next to it This is my first control so please comment and give suggestions on how to do things better, etc.

Using the Code

Basically what I did was override the OnPaint events to get the desired look like this:

protected override voi OnPaint(System.Windows.Forms.PaintEventArgs e)
{ 
    Graphics g = e.Graphics; SolidBrush brush =
        new SolidBrush(this.textColor);
    Pen pen =new Pen(this.lineColor);
    SizeF sizef = g.MeasureString(this.Text, this.Font);
    PointF pointf = new PointF(0, 0);
  
    g.DrawString(this.Text, this.Font, brush, pointf);
    g.DrawLine(pen, sizef.Width + 8, sizef.Height / 2, this.Width - 8,
        sizef.Height / 2);
}

After that I created a custom designer so I can control what it looks like in design mode.

public class GroupBoxLineDesigner : ParentControlDesigner
{
  protected override void PostFilterProperties(IDictionary id)
  {
    id.Remove("FlatStyle");
    base.PostFilterProperties(id);
  }

  protected override void OnPaintAdornments(PaintEventArgs pe)
  {
    this.DrawBorder(pe.Graphics);
    base.OnPaintAdornments(pe);
  }

  private void DrawBorder(Graphics graphics)
  {
    Control control1 = base.Control;
    Rectangle rectangle1 = control1.ClientRectangle;
    Color borderColor;
    Color backColor = control1.BackColor;
    if (((double) backColor.GetBrightness()) < 0.5)
    {
      borderColor = ControlPaint.Light(control1.BackColor);
    }
    else
    {
      borderColor = ControlPaint.Dark(control1.BackColor);
    }
    Pen pen = new Pen(borderColor);
    pen.DashPattern = new float[] {3, 1};
    rectangle1.Width--;
    rectangle1.Height--;
    graphics.DrawRectangle(pen, rectangle1);
    pen.Dispose();
  } 
}

[Designer(typeof(GroupBoxLineDesigner))]
...

And here's all code put together:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Newco.Controls
{
  /// <SUMMARY>
  /// Summary description for GroupBoxLine.
  /// </SUMMARY>
  
  public class GroupBoxLineDesigner : ParentControlDesigner
  {
    protected override void PostFilterProperties(IDictionary id)
    {
      id.Remove("FlatStyle");
      base.PostFilterProperties(id);
    }
    protected override void OnPaintAdornments(PaintEventArgs pe)
    {
      this.DrawBorder(pe.Graphics);
      base.OnPaintAdornments(pe);
    }

    private void DrawBorder(Graphics graphics)
    {
      Control control1 = base.Control;
      Rectangle rectangle1 = control1.ClientRectangle;
      Color borderColor;
      Color backColor = control1.BackColor;

      if (((double) backColor.GetBrightness()) < 0.5)
      {
        borderColor = ControlPaint.Light(control1.BackColor);
      }
      else
      {
        borderColor = ControlPaint.Dark(control1.BackColor);
      }
      Pen pen = new Pen(borderColor);
      pen.DashPattern = new float[] {3, 1};
      rectangle1.Width--;
      rectangle1.Height--;
      graphics.DrawRectangle(pen, rectangle1);
      pen.Dispose();
    } 
  }

  [Designer(typeof(GroupBoxLineDesigner))]
  [ToolboxBitmap(typeof(GroupBoxLine), @"Toolbox_GroupBoxLine.bmp")]
  public class GroupBoxLine : System.Windows.Forms.GroupBox
  {
    protected Color lineColor;
    protected Color textColor;
    private System.ComponentModel.Container components = null;

    // Add the 'LineColor' property
    [Description("The line color."),
    Category("Appearance")]
    public Color LineColor
    {
      get
      {
        return this.lineColor;
      }
      set
      {
        this.lineColor = value;
        Invalidate();
      }
    }

    // Add the 'TextColor' property
    [Description("The text color."),
    Category("Appearance")]
    public Color TextColor
    {
      get
      {
        return this.textColor;
      }
      set
      {
        this.textColor = value;
        Invalidate();
      }
    }

    public GroupBoxLine()
    {
      this.textColor = Color.FromArgb(0, 70, 213);
      this.lineColor = Color.FromArgb(208, 208, 191);
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      SolidBrush brush = new SolidBrush(this.textColor);
      Pen pen = new Pen(this.lineColor);
      SizeF sizef = g.MeasureString(this.Text, this.Font);
      PointF pointf = new PointF(0, 0);

      g.DrawString(this.Text, this.Font, brush, pointf);
      g.DrawLine(pen, sizef.Width, sizef.Height / 2, this.Width,
        sizef.Height / 2);

      brush.Dispose();
      pen.Dispose();
    }

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if( components != null )
          components.Dispose();
      }
      base.Dispose( disposing );
    }
  }
}

Points of Interest

Like I said before - this is my first control and I think it turned out rather well, but I know there's better ways to do this I've also noticed that if you change the FlatStyle property then the control goes back to the original form. What can I do to fix this? And is the GroupBox is hard to see in design mode so is there a way to draw a rectangle in design mode only? Thanks for helping me out everyone and I hope I learn something so I can post more articles to codeproject. Thanks, Rob Tomson

Updates

  • 08/18//2003: Initial Release
  • 08/20/2003: Added a 'design time' interface.