HOWTO: Print a Form That is Too Large for the Screen or PageID: Q230502
|
You are attempting to print a form that is larger than the screen, or only partially displayed. The PrintForm method prints only the portion of the form that is displayed. You may also have a form that is too large to print on one page. This article demonstrates how to accomplish both of these tasks.
NOTE: Visual Basic 6.0 Online help states that PrintForm prints the entire form, whether or not the entire form is visible. This information is not correct.
In order to accomplish this task, the controls on the form must be placed in a PictureBox. What you will actually print is the bitmap that is contained in the PictureBox, instead of a screen capture of the form itself (which is what PrintForm does). Because the PictureBox's bitmap is stored in a device context in memory, we have access to the entire bitmap, regardless of what displays. Note that for the purposes of this article the height of the form is set to 11 inches. If another paper size is desired, like 8.5 x 14, set the sTall variable to an appropriate value, for example 14. Note also that these assignments are made in twips because this is the default scalemode for forms. If you change the scalemode, you will need to also change these values to match.
Private Const twipFactor = 1440
Private Const WM_PAINT = &HF
Private Const WM_PRINT = &H317
Private Const PRF_CLIENT = &H4& ' Draw the window's client area.
Private Const PRF_CHILDREN = &H10& ' Draw all visible child windows.
Private Const PRF_OWNED = &H20& ' Draw all owned windows.
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Private Sub Form_Load()
Dim sWide As Single, sTall As Single
Dim rv As Long
Me.ScaleMode = vbTwips ' default
sWide = 8.5
stall = 11 ' or 14, etc.
Me.Width = twipFactor * sWide
Me.Height = twipFactor * stall
With Picture1
.Top = 0
.Left = 0
.Width = twipFactor * sWide
.Height = twipFactor * stall
End With
With Picture2
.Top = 0
.Left = 0
.Width = twipFactor * sWide
.Height = twipFactor * stall
End With
With Label1
.Caption = "Top"
.Left = Me.Width / 2
.Top = 0
End With
With Label2
.Caption = "Bottom"
.Top = (twipFactor * stall) - .Height * 2
.Left = Me.Width / 2
End With
Me.Visible = True
DoEvents
Picture1.SetFocus
Picture2.AutoRedraw = True
rv = SendMessage(Picture1.hwnd, WM_PAINT, Picture2.hDC, 0)
rv = SendMessage(Picture1.hwnd, WM_PRINT, Picture2.hDC, _
PRF_CHILDREN + PRF_CLIENT + PRF_OWNED)
Picture2.Picture = Picture2.Image
Picture2.AutoRedraw = False
Printer.Print ""
Printer.PaintPicture Picture2.Picture, 0, 0
Printer.EndDoc
End Sub
Printer.Print ""
Printer.PaintPicture Picture2.Picture, 0, 0
Printer.EndDoc
Replace those three lines of code with the following:
Printer.Print ""
Printer.PaintPicture Picture2.Picture, 0, 0, , , _
0, 0, , Picture2.Height / 2
Printer.NewPage
Printer.PaintPicture Picture2.Picture, 0, 0, , , _
0, Picture2.Height / 2, , Picture2.Height / 2
Printer.EndDoc
For ease of illustration, this example merely divides the form in half. You can adjust the clipping region appropriately for you needs.
For additional information about printing forms, please see the following article in the Microsoft Knowledge Base:
Q178076 HOWTO: Use a PictureBox to Control Orientation Printing a FormVisual Basic OnLine Help; search on: "PaintPicture"
Additional query words:
Keywords : kbsample kbPrinting kbVBp kbVBp400 kbVBp500 kbVBp600 kbGrpVB
Version : WINDOWS:4.0,5.0,6.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 28, 1999