2

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

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

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

我想知道是否有一种简单的方法可以适应一页?

4

1 回答 1

1

检查这个msdn 链接

这里

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

打印比屏幕大的表格

  1. 在 Visual Basic 中启动一个新的标准 EXE 项目。默认情况下创建 Form1。

  2. 将两个图片框添加到 Form1。

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

  3. 右键单击Picture2并选择Send to Back

  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. 无论表单是否完全显示,顶部和底部标签都应出现在各自位置

这段代码可以让我们调整表单快照的宽度和高度,所以稍后我们想打印它,它会按照我们设置的方式自行设置。

于 2013-04-04T03:36:34.557 回答