Click here to Skip to main content
Licence 
First Posted 17 Aug 2003
Views 89,602
Bookmarked 54 times

Office 2003 Line Control

By | 21 Aug 2003 | Article
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Rob Tomson



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWell Done PinmemberME-Mike Elliott22:07 27 Jan '06  
GeneralIntegration PinsussM Y David2:55 16 Dec '04  
GeneralComplex Line Choice PinsussPoRniK [yoda]10:00 3 Sep '03  
GeneralRe: Complex Line Choice Pinmember""14:32 3 Sep '03  
GeneralRe: Complex Line Choice PinmemberPoRniK10:47 4 Sep '03  
GeneralVisual Styles PineditorHeath Stewart4:52 18 Aug '03  
GeneralRe: Visual Styles Pinmember""6:32 19 Aug '03  
GeneralRe: Visual Styles PineditorHeath Stewart8:04 19 Aug '03  
GeneralRe: Visual Styles Pinmember""17:17 19 Aug '03  
GeneralRe: Visual Styles PineditorHeath Stewart18:32 19 Aug '03  
GeneralRe: Visual Styles Pinmember""20:24 19 Aug '03  
GeneralRe: Visual Styles Pinmember""20:37 19 Aug '03  
GeneralRe: Visual Styles PineditorHeath Stewart2:16 20 Aug '03  
GeneralIntegration PinsussM Y David2:54 16 Dec '04  
GeneralIntegration PinsussM Y David2:54 16 Dec '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 22 Aug 2003
Article Copyright 2003 by Rob Tomson
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid