vb.net 我如何打印我的表格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/966596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:15:10  来源:igfitidea点击:

how do i print my form?

vb.net

提问by l--''''''---------''''''''''''

I need to print an image of my VB.Net Windows.Formwhen the user clicks a button. Is there a good method for doing this?

Windows.Form当用户单击按钮时,我需要打印我的 VB.Net 的图像。有没有好的方法可以做到这一点?

回答by Andrew Hare

You need to use the PrintFormcomponent. Please see How to: Print a Form by Using the PrintForm Component:

您需要使用该PrintForm组件。请参阅如何:使用 PrintForm 组件打印表单

The PrintForm component enables you to quickly print an image of a form exactly as it appears on screen without using a PrintDocument component. The following procedures show how to print a form to a printer, to a print preview window, and to an Encapsulated PostScript file.

PrintForm 组件使您能够在不使用 PrintDocument 组件的情况下快速打印与屏幕上显示的完全相同的表单图像。以下过程显示如何将表单打印到打印机、打印预览窗口和封装的 PostScript 文件。

回答by spacemonkeys

If you want to amend the image the following code will capture the bitmap and send it to the printer (On the Button1 Click)

如果要修改图像,以下代码将捕获位图并将其发送到打印机(在 Button1 单击上)

Imports System.Drawing.Printing

Public Class Form1

Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap


Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
    ' Draw the image centered.
    Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width - mPrintBitMap.Width) \ 2
    Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height - mPrintBitMap.Height) \ 2
    e.Graphics.DrawImage(mPrintBitMap, lWidth, lheight)

    ' There's only one page.
    e.HasMorePages = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Copy the form's image into a bitmap.
    mPrintBitMap = New Bitmap(Me.Width, Me.Width)
    Dim lRect As System.Drawing.Rectangle
    lRect.Width = Me.Width
    lRect.Height = Me.Width
    Me.DrawToBitmap(mPrintBitMap, lRect)


    ' Make a PrintDocument and print.
    mPrintDocument = New PrintDocument
    mPrintDocument.Print()
End Sub
End Class

回答by jvenema

If you really want a snapshot of the form, you'll have to simulate a screen capture. Here's a samplethat'll help. It's going to be pretty ugly though, so you may want to consider making it a report using a PDF or report builder.

如果您真的想要表单的快照,则必须模拟屏幕截图。这是一个可以提供帮助的示例。不过它会非常难看,因此您可能需要考虑使用 PDF 或报告生成器将其制作为报告。