Click here to Skip to main content
Licence 
First Posted 28 Oct 2003
Views 53,416
Bookmarked 19 times

Horizonal and Vertical Rules using System.Drawing Graphics Object

By | 28 Oct 2003 | Article
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

    About the Author

    lagay

    Web Developer

    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
    GeneralMy vote of 5 Pinmemberproxact0:55 2 Mar '12  
    Generalgreat idea! [modified] PinmemberLeblanc Meneses14:56 15 Jul '06  
    GeneralRe: great idea! [modified] PinmemberLeblanc Meneses15:15 15 Jul '06  
    GeneralHmmm Pinmemberbierdopje21:45 5 Nov '03  
    GeneralOh sure... PinmemberStephen Quattlebaum5:32 5 Nov '03  
    QuestionWhy not create as a UserControl? PinmemberDaren May5:31 5 Nov '03  
    AnswerRe: Why not create as a UserControl? Pinmemberlagay6:13 5 Nov '03  
    GeneralRe: Why not create as a UserControl? PinmemberDaren May6:24 5 Nov '03  

    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 29 Oct 2003
    Article Copyright 2003 by lagay
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid