The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for
Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0
SUMMARY
This article explains how to print a Visual Basic picture control to a
printer using several Windows API function calls.
NOTE: this example will not work correctly on PostScript printers. Instead
of the picture control printing, two blank sheets are ejected from the
printer when using a printer configured to use the PostScript printer
driver. For the example to work correctly, the printer must use a standard
non-PostScript laser printer configuration (such as PCL/HP.)
For additional information on printing to PostScript printers, please see
the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q85978
TITLE : Print Form or Client Area to Size on PostScript or
PCL Printer
MORE INFORMATION
To print a picture control from Visual Basic, you must use the
PrintForm method. Although this can very useful, there is no
straightforward method of printing just a picture control without the
use of API function calls. Printing a picture control to the printer
is useful when you want to control the location or size of the printed
image. Calling API functions to print a picture control is also useful
if you want to include other images or text along with the picture
image on a single sheet of paper.
To print a bitmap, you need to do the following:
- Create a memory device context that is compatible with the
bitmap (CreateCompatibleDC). A memory device context is a block of
memory that represents a display surface. It is used to prepare
images before copying them to the actual device surface of the
compatible device.
- Save the present object (SelectObject) and select the picture
control using the handle from the memory device context.
- Use the BitBlt or StretchBlt function to copy the bitmap from the
memory device context to the printer.
- Remove the bitmap from the memory device context (SelectObject) and
delete the device context (DeleteDC).
Step-by-Step Example
The following steps demonstrate this process:
- Start Visual Basic or from the File menu, choose New Project (ALT, F, N)
if Visual Basic is already running. Form1 is created by default.
- Add a picture control (Picture1) to Form1 and set the AutoRedraw
property to True.
- Add a command button (Command1).
- Display some graphics in Picture1 by loading from a picture file or
pasting from the Clipboard at design time. You can load a picture
from a file as follows:
a. Select the Picture property from the Properties bar.
b. Click the arrow at the right of the Settings box, then select the
desired picture file (such as a .BMP or .ICO file supplied with
Microsoft Windows) from the dialog box.
- Add the following declarations to the global Declarations section of
the Code window. Enter each Declare statement as one, single line.
Declare Function CreateCompatibleDC% Lib "GDI" (ByVal hDC%)
Declare Function SelectObject% Lib "GDI" (ByVal hDC%, ByVal hObject%)
Declare Function StretchBlt% Lib "GDI" (ByVal hDC%, ByVal X%,
ByVal Y%, ByVal nWidth%, ByVal nHght%, ByVal hSrcDC%, ByVal XSrc%,
ByVal YSrc%, ByVal nSrcWidth%, ByVal nSrcHeight%, ByVal dwRop&)
Declare Function DeleteDC% Lib "GDI" (ByVal hDC%)
Declare Function Escape% Lib "GDI" (ByVal hDC As Integer,
ByVal nEscape As Integer, ByVal nCount As Integer,
LpInData As Any, LpOutData As Any)
- Add the following code to the Command_Click event:
Sub Command1_Click ()
Const SRCCOPY = &HCC0020
Const NEWFRAME = 1
Const PIXEL = 3
'* Display hour glass.
MousePointer = 11
Picture1.Picture = Picture1.Image
'* StretchBlt requires pixel coordinates.
Picture1.ScaleMode = PIXEL
Printer.ScaleMode = PIXEL
Printer.Print ""
hMemoryDC% = CreateCompatibleDC(Picture1.hDC)
hOldBitMap% = SelectObject(hMemoryDC%, Picture1.Picture)
'Enter the following three lines as one, single line:
ApiError% = StretchBlt(Printer.hDC, 0, 0, Printer.ScaleWidth,
Printer.ScaleHeight, hMemoryDC%, 0, 0, Picture1.ScaleWidth,
Picture1.ScaleHeight, SRCCOPY)
hOldBitMap% = SelectObject(hMemoryDC%, hOldBitMap%)
ApiError% = DeleteDC(hMemoryDC%)
Result% = Escape(Printer.hDC, NEWFRAME, 0, 0&, 0&)
Printer.EndDoc
MousePointer = 1
End Sub
- Run the program to copy the bitmap to the printer. If you have
selected a low resolution from the Print Manager, printing the
bitmap will proceed quickly (the lower the resolution, the faster
the print time). While designing your software, you may want
to keep this at the lowest possible resolution. The print
resolution can be changed from the Windows Control Manager.
REFERENCES
"Programming Windows: The Microsoft Guide to Writing Applications
for Windows 3," Charles Petzold, Microsoft Press, 1990
"Microsoft Windows Software Development Kit: Reference Volume 1,"
version 3.0
"Microsoft Windows Software Development Kit: Guide to Programming,"
version 3.0.
WINSDK.HLP file shipped with Microsoft Windows 3.0 Software
Development Kit
|