如何将组框的内容发送到打印机
How to send contents of groupbox to printer
我有一个组框,里面有一些控件,我想将它发送到打印机。
我有这段代码可以从组框构建 bmp 文件。我怎样才能在点击按钮时将它发送到打印机?
Private Sub Doc_PrintPage(sender As Object, e As PrintPageEventArgs)
Dim x As Single = e.MarginBounds.Left
Dim y As Single = e.MarginBounds.Top
Dim bmp As New Bitmap(Me.GroupBox1.Width, Me.GroupBox1.Height)
Me.GroupBox1.DrawToBitmap(bmp, New Rectangle(0, 0, Me.GroupBox1.Width, Me.GroupBox1.Height))
e.Graphics.DrawImage(DirectCast(bmp, Image), x, y)
End Sub
我有按钮点击事件:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim doc As New PrintDocument()
doc = Doc_PrintPage()
Dim dlgSettings As New PrintDialog()
dlgSettings.Document = doc
If dlgSettings.ShowDialog() = DialogResult.OK Then
doc.Print()
End If
End Sub
建议后的最终工作代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BMP = New Bitmap(GroupBox1.Width, GroupBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
GroupBox1.DrawToBitmap(BMP, New Rectangle(0, 0, GroupBox1.Width, GroupBox1.Height))
Dim pd As New PrintDocument
Dim pdialog As New PrintDialog
AddHandler pd.PrintPage, (Sub(s, args)
args.Graphics.DrawImage(BMP, 0, 0)
args.HasMorePages = False
End Sub)
pdialog.ShowDialog()
pd.PrinterSettings.PrinterName = pdialog.PrinterSettings.PrinterName
pd.Print()
End Sub
这个想法是你有一个 PrintDocument
对象,你调用它的 Print
方法,它引发它的 PrintPage
事件,你处理那个事件并在你使用的处理程序方法中GDI+ 绘制要打印的任何内容。所以,你需要去掉这一行:
doc = Doc_PrintPage()
那可能在做什么?您正在尝试将方法的结果分配给 PrintDocument
变量。为了使它有意义,该方法必须 return 一个 PrintDocument
对象,而它不是。您需要做的是注册该方法来处理 PrintDocument
:
的 PrintPage
事件
AddHandler doc.PrintPage, AddressOf Doc_PrintPage
如果这样做,则还需要删除处理程序。更好的选择是将所有打印对象添加到设计器中的表单,然后为 PrintDocument
创建一个 PrintPage
事件处理程序,就像为 Button
创建事件处理程序一样TextBox
.
有关打印的详细信息,您可能会发现 this 有用。
我有一个组框,里面有一些控件,我想将它发送到打印机。
我有这段代码可以从组框构建 bmp 文件。我怎样才能在点击按钮时将它发送到打印机?
Private Sub Doc_PrintPage(sender As Object, e As PrintPageEventArgs)
Dim x As Single = e.MarginBounds.Left
Dim y As Single = e.MarginBounds.Top
Dim bmp As New Bitmap(Me.GroupBox1.Width, Me.GroupBox1.Height)
Me.GroupBox1.DrawToBitmap(bmp, New Rectangle(0, 0, Me.GroupBox1.Width, Me.GroupBox1.Height))
e.Graphics.DrawImage(DirectCast(bmp, Image), x, y)
End Sub
我有按钮点击事件:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim doc As New PrintDocument()
doc = Doc_PrintPage()
Dim dlgSettings As New PrintDialog()
dlgSettings.Document = doc
If dlgSettings.ShowDialog() = DialogResult.OK Then
doc.Print()
End If
End Sub
建议后的最终工作代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BMP = New Bitmap(GroupBox1.Width, GroupBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
GroupBox1.DrawToBitmap(BMP, New Rectangle(0, 0, GroupBox1.Width, GroupBox1.Height))
Dim pd As New PrintDocument
Dim pdialog As New PrintDialog
AddHandler pd.PrintPage, (Sub(s, args)
args.Graphics.DrawImage(BMP, 0, 0)
args.HasMorePages = False
End Sub)
pdialog.ShowDialog()
pd.PrinterSettings.PrinterName = pdialog.PrinterSettings.PrinterName
pd.Print()
End Sub
这个想法是你有一个 PrintDocument
对象,你调用它的 Print
方法,它引发它的 PrintPage
事件,你处理那个事件并在你使用的处理程序方法中GDI+ 绘制要打印的任何内容。所以,你需要去掉这一行:
doc = Doc_PrintPage()
那可能在做什么?您正在尝试将方法的结果分配给 PrintDocument
变量。为了使它有意义,该方法必须 return 一个 PrintDocument
对象,而它不是。您需要做的是注册该方法来处理 PrintDocument
:
PrintPage
事件
AddHandler doc.PrintPage, AddressOf Doc_PrintPage
如果这样做,则还需要删除处理程序。更好的选择是将所有打印对象添加到设计器中的表单,然后为 PrintDocument
创建一个 PrintPage
事件处理程序,就像为 Button
创建事件处理程序一样TextBox
.
有关打印的详细信息,您可能会发现 this 有用。