Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Horizonal and Vertical Rules using System.Drawing Graphics Object

0.00/5 (No votes)
28 Oct 2003 1  
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