Despite claims that computers would create a "paperless society", printing is still one of the most important functions of any application. Visual Basic makes it easy to print, and in this article James shows you what you need to know to print text, shapes and images with Visual Basic. If you need to create your own invoicing or reporting system with Visual Basic then this article is a definite must read.
Printing With Visual Basic - Printing graphics (Page 3 of 5 )
Outputting graphics to the printer is identical to outputting graphics to a PictureBox. We will cover a few basic commands here, but first lets take a quick look at the CurrentX and CurrentY properties. These were used in the last example, and show where the current coordinates are. Visual Basic automatically changes these when it has finished the outputting. For example, imagine you draw a line on a piece of paper - when you have finished drawing, the pen is at the end of the line, not the start. This is the same with the CurrentX and CurrentY properties. If you call the Line method, the line is drawn, and then CurrentX and CurrentY are changed to point to point at the end of the line. You can then change these if necessary to control where the next piece of text or next line is printed.
VB uses the same functions for drawing in picture boxes and outputting to the printer. If you don't want to have to visit the printer and waste paper whilst following this tutorial, simply create a new project, set the forms backcolor to white, autoredraw property to true, and change any Printer.Function to Function. You will also need to comment out the Printer.EndDoc lines. The data that was going to be printed on paper will now be 'printed' to the form instead.
The first obvious drawing function is Line, which uses this syntax:
This might look a bit odd at the moment, so lets explore it in more detail. First of all, the (x1, y1) parameter tells VB the coordinates where you want the line to start and the (x2, y2) parameter tells VB the coordinates where you want the line to end. The brackets surrounding the word 'Step' mean that it is optional - you don't have to include it. If you include the first step (without the brackets), then VB will draw the line relative to the CurrentX and CurrentY properties. This means, that if you passed Step (4,8) instead of the line starting 4 pixels (or whatever DrawMode you are in) from the left of the page, and 8 pixels from the top, it will draw the line 4 pixels to the right of CurrentX, and 8 pixels below CurrentY. Essentially,
The final parameter specifies the colour of the line. The B and F options provide two more options. If B is specified, then VB will treat the second set of coordinates as the position of the bottom right hand corner of a box, and draw that box for you. If you use the B option, you can also give the F option, which means the box will be filled using the graphic options, which are described later.
Lets take a look at a few examples.
Printer.Line (1000, 2000)-(5000, 3000), vbRed
This code draws something like this:
Using the a combination of Line statements and the Step keyword, we can also draw a box:
Printer.Line (1000, 2000)-(5000, 2000), vbRed
'Draw down from end of first line
'don't change X pos
'change Y pos by 4000
Printer.Line Step(0, 0)-Step(0, 2000), vbRed
'draw across 'change X pos by -4000
'don't change Y pos
Printer.Line Step(0, 0)-Step(-4000, 0), vbRed
'don't change X pos
'change Y pos by -4000
Printer.Line Step(0, 0)-Step(0, -2000), vbRed
Which looks like this:
Of course by using the B statement, we can draw a box far more easily with the following code:
Printer.Line (1000, 2000)-(5000, 4000), vbRed, B
The other major command is Circle, which allows you to draw a circle, ellipse or an arc. The circle statement uses the following syntax:
As you can see, there are once again plenty of options. The (x, y) part specifies the centre of the circle to be drawn. As in the Line function, using the Step option means that these x and y coordinates will be taken relative to CurrentX and CurrentY. The radius parameter specifies the radius of the circle (that is the distance from its centre to its edge). There are then a number of optional parameters. color specifies the colour of the circle in RGB (red green blue). The start and end parameters allow you to draw a partial circle, or rather an ellipse or arc. Start specifies the start of the arc in radians (see note), and end specifies the end of the arc, in radians. Finally, the aspect option allows you to change the aspect ratio of the circle. A value of 1 is a perfect circle, a value of 1.5 is an ellipse.
Radians are used to measure angles. 1 radian is (180/pi)°. 360°= 2 * pi