15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Drawing shapes <strong>and</strong> lines ❘ OC23<br />

Before we leave the subject of drawing simple objects, this section rounds off with a simple example<br />

that demonstrates the kinds of visual effects you can achieve using brushes. The example is called<br />

ScrollMoreShapes, <strong>and</strong> it is essentially a revision of ScrollShapes. Besides the rectangle <strong>and</strong> ellipse,<br />

you will add a thick line <strong>and</strong> fill in the shapes with various custom brushes. You have already learned the<br />

principles of drawing, so the code speaks for itself. First, because of your new brushes, you need to indicate<br />

that you are using the System.Drawing.Drawing2D namespace:<br />

using System;<br />

using System.Collections.Generic;<br />

using System.ComponentModel;<br />

using System.Data;<br />

using System.Drawing;<br />

using System.Drawing.Drawing2D;<br />

using System.Text;<br />

using System.Windows.Forms;<br />

Next are some extra fields in your Form1 class, which contain details of the locations where the shapes are<br />

to be drawn, as well as various pens <strong>and</strong> brushes you will use:<br />

private Rectangle rectangleBounds = new Rectangle(new Point(0,0),<br />

new Size(200,200));<br />

private Rectangle ellipseBounds = new Rectangle(new Point(50,200),<br />

new Size(200,150));<br />

private readonly Pen bluePen = new Pen(Color.Blue, 3);<br />

private readonly Pen redPen = new Pen(Color.Red, 2);<br />

private readonly Brush solidAzureBrush = Brushes.Azure;<br />

private readonly Brush solidYellowBrush = new SolidBrush(Color.Yellow);<br />

private static readonly Brush brickBrush = new<br />

HatchBrush(HatchStyle.DiagonalBrick, Color.DarkGoldenrod, Color.Cyan);<br />

private readonly Pen brickWidePen = new Pen(brickBrush, 10);<br />

code download ScrollMoreShapes.sln<br />

The brickBrush field has been declared as static so that you can use its value to initialize the<br />

brickWidePen field. <strong>C#</strong> will not let you use one instance field to initialize another instance field because it<br />

has not defined which one will be initialized first. However, declaring the field as static solves the problem.<br />

Because only one instance of the Form1 class will be instantiated, it is immaterial whether the fields are<br />

static or instance fields.<br />

The following is the OnPaint() override:<br />

protected override void OnPaint( PaintEventArgs e )<br />

{<br />

base.OnPaint(e);<br />

Graphics dc = e.Graphics;<br />

Point scrollOffset = AutoScrollPosition;<br />

dc.TranslateTransform(scrollOffset.X, scrollOffset.Y);<br />

if (e.ClipRectangle.Top+scrollOffset.X < 350 ||<br />

e.ClipRectangle.Left+scrollOffset.Y < 250)<br />

{<br />

dc.DrawRectangle(bluePen, rectangleBounds);<br />

dc.FillRectangle(solidYellowBrush, rectangleBounds);<br />

dc.DrawEllipse(redPen, ellipseBounds);<br />

dc.FillEllipse(solidAzureBrush, ellipseBounds);<br />

dc.DrawLine(brickWidePen, rectangleBounds.Location,<br />

ellipseBounds.Location+ellipseBounds.Size);<br />

}<br />

}<br />

code download ScrollMoreShapes.sln<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!