15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

OC10 ❘ ChaPTer 48 GrAphics with Gdi+<br />

ab.X = (int)abFloat.X;<br />

ab.Y = (int)abFloat.Y;<br />

Point ab1 = Point.Round(abFloat);<br />

Point ab2 = Point.Truncate(abFloat);<br />

Point ab3 = Point.Ceiling(abFloat);<br />

// but conversion back to PointF is implicit<br />

PointF abFloat2 = ab;<br />

You might be wondering what a unit is measured in. By default, GDI+ interprets units as pixels along the<br />

screen (or printer, whatever the graphics device is). This is how the Graphics object methods view any<br />

coordinates that they are passed as parameters. For example, the point new Point(20,10) represents<br />

20 pixels across the screen <strong>and</strong> 10 pixels down. Usually these pixels are measured from the top-left corner<br />

of the client area of the window, as has been the case in the previous examples. However, that will not<br />

always be the case. For example, on some occasions you might want to draw relative to the top-left corner<br />

of the whole window (including its border), or even to the top-left corner of the screen. In most cases,<br />

however, unless the documentation tells you otherwise, you can assume that you are talking about pixels<br />

relative to the top-left corner of the client area.<br />

You learn more on this subject later in this chapter, after scrolling is examined, when we discuss the three<br />

different coordinate systems in use — world, page, <strong>and</strong> device coordinates.<br />

Fize <strong>and</strong> sizef<br />

As with Point <strong>and</strong> PointF, sizes come in two varieties. The Size struct is for int types. SizeF is available<br />

if you need to use float types. Otherwise, Size <strong>and</strong> SizeF are identical. This section focuses on the Size<br />

struct.<br />

In many ways, the Size struct is identical to the Point struct. It has two integer properties that represent<br />

a distance horizontally <strong>and</strong> vertically. The main difference is that instead of X <strong>and</strong> Y, these properties are<br />

named Width <strong>and</strong> Height. You can represent the earlier diagram using this code:<br />

Size ab = new Size(20,10);<br />

Console.WriteLine("Moved {0} across, {1} down", ab.Width, ab.Height);<br />

Although Size mathematically represents exactly the same thing as Point, conceptually, it is intended to<br />

be used in a slightly different way. Point is used when you are talking about where something is, <strong>and</strong> Size<br />

is used when you are talking about how big it is. However, because Size <strong>and</strong> Point are so closely related,<br />

there are even supported conversions between these two:<br />

Point point = new Point(20, 10);<br />

Size size = (Size) point;<br />

Point anotherPoint = (Point) size;<br />

As an example, think about the rectangle you drew earlier, with top-left coordinate (0,0) <strong>and</strong> size (50,50).<br />

The size of this rectangle is (50,50) <strong>and</strong> might be represented by a Size instance. The bottom-right corner is<br />

also at (50,50), but would be represented by a Point instance. To see the difference, suppose that you draw<br />

the rectangle in a different location, so that its top-left coordinate is at (10,10):<br />

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

Now the bottom-right corner is at coordinate (60,60), but the size is unchanged at (50,50).<br />

The addition operator has been overloaded for Point <strong>and</strong> Size structs so that it is possible to add a Size to<br />

a Point struct, resulting in another Point struct:<br />

static void Main(string[] args)<br />

{<br />

Point topLeft = new Point(10,10);<br />

Size rectangleSize = new Size(50,50);<br />

Point bottomRight = topLeft + rectangleSize;<br />

Console.WriteLine("topLeft = " + topLeft);<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!