Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
XML RSS Feed
Tips & Tricks
These are brief tips and tricks that can make Visual Basic programming easier.
New Control Usage Numerical Stuff
Starting & Stopping I/O Bug Alerts
Software Engineering Printing System Issues
Graphics Coding Techniques Other Programs
Databases Misc Forms
Web

Printing

Printing Color Lines
Trevor Finch

There seems to be a slight problem when printing lines in colour from VB6 (SP4). This works correctly on a PictureBox, but prints black on the Printer (HP Laser Jet and Lexmark)...

    Object.ForeColor = vbRed
    Object.Line (X1, Y1)-(X2, Y2)    -red on PictureBox, black on Printer
    Object.Print "Text"              -ok on both PictureBox and Printer
But if the colour is added as an argument...
    Object.ForeColor = vbRed
    Object.Line (X1, Y1)-(X2, Y2), vbRed  - ok on both PictureBox and Printer
    Object.Print "Text"                   - ok on both PictureBox and Printer
If you are not getting colored lines when you expect them, add the color argument to the Line statement.


Implied Raster Ops for the Printer: The Color White
Chris Wagg

I guess I always knew this, but was unconscious of it until now: the color white (vbWhite or 16777215 or 0xFFFFFF) is always ignored by the printer and treated like a transparent color. If you don't believe me, put some colored stock into your printer and print an image with some white in it: everywhere there's white on the picture, no color is printed on the paper, allowing the stock color to show through. If you don't have colored stock, scribble on a sheet of paper first, then print. If a color is not pure white, like a sand or an eggshell color, it will be printed: this is a concern if you're using the default system colors that are assigned to most controls (it looks white but it's not!).

Keep this basic fact in mind when designing graphics: pure white is a good default color for a canvas (the background color of a picture). Also make sure your forms and controls are white too (for the background color) if you're using something like the VB PrintForm method or simulated code like screen capture printing. In other "Form and Report" development environments, like VBA, Access, Oracle Forms, Crystal Reports, etc., usually there's a default PRINT button that simulates the "PrintForm" method: not surprisingly, most users click this button.

By using white, not only will the output print faster, but it will also save some toner AND some wear and tear on the printer. This is especially important on a network when you're in charge of keeping the network moving fast AND in charge to maintaining the printers. In other words, forms and graphics with a white background print faster and reduce network traffic. I know, it's difficult these days to NOT add graphics and vibrant colors to a form, but it's a simple sacrifice to make if your users do a lot of printing.

I guess this leaves one unanswered question: how do you print White when you really want it?

I almost forgot to add this - there's one caveat about changing colors and not using the default system colors: if you force the background color to white (or any other color), make sure you force the foreground color too. Why? A user could design or install a desktop scheme where white (or almost white) is the default foreground color. If so, the text would be invisible and unreadable!


Print Multi-Page Rich Text
The Printer object does not work well with the RichTextBox because it ignores the Rich Text codes. To print from the RichTextBox, use SelPrint. SelPrint is not very friendly. It prints the way it wants to and then ejects the page. To print multi-page Rich Text, find the start and end of each page, set SelStart and SelLength accordingly, and use SelPrint to print each page. Here are the steps:
  1. Save the edit version with SaveFile (because we're going to modifiy the heck out of it).
  2. Convert the edit version to a print version:
    1. Run through the entire document changing SelIndent and SelRightIndent to values appropriate for the printer (indent values in the edit version are appropriate for the screen, not the printer).
    2. Make any other changes; in my case, add page headers and eliminate scene numbers.
  3. Print one page at a time with a SelPrint loop. Counting lines is tricky. Finding LFCRs is easy but I had to also account for line wraps which bump the line count, and page overflows which must not be allowed to happen.
  4. Delete the print version with SelRTF="".
  5. Reinstart the edit version with LoadFile.
Thanks to Ray White.
Print MSFlexGrid
Here's a quick way to print a MSFlexGrid control's contents:
    Printer.PaintPicture MSFlexGrid_Name.Picture, 0, 0
    Printer.EndDoc
And if you want it to be the full length of the printer page add this before those two statements:
    Dim old_width as Integer
    MSFlexGrid_Name.width=printer.width
and this at the end:
     MSFlexGrid_Name.width=old_width
Thanks to Edward Baisa.
Print Multiple Copies Quickly
When you print a document with TrueType fonts, the system downloads the fonts to the printer. That can take a long time. You can make printing multiple copies faster by printing all of the copies within one printer document.
    For i = 1 To num_copies
        ' Print the document here...

        Printer.NewPage
    Next i
    Printer.EndDoc

Print Gray Lines and Areas
Black and white printers cannot print gray lines, but they can dither to create gray areas. To create a gray border around an area, draw thin gray boxes with DrawStyle = vbInvisible (5) so they have no borders.
Set Printer Margins
Use the Printer's scale properties to set margins. Set the properties so the position (0, 0) is mapped to where you want the margins. For example, the following code makes the left margin 0.5 inches and the top margin 0.75 inches. The factor of 1440 converts inches into twips.
    Printer.ScaleLeft = -0.5 * 1440
    Printer.ScaleTop = -0.75 * 1440
    Printer.CurrentX = 0
    Printer.CurrentY = 0
Unfortunately printers don't print exactly where you think they should. Try using this statement to print a box 1 inch from the upper left corner:
    Printer.Line (1440, 1440)-Step(1440, 1440)
See how far off it is. Try printing several times. If your printer is consistently off by some amount at the top and left, you may be able to get good results by subtracting these amounts from the margins you set.
Provide Print Preview
Start by making the printing routine take the object it should draw on as a parameter. For printing, pass the routine the Printer object.

For preview, pass the routine a hidden PictureBox. Then use PaintPicture to copy the hidden picture to a visible PictureBox at the desired scale. Drawing at full scale on the hidden PictureBox first allows you to scale fonts and other graphics without distortion.


Printing Tools
By Dale Thorn:

In the Dr. Dobb's web site (ddj.com), under Programmer's Resources, under BASIC, there are some utilities that do a great job of printing.

If you have a preformatted file that's coded for a particular printer, use PRNT, which simply spools characters to the printer.

For various plain-text files, PSET decides the best fit to the paper you use, and optimizes the printing as much as possible to fit.

Note: These are DOS programs called from a SHELL command. Of course, you can import the code into VB and make them VB programs.

I've used PSET extensively on every combination of network and PC, from handheld and serial port to NT and network. As long as the drivers etc. are in place, it works perfectly.


Send your Tips and Tricks to feedback@vb-helper.com.

 
Subscribe to the VB Helper newsletter
Copyright © 1997-2001 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
www.vb-helper.com/tips7.htm Updated