Click here to Skip to main content
15,919,245 members
Articles / Programming Languages / C#
Article

Horizonal and Vertical Rules using System.Drawing Graphics Object

Rate me:
Please Sign up or sign in to vote.
3.21/5 (14 votes)
28 Oct 2003 77.8K   962   19   8
This article discusses the implemenation of horizontal and vertical rules for Windows Forms by utilizing the System.Drawing Graphics object.

Introduction

Although the article by Stephen Quattlebaum (Wrapping Win32 Controls in .NET - Horizontal and Vertical Rules) is an excellent example, I wanted to find a way to draw horizontal and vertical rules using the System.Drawing namespace.

Using the code

I first created a sealed class called drawRules. In this class, there are two public classes horizontalRule and verticalRule that inherit from Control and utilize the parameter PaintEventArgs. The hardest part of the implementation was trying to find the best color for the gray line and I finally stumbled across this color System.Drawing.KnownColor.ControlDark. Basically a gray line is drawn and then a white line is drawn off by one position to make it appear 3D like.

public class horizontalRule : Control
{
    /// <summary>
    /// Draw a Horizontal Rule
    /// </summary>
    public horizontalRule(PaintEventArgs e, int x, int y, int width)
    {
        if (width < 0) {width = 0;}
        Graphics grfx = e.Graphics;
        Pen penGray = new 
            Pen(Color.FromKnownColor
            (System.Drawing.KnownColor.ControlDark),1);
        Pen penWhite = new Pen(Color.WhiteSmoke,1);
    
        grfx.DrawLine(penGray, x, y, x+width, y);
        grfx.DrawLine(penWhite, x, y+1, x+width, y+1);
    }
}

public class verticalRule : Control
{
    /// <summary>
    /// Draw a Vertical Rule
    /// </summary>
    public verticalRule(PaintEventArgs e, int x, int y, int height)
    {
        if (height < 0) {height = 0;}
        Graphics grfx = e.Graphics;
        Pen penGray = new Pen(Color.FromKnownColor
                        (System.Drawing.KnownColor.ControlDark),1);
        Pen penWhite = new Pen(Color.WhiteSmoke,1);
    
        grfx.DrawLine(penGray, x, y, x, y+height);
        grfx.DrawLine(penWhite, x+1, y, x+1, y+height);
    }
}

In the form, I had to override the OnPaint method and create the horizontal and vertical rules within this method.

/// <summary>
/// OnPaint event.
/// </summary>
protected override void OnPaint(PaintEventArgs e)
{
    // Horizontal Rules
    drawRules.horizontalRule hr1 = new drawRules.horizontalRule(e,10,40,250);
    drawRules.horizontalRule hr2 = new drawRules.horizontalRule(e,20,65,100);
    drawRules.horizontalRule hr3 = new drawRules.horizontalRule(e,40,80,200);

    // Vertical Rules
    drawRules.verticalRule vr1 = new drawRules.verticalRule(e,30,130,60);
    drawRules.verticalRule vr2 = new drawRules.verticalRule(e,60,150,90);
    drawRules.verticalRule vr3 = new drawRules.verticalRule(e,80,180,30);
}

Points of interest

A future implementation would be to handle resizing of the rules when the form is resized.

History

  • October 28, 2003 - Initial release.

    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


    Written By
    Web Developer
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralMy vote of 5 Pin
    proxact2-Mar-12 0:55
    proxact2-Mar-12 0:55 
    Generalgreat idea! [modified] Pin
    Leblanc Meneses15-Jul-06 14:56
    Leblanc Meneses15-Jul-06 14:56 
    GeneralRe: great idea! [modified] Pin
    Leblanc Meneses15-Jul-06 15:15
    Leblanc Meneses15-Jul-06 15:15 
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;

    namespace GUIControls
    {
    public partial class DrawCrossRule : DrawRuleBase
    {
    public DrawCrossRule()
    {
    this.SuspendLayout();
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
    this.Name = "DrawCrossRule";
    this.Size = new System.Drawing.Size(50, 50);
    this.ResumeLayout(false);
    }


    protected override void OnPaint(PaintEventArgs e)
    {
    Graphics grfx = e.Graphics;
    Pen penGray = new Pen(Color.FromKnownColor(System.Drawing.KnownColor.ControlDark), 1);
    Pen penWhite = new Pen(Color.WhiteSmoke, 1);

    //
    // Verticle Rule
    //
    float x = this.ClientRectangle.X + this.ClientRectangle.Width / 2;
    float y = this.ClientRectangle.Y;
    float height = this.ClientRectangle.Height;

    if (height < 0) {
    height = 0;
    }

    grfx.DrawLine(penGray, x, y, x, y + height);
    grfx.DrawLine(penWhite, x + 1, y, x + 1, y + height);

    //
    // Horizontal Rule
    //
    x = this.ClientRectangle.X;
    y = this.ClientRectangle.Y + this.ClientRectangle.Height / 2;
    float width = this.ClientRectangle.Width;

    if (width < 0)
    {
    width = 0;
    }

    grfx.DrawLine(penGray, x, y, x + width, y);
    grfx.DrawLine(penWhite, x, y + 1, x + width, y + 1);


    penGray.Dispose();
    penWhite.Dispose();
    }
    }
    }


    note DrawRuleBase extends UserControl

    Leblanc Meneses
    http://www.blogsyndrome.com
    http://www.robusthaven.com
    GeneralHmmm Pin
    bierdopje5-Nov-03 21:45
    bierdopje5-Nov-03 21:45 
    GeneralOh sure... Pin
    Stephen Quattlebaum5-Nov-03 5:32
    Stephen Quattlebaum5-Nov-03 5:32 
    QuestionWhy not create as a UserControl? Pin
    Daren May5-Nov-03 5:31
    Daren May5-Nov-03 5:31 
    AnswerRe: Why not create as a UserControl? Pin
    lagay5-Nov-03 6:13
    lagay5-Nov-03 6:13 
    GeneralRe: Why not create as a UserControl? Pin
    Daren May5-Nov-03 6:24
    Daren May5-Nov-03 6:24 

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

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