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.
I won't cover everything here, so if you want more info, open up the MSDN library and select 'PaintPicture method' from the Index list. Picture is a IPictureDisp class, which can be retrieved from a PictureBox or Forms Picture property, or using LoadPicture.
x1 and y1 specify the coordinates where the picture should be placed on the page. Width1 and Height1 set the width and height of the picture to be outputted, respectively. x2 and y2, in combination with width2 and height2 can be used to specify a clipping area from the source image.
Take a look at the examples below.
The code below outputs the picture contained within Picture1 to the printer with 1000 twip (if this is the current scale mode) margins.
resizes the picture in Picture1 to 2000x2000 pixels, and prints it.
Both the Circle and the Line command allow you to specify the line colour, but you appear to have no control over the line width, what units the distances are measured in or any fill colours.
As all of these are fairly simple, they are explained briefly below:
FillColor: This specifies the fill color (in RGB), if any. Use the RGB function or VB colour constants to generate values for this parameter.
FillMode: This sets the pattern used to fill any shapes drawn by the Printer object. To see its possible values, hit F2, and select FillStyleConstants from the Classes list.
DrawMode: The mode of drawing. Unless you know what you are doing, leave this to 13 - Copy Pen. Many of these settings yield unpredictable results depending on the colours on the screen. See DrawModeConstants in the object browser for its values.
DrawWidth: The width of the drawing line.
DrawStyle: The style of the line. (ie solid, dotted etc). See DrawStyleConstants in the object browser for its values.
ScaleMode: This specifies the units in which coordinates are measured. The default is twips (1,440 twips equal one inch).
Aside from graphics, there are also a number of properties that directly affect printing. I'll cover these here:
Copies: The number of copies of your output that will be printed.
DeviceName: The name of the printer device.
DriverName: The name of the driver for the printer.
Duplex: Whether the printer is in Duplex mode or not (printing on both sides).
hDC: A handle to the printers device context.
Orientation: The orientation of the paper. This can be vbPRORLandscape or vbPRORPortrait.
Page: Returns the current page number (read-only).
PaperBin: The paper bin to use when printing the document. The values for this property can be found by hitting F2 (Object Browser), selecting PrinterObjectConstants from the Classes list, and taking a look at all the constants beginning with vbPRBN.
PaperSize: The size of the paper that is being printed on. See PrinterObjectConstants in the Object Browser, beginning with vbPRPS.
Port: The name of the printer port (read-only).
PrintQuality: The quality of printing (printer resolution). See PrinterObjectConstants, beginning with vbPRPQ.
TrackDefault: Whether to automatically move to the default printer if this is changed in Control Panel.
Zoom: The percentage with which the output is scaled up or down. Note that this only works on printers that support it!.