May 29, 1995
This article explains how you can print a Visual Basic® form several times on a single piece of paper.
You can use the Windows® application programming interface (API) StretchBlt function to copy a form to another form multiple times. For instance, the example program below uses the StretchBlt function to copy a form to a new form. The original form is copied four times. The destination form then contains a copy of the original form in its upper left, lower left, upper right, and lower right corners.
The technique presented in this article is useful for duplicating a form several times. For example, if you designed a form to keep track of telephone messages, you could print four messages per printed page, instead of using one piece of paper per telephone message.
The StretchBlt function can be used to copy an image from one device context to another. To use this function in your Visual Basic® application, include the following Declare statement in the General Declarations section of your form (note that it must be typed as a single line of code):
Private Declare Function StretchBlt Lib "GDI" (ByVal hDC%, ByVal X%, ByVal y%,
ByVal nWidth%, ByVal nHeight%, ByVal hSrcDC%, ByVal XSrc%, ByVal YSrc%, ByVal
nSrcWidth%, ByVal nSrcHeight%, ByVal dwRop&) As Integer
The StretchBlt function requires the following arguments:
hDC | An integer value containing the destination device context. |
X,Y | Integer values defining the rectangle's upper left corner for the destination device context. |
nWidth | The width of the image. |
nHeight | The height of the image. |
hSrcDC | An integer value containing the source device context. |
Xsrc, YSrc | Integer values defining the rectangle's upper left corner for the source device context. |
nSrcWidth | The width of the image. |
nSrcHeight | The height of the image. |
dwRop | The raster operation that is to be used. |
In the example program below, the StretchBlt function is called four times to copy the original Form1 to the destination Form2. Each time the copy operation is performed, the destination is offset to the next quarter section of Form2.
Before running this program, make sure your printer is online and ready to accept data. Press the F5 function key to run the example program, which will print Form2 on the paper four times.
Private Sub Command1_Click()
Dim W As Integer
Dim H As Integer
Dim X As Integer
W = Form2.ScaleWidth / 2
H = Form2.ScaleHeight / 2
X = SetStretchBltMode(Form2.hDC, 3)
X = StretchBlt(Form2.hDC, 0, 0, W, H, Form1.hDC, 0, 0, Form1.ScaleWidth,
Form1.ScaleHeight, SRCCOPY)
X = StretchBlt(Form2.hDC, W, 0, W, H, Form1.hDC, 0, 0, Form1.ScaleWidth,
Form1.ScaleHeight, SRCCOPY)
X = StretchBlt(Form2.hDC, W, H, W, H, Form1.hDC, 0, 0, Form1.ScaleWidth,
Form1.ScaleHeight, SRCCOPY)
X = StretchBlt(Form2.hDC, 0, H, W, H, Form1.hDC, 0, 0, Form1.ScaleWidth,
Form1.ScaleHeight, SRCCOPY)
Form2.Refresh
Form2.PrintForm
End Sub
Private Declare Function SetStretchBltMode Lib "GDI" (ByVal hDC%, ByVal
nStretchMode%) As Integer
Private Declare Function StretchBlt Lib "GDI" (ByVal hDC%, ByVal X%, ByVal y%,
ByVal nWidth%, ByVal nHeight%, ByVal hSrcDC%, ByVal XSrc%, ByVal YSrc%, ByVal
nSrcWidth%, ByVal nSrcHeight%, ByVal dwRop&) As Integer
Const SRCCOPY = &HCC0020
"Creating Bitmaps of Any Size with StretchBlt." Inside Visual Basic. (MSDN Library, Periodicals)
Knowledge Base Q84066. "How to Print Entire VB Form and Control the Printed Size."
Knowledge Base Q77060. "How to Print a VB Picture Control Using Windows API Functions."