【问题标题】:PrintForm to fit to one page (Landscape)PrintForm 适合一页(横向)
【发布时间】:2013-04-02 11:34:59
【问题描述】:

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    查看msdn link

    here

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

    打印比屏幕大的表格

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

    2. 在 Form1 中添加两个 PictureBox。

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

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

    【讨论】:

    • 虽然其中一些链接可能包含此问题的答案,但it would be preferable 将在此处包含答案的基本部分,并提供链接以供参考。这也将有助于您的答案保持有用,即使您包含的链接将来会中断。
    • 非常感谢您的建议
    猜你喜欢
    • 2019-09-08
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 2023-03-17
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多