-->![Get Get](http://www.java2s.com/Tutorial/VBImages/GDIObjectsCreatePenWidth.PNG)
Definition
Configure you Surface Pen, change Pen setting. Configure you Surface Pen, change Pen setting. Programming in Visual Basic.Net How to Connect Access Database to VB.Net - Duration: 19:11.
Gets or sets an array of custom dashes and spaces.
Property Value
An array of real numbers that specifies the lengths of alternating dashes and spaces in dashed lines.
Exceptions
ArgumentExceptionArgumentExceptionArgumentExceptionArgumentException
The DashPattern property is set on an immutable Pen, such as those returned by the Pens class.
Examples
The following code example demonstrates the effects of setting the DashCap, DashPattern, and SmoothingMode properties.
This example is designed to be used with Windows Forms. Paste the code into a form and call the
ShowPensAndSmoothingMode
method when handling the form's Paint event, passing e
as PaintEventArgs. Remarks
Assigning a value other than
null
to this property will set the DashStyle property for this Pen to Custom. The elements in the
dashArray
array set the length of each dash and space in the dash pattern. The first element sets the length of a dash, the second element sets the length of a space, the third element sets the length of a dash, and so on. Consequently, each element should be a non-zero positive number. The length of each dash and space in the dash pattern is the product of the element value in the array and the width of the Pen.
Applies to
Home > Articles > Programming > Windows Programming
␡- Drawing Basic Shapes
< BackPage 4 of 10Next >
This chapter is from the book Visual Basic Programmer's Guide to the .NET Framework Class Library
This chapter is from the book
This chapter is from the book
Visual Basic Programmer's Guide to the .NET Framework Class Library
Drawing Basic Shapes
We continue our exploration of the namespace by drawing some basic shapes. Again, the variety and features contained in the namespace are astounding; we'll try to take a path that focuses on the shapes and illustration details you're most likely to employ in building your application.
Rectangles
The Rectangle structure is the backbone of the shapes presented in this section. Classes like Ellipse and Pie use it to bind their shape. The structure stores the size and location of a rectangular region.
We have two constructors available to create an instance of the Rectangle structure. One creates the rectangle based on the upper-left x and y coordinate, the width of the rectangle, and its height. To the other constructor, you pass both location as an instance of the Point structure, and Size as an instance of the Size structure.
Listing 9.1 illustrates both rectangle constructors. The code is simply fired by a button's click event. The rectangles are output to the active form.
Listing 9.1 DrawRectangle
The DrawRectangle method of the Graphics object allows us to draw the outline of a rectangle to the drawing surface. The method is overloaded with three different sets of parameters. The first set allows you to create a rectangle based on a Pen and Rectangle instance. The other two sets create a rectangle directly from a Pen instance and the rectangle's x and y coordinates (width and height). The difference between these two is the data types used to define the coordinates and size. One uses the Int32 data type, and the other uses a Single.
Ellipses
An ellipse is simply a circle or oval bound inside a Rectangle structure. To draw an ellipse, you can use the DrawEllipsemethod of the Graphics class. This method requires a Penobject and some semblance of a rectangle definition (structure instance, coordinates, and so on). The following code illustrates drawing an ellipse inside of a defined Rectangle instance.
Polygons
A polygon is a closed plane, or object, represented by at least three lines (segments). To draw polygons with the namespace, we simply play connect-the-dots. We first create the dots using the Pointstructure. These dots are actually a series of coordinates through which we will draw lines. Figure 9.3 shows a set of six points using the basic Windows coordinate system.
To connect the dots, we use the DrawPolygon method of the Graphicsclass. We indicate the order in which to connect the points of the polygons by their order in our Point array. You can see from code Listing 9.2 that the points can be connected in a variety of ways to produce varied results.
Figure 9.3 Polygon coordinates.
Listing 9.2 DrawPolygon
Notice that the last member of the array in Listing 9.2 always points back to the starting point. This closes the polygon. If you omit this last element in the array, the method assumes it and will close the polygon for you.
Pies
A pie is simply a wedge of a circle, similar to a section in a pie chart or a piece of pie. GDI+ defines a pie section by an Ellipse object (which is contained by a Rectangle), and the two radial lines that intersect with the endpoints of the arc defined by the Ellipse. Figure 9.4 illustrates this point.
Figure 9.4DrawPie method.
From Figure 9.4, you can see that the x and y coordinates actually define the upper-left corner (or starting point) of the bounding rectangle. The pie piece is drawn from the center of this rectangle. The width and height of the rectangle define the size of the ellipse, which in turn defines the size of our wedge.
To create a pie wedge we use, you guessed it, the Graphicsclass. The DrawPie method has two key parameters (in addition to the Rectangle that defines the Ellipseand the Penobject that is used to draw the pie), startAngle and sweepAngle. The startAngle parameter is an angle that is defined clockwise from the x-axis to the first side of the pie. The parameter, sweepAngle, is defined clockwise from the first side of the pie to the second side of the pie section. This is easier to grasp by looking at both the code in Listing 9.3.
Listing 9.3 DrawPie
From Listing 9.3, you can see that when the first section of the pie is created, we set the start angle to 0 and the sweep angle to 90° counterclockwise (negative). The next piece starts where the last one left off and again creates a section of equal value (90°). Note how the last piece was created with the FillPie method with an offset to add extra highlighting to the piece.
Suggestions for Further Exploration
- Experiment with the following methods of the Rectangle structure: inflate, intersects, and contains.
- Check out the Graphics class methods: DrawRectangles, DrawLines, FillRectangle, and FillPolygon.
Related Resources
- Book $39.99
- Book $31.99
- eBook (Watermarked) $25.59