How to Print a VB Picture Control Using Windows API FunctionsID: Q77060
|
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:
Q85978 : Print Form or Client Area to Size on PostScript or PCL Printer
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:
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)
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
"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 query words: 2.00 3.00
Keywords :
Version : WINDOWS:1.0,2.0,3.0
Platform : WINDOWS
Issue type :
Last Reviewed: May 24, 1999