ID Number: Q77060
1.00
WINDOWS
Summary:
This article explains how to print a Visual Basic picture control to a
printer using several Windows 3.0 API function calls.
Note that 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.)
This information applies to Microsoft Visual Basic programming system
version 1.0 for Windows.
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:
1. 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.
2. Save the present object (SelectObject) and select the picture
control using the handle from the memory device context.
3. Use the BitBlt or StretchBlt function to copy the bitmap from the
memory device context to the printer.
4. Remove the bitmap from the memory device context (SelectObject) and
delete the device context (DeleteDC).
The following steps demonstrate this process:
1. Run Visual Basic, or from the File menu, choose New Project (ALT,
F, N) if Visual Basic is already running. Form1 is created by
default.
2. Add a picture (Picture1) control to Form1 and set the AutoRedraw
property to True from the Properties bar.
3. Add a command (Command1) button.
4. 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.
5. Add the following declarations to the global Declarations section of
the Code window. Note: Each Declare statement must be on one 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)
6. Add the following code to the Command_Click event:
Sub Command1_Click ()
Const NULL = 0&
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)
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%)
Print Escape(Printer.hDC, NEWFRAME, 0, NULL, NULL)
Printer.EndDoc
MousePointer = 1
End Sub
7. 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.
Reference(s):
"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
Additional reference words: 1.00 3.00