Print a form in any orientation
By: Microsoft Corporation  Date: 11/01/2001

The PrintForm method creates its own Device Context and therefore ignores any Printer object settings. It will just use the default orientation of the current default printer.
Because you cannot change how
PrintForm behaves, you must print another way. The process described here uses two PictureBoxes, each one covering the Form. This same technique can be used to print any group of controls or a form that is too large for the screen or the page.  This example basically prints the controls contained in the PictureBox.

  1. Add two PictureBoxes to Form1 so that each nearly covers the Form.
    Avoid drawing the second PictureBox within the first. Doing so makes the second PictureBox a member of the first. A simple way to avoid the problem is to place the origin point of the second PictureBox to the left of the origin point of the first PictureBox. Once the second box is drawn, you can resize it so that it is the same size as the first PictureBox.
  2. Right-mouse click on Picture2 and choose Send to Back.
  3. Place Picture1 on top of Picture2, covering Picture2.
  4. Place this code in the Declarations Section of the module of the Form:
    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 Const WM_PAINT = &HF
    Private Const WM_PRINT = &H317
    Private Const PRF_CLIENT = &H4&
    Private Const PRF_CHILDREN = &H10&
    Private Const PRF_OWNED = &H20&

To print all the controls contained in Picture1, use the following code:

Dim rv As Long
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.Orientation = vbPRORLandscape
Printer.Print ""
Printer.PaintPicture Picture2.Picture, 0, 0
Printer.CurrentY = Picture2.Height + 200
Printer.EndDoc

In this case, landscape orientation is used. Change the Printer.Orientation line for portrait orientation.

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it