1. Trang chủ
  2. » Công Nghệ Thông Tin

Addison Wesley Windows Forms Programming In C Sharp Sep 2003 ISBN 0321116208

7 67 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 7
Dung lượng 381,52 KB

Nội dung

Shapes Now that you know how to frame and fill shapes with pens and brushes, you might be interested in the shapes that are available Figure 4.15 shows them Figure 4.15 The Basic Shapes All the shapes in Figure 4.15 were edged using a DrawXxx function from the Graphics object for the formfor example, DrawArc and DrawBezier The shapes that can be filled were drawn using a FillXxx function, such as FillClosedCurve and FillEllipse Not all of the shapes could be filled because not all of them are closed shapes; for example, there is no FillCurve However, all the open shapes (except the Bezier) have closedshape equivalents; for example, a filled arc is called a pie Also, notice the use of the Lines shape This shape could be drawn using multiple calls to the DrawLine function, but three of the shapesline, rectangle, and Bezierhave helpers that draw more of them at once In addition to being convenient, these helpers handle the appropriate mitering at intersections that you'd otherwise have to do by hand For example, the Graphics object provides all the following functions for drawing rectangles: DrawRectangle, DrawRectangles, FillRectangle, and FillRectangles Curves Most of the shapes are specified as you'd expect You specify the rectangle and the ellipse using an x, y, width, and height, or a Rectangle object You specify the arc and the pie as with a rectangle, but you also include a start and a length of sweep, both specified in degrees (the shown arc and pie start at 180 degrees and sweep for 180 degrees) The lines and polygon are specified with an array of points, as are the curves, but the curves are a little different The curve (also known as a cardinal spline) acts just like a set of lines, except as a point is approached, there's a curve instead of a sharp point In addition to a set of points, the curve is specified using a tension, which is a value that determines how "curvy" the curve is around the points A tension of 0 indicates no curve, and a tension of 0.5 is the default The tension can get as high as allowed by the floating point type Figure 4.16 shows some common variations Figure 4.16 Curves Drawn with Various Values of Tension Figure 4.16 shows the same set of points (as indicated by the black dots and index number) drawn using the DrawCurve function with three different values of tension As the tension increases, so does the amount of curve at each point Unlike normal curves, Bezier curves are specified with exactly four points: one start point, followed by two control points, followed by an end point If the DrawBeziers function is used to draw multiple curves, the end point of the preceding curve becomes the start point of the next Figure 4.17 shows three Bezier curves drawn using the same set of points, but in different orders Figure 4.17 Three Bezier Curves Drawn Using the Same Set of Points in Different Orders In each case, the Bezier is drawn between the start point and the end point, but the two control points are used to determine the shape of the curve by exerting more "control" over the curve as they get farther away Smoothing Modes When drawing shapes, you may want the smooth rendering you've seen in the really cool applications The shapes in Figures 4.15, 4.16, and 4.17 were all drawn without any kind of "smoothing," as evidenced by the jagged edges The jagged edges are caused by the swift transition between the color of the shape being drawn and the color of the background A technique known as antialiasing uses a smoother transition between the shape color and the background color, in much the same way that a gradient brush provides a smooth transition from one color to another To turn on antialiasing for shapes subsequently drawn on the Graphics object, you set the SmoothingMode property: g.SmoothingMode = SmoothingMode.AntiAlias; The default value of the SmoothingMode property is SmoothingMode.None In addition to the AntiAlias value, SmoothingMode has three other values: Default, HighSpeed, and HighQuality These are merely aliases for None, None, and AntiAlias, depending on your system settings Figure 4.18 shows the difference between using and not using antialiasing Figure 4.18 The Effect of Changing the SmoothingMode from AntiAlias to None Notice that setting the SmoothingMode has no effect on the text drawn on the Graphics object You set the rendering effects of text using the TextRenderingHint property, which I discuss in Chapter 5: Drawing Text Saving and Restoring Graphics Settings Setting the SmoothingMode in the preceding section is the first time we've changed a property on the Graphics object that affects subsequent operations You can also set other properties that affect subsequent operations, and we'll cover those topics as appropriate When you change a property of a Graphics object in a method other than the Paint event handler itself, it's a good idea to reset it on the Graphics object before the method returns: void DrawSomething(Graphics g) { // Save old smoothing mode SmoothingMode oldMode = g.SmoothingMode; // Make things draw smoothly g.SmoothingMode = SmoothingMode.AntiAlias; // Draw things // Restore smoothing mode g.SmoothingMode = oldMode; } This can quickly become painful when there are multiple properties to restore Luckily, you can save yourself the trouble by taking a snapshot of a Graphics object state in a GraphicsState object from the System.Drawing Drawing2D namespace: void DrawSomething(Graphics g) { // Save old graphics state GraphicsState oldState = g.Save(); // Made things draw smoothly g.SmoothingMode = SmoothingMode.AntiAlias; // Draw things // Restore old graphics state g.Restore(oldState); } The Save method on the Graphics class returns the current state of the properties in the Graphics object The call to Restore takes a GraphicsState object and sets the Graphics object to the state cached in that object The code shows a pair of calls to Save and Restore, but it's not necessary to keep them in balance, something that's handy for switching a lot between a couple of states Chapter 5 Drawing Text Probably the most useful thing to draw in any application is text Sometimes you'll draw text yourself, and sometimes it will be drawn for you by the controls you're using No matter who does the drawing, you often can specify the font used to draw the text, and that's what the first part of this chapter is about The second part deals with drawing text yourself into a Graphics object or a GraphicsPath ... of lines, except as a point is approached, there's a curve instead of a sharp point In addition to a set of points, the curve is specified using a tension, which is a value that determines how "curvy" the curve is around the points... Notice that setting the SmoothingMode has no effect on the text drawn on the Graphics object You set the rendering effects of text using the TextRenderingHint property, which I discuss in Chapter 5: Drawing Text... Chapter 5: Drawing Text Saving and Restoring Graphics Settings Setting the SmoothingMode in the preceding section is the first time we've changed a property on the Graphics object that affects subsequent operations

Ngày đăng: 26/03/2019, 17:13