SunQuest
 
       Visual Basic
  Home arrow Visual Basic arrow Page 3 - Printing With Visual Basic
Moblin
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC

Printing With Visual Basic
By: James Crowley
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 199
    2002-04-07

    Table of Contents:
  • Printing With Visual Basic
  • Printing with VB
  • Printing graphics
  • More Printing in VB
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Try It Free
     
    ADVERTISEMENT

    Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!

    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

    More Visual Basic Articles
    More By James Crowley


     

    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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway