Home arrow Visual Basic arrow Page 3 - Printing With Visual Basic
VISUAL BASIC

Printing With Visual Basic


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.

Author Info:
By: James Crowley
Rating: 4 stars4 stars4 stars4 stars4 stars / 302
April 07, 2002
TABLE OF CONTENTS:
  1. · Printing With Visual Basic
  2. · Printing with VB
  3. · Printing graphics
  4. · More Printing in VB
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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:

Printer.Line [Step] (x1, y1) [Step] - (x2, y2), [color], [B][F]

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,

Printer.Line Step (x1, y1)

is the same as

Printer.Line (x1 + Printer.CurrentX, y1 + Printer.CurrentY)

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

Printer.EndDoc


This code draws something like this:

Printing a line in VB

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

'draw up

'don't change X pos

'change Y pos by -4000

Printer.Line Step(0, 0)-Step(0, -2000), vbRed

Printer.EndDoc


Which looks like this:

Printing a box in VB

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

Printer.EndDoc


The other major command is Circle, which allows you to draw a circle, ellipse or an arc. The circle statement uses the following syntax:

Printer.Circle [Step] (x, y), radius, [color, start, end, aspect]

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

Now lets take a look at a few examples:

Printer.Circle (3000, 3000), 1000, RBG(100, 30, 30)

Draws a circle like this:

Printing a circle with VB

To fill a circle you will need to change the FillStyle option, which is discussed in the next section. Using the end and start properties, we can create arcs.

As radians are closely related to pi and the pi constant is used for the end and start parameters, add the following declaration to your project: Const pi = 3.141592654

This code

Printer.Circle (3000, 3000), 1000, vbBlue, 0, pi

draws the following arc:

Printing an arc with VB

If you provide negative values, VB draws a line to the center of the circle too. For example,

Printer.Circle (3000, 3000), 1000, vbBlue, -1, -pi

draws the following:

Printing an arc with VB

Finally, using the aspect option, we can draw an ellipse. For example,

Printer.Circle (3000, 3000), 1000, vbBlue, , , 0.5

outputs

Printing a closed arc with VB
blog comments powered by Disqus
VISUAL BASIC ARTICLES

- Developing an XML Web Service Using Visual S...
- Creating an HTML File List with VB
- Fun with Email: VB6, CDO, MAPI, and a Remote...
- Extranet/Intranet Dictionary Cracker in VB
- Finding Default App Icons With Visual Basic
- Registry Fever With Visual Basic
- Implementing An ADO Data Control With VB6
- Printing With Visual Basic
- MSMQ Part 1/2: Architecture and Simple Imple...
- Magnifying The Desktop With Visual Basic
- Sending Email With MAPI Components in Visual...
- Two Person Chat With The Winsock Control And...
- A Real-Time ActiveX News Control
- Accessing the Windows API in Visual Basic

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials

antalya escort escort antalya antalya escortlari antalya escort bayan