PrintForm适合一页(横向)

时间:2013-04-02 11:35:00

标签: .net vb.net winforms visual-studio-2010

我有一个表单,它使用放在按钮上的PrintForm方法。

以下是确保表单打印横向的代码,但它会从左侧切掉一块。

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

我想知道是否有一种简单的方法适合一个页面?

1 个答案:

答案 0 :(得分:1)

选中此msdn link

here

另外,我更建议使用打印预览,因为它可以调整边距。这是关于打印预览的link。 但是在这些链接之间,我会建议这段代码..

打印比屏幕大的表格

  1. 在Visual Basic中启动一个新的标准EXE项目。默认情况下会创建Form1。
  2. 将两个PictureBox添加到Form1。

    避免在第一个PictureBox中绘制第二个PictureBox,因为这样做会使第二个PictureBox成为第一个PictureBox的成员。而是将第二个PictureBox的原点放在第一个PictureBox原点的左侧。

  3. 右键点击 Picture2 ,然后选择发送至后方
  4. 向Picture1添加两个标签,将Picture2留空。
  5. 将以下代码添加到Form1的General Declarations部分:

    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. 运行项目。

  7. 顶部底部标签应出现在各自的位置,无论表单是否完整显示。
  8. 这段代码可以让我们调整Form快照的宽度和高度,所以稍后我们想要打印它,它只会按照我们设置的方式自行调整。