PrintForm to fit to one page (Landscape)

14,848

Check this msdn link

and here

Also, i more suggest to use print preview too, cause it can adjust the margin. Here is the link about print preview. But between these link, i would most suggest this code..

Print a Form That Is Larger Than the Screen

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.

  2. Add two PictureBoxes to Form1.

    Avoid drawing the second PictureBox inside the first, because doing so makes the second PictureBox a member of the first. Instead, place the origin point of the second PictureBox to the left of the origin point of the first PictureBox.

  3. Right-click Picture2 and choose Send to Back.

  4. Add two labels to Picture1, leaving Picture2 empty.

  5. Add the following code to the General Declarations section of Form1:

    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
    
  6. Run the project.

  7. The Top and Bottom labels should appear in their respective positions regardless of whether the form is completely displayed.

This code can let us adjust the width and height of the Form snapshot, so later we wanted to print it, it would just settle its own to the way we had setting.

Share:
14,848
user1662306
Author by

user1662306

Updated on June 04, 2022

Comments

  • user1662306
    user1662306 almost 2 years

    I have a form which uses the PrintForm method that has been placed on a button.

    The following is the code which ensures the form is printed landscape, however it cuts a chunk off the left side.

    Me.PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = True
    PrintForm1.Print()
    

    I was wondering if there was a simple way to fit to one page?

  • Cody Gray
    Cody Gray about 11 years
    While some of these links may contain the answer to this question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. This will also help your answer remain useful even if the links you included break in the future.
  • Kasnady
    Kasnady about 11 years
    Thanks so much for your advice