Drawing Lines Using the Pen Class


The first example here draws lines. In the following Try It Out, you draw lines using the Pen class, which allows you to define the color, width, and pattern of the line that your code is drawing. The color and width properties are obvious. However, the pattern of a line indicates whether the line is a solid line, or is composed of dashes and dots. the Pen class is in the System.Drawing namespace.

Try It Out – Pen Example

image from book

Follow these steps to draw some lines in a window using the Pen class.

  1. Create a new Windows application called DrawingLines in the directory C:\BegVCSharp\ Chapter30.

  2. Enter the following code into the body of Form1.

     protected override void OnPaint(PaintEventArgs e)  { Graphics g = e.Graphics; using (Pen blackPen = new Pen(Color.Black, 1))  { if (ClientRectangle.Height/10>0) { for (int y = 0; y < ClientRectangle.Height; y += ClientRectangle.Height / 10) { g.DrawLine(blackPen, new Point(0, 0), new Point(ClientRectangle.Width, y)); } } } } 
  3. Now press F5 to compile and run the code. When you run it, it will create the window shown in Figure 30-9.

    image from book
    Figure 30-9

How It Works

Earlier in the chapter, I introduced the Graphics class. The first thing that you do in the OnPaint() method is to get the Graphics object from the PaintEventArgs parameter:

Graphics g = e.Graphics;

Note that because you are passed the reference to the Graphics object, and you did not create it, you do not need to (and should not) manually call Dispose() on it. However, since you are using a potentially resource-hungry Pen object for this example, I have wrapped the rest of the code in a using block, as described earlier, which will ensure that the object is destroyed as soon as possible.

When you construct the pen, you pass as parameters to the constructor a color and a width of the pen. In this example, the color is black, and the width is one. This is the line of code to construct the pen:

using (Pen blackPen = new Pen(Color.Black, 1))

Every window into which you can draw has a client area, which is a rectangle that exists within the border and defines the exact area into which you can draw. You can get the client area from ClientRectangle, which is a public, read-only property of the form (inherited from Control). It contains the size (that is, the width and height) of the client area of the window into which you are drawing. The following code starts a loop that goes from zero up to the height of the client area (given by ClientRectangle.Height) in steps of 10. Note that you first check that ClientRectangle.Height/10 is bigger than zero — without this, the loop will run indefinitely if the form is resized below a certain height. (Because ClientRectangle.Height/10 is the loop increment, if this is zero, you'll loop forever.)

if (ClientRectangle.Height/10>0) {    for (int y = 0; y < ClientRectangle.Height;         y += ClientRectangle.Height / 10)

Now you can draw the lines — when you draw each line, you pass the Pen that you just created, along with the starting point and ending point of the line:

g.DrawLine(blackPen, new Point(0, 0),            new Point(ClientRectangle.Width, y));

Important

Always call Dispose( ) on Pen objects.

Just as for Graphics objects, it is important to either call Dispose() on Pen objects when you are finished with them, or use the using block, otherwise your application can deplete the Windows resources.

In this example, you constructed a Pen. However, there is an easier way to get a Pen. The Pens class contains properties for getting approximately 150 pens, one for each of the predefined colors that you learned about previously. The following version of the example works identically to the previous one, but instead of constructing a Pen, you get it from the Pens class:

protected override void OnPaint(PaintEventArgs e) {    if (ClientRectangle.Height/10>0)    { for (int y = 0; y < ClientRectangle.Height; y += ClientRectangle.Height / 10) {          e.Graphics.DrawLine(Pens.Black,new Point(0, 0), new Point(ClientRectangle.Width, y)); }    } } 

In this case, you did not create the Pen, so it is not necessary to call Dispose().

There are many more features of the Pen class. You could create a pen to draw a dashed line, or you could create a pen with a width thicker than 1 pixel. There is an Alignment property of the Pen class that allows you to define whether the pen is drawn to the left or right (or above or below) of the line that you specify. By setting the StartCap and EndCap properties, you can specify that your lines are ended with an arrow, a diamond, a square, or are rounded off. You can even program a custom start cap and end cap using the CustomStartCap and CustomEndCap properties. After learning about images, you will see how to specify a Brush with a Pen so that you can draw the line using a bitmap instead of a solid color.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net