我在升级后将VB6升级到.NET我收到了编译错误:
Name 'Printer' is not declared
我在VB6中的代码是这样的:
THeight = Printer.TextHeight("#")
在.NET中声明打印机的正确方法是什么?
注意:我尝试下载打印机电源组,但无法使其正常工作。
答案 0 :(得分:5)
Visual Basic 6.0有一个固有的Printer对象,您可以在不明确声明的情况下使用它。相反,打印机兼容性库的行为与任何其他.NET Framework对象相同;您必须先显式声明.NET Framework Printer对象,然后才能使用它。
升级项目后,可以像这样添加Printer对象:
1)在“项目”菜单上,单击“添加引用”。
2)在“添加引用”对话框的“.NET”选项卡上,单击“Microsoft.VisualBasic.PowerPacks.Printing.Printer”,然后单击“确定”。
3)在代码编辑器中,在包含Visual Basic 6.0打印机代码的模块顶部添加以下语句:
导入Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6
4)在包含打印机代码的过程的顶部添加以下代码:
公共打印机作为新打印机
答案 1 :(得分:2)
在 System.Drawing.Printing 命名空间中查看 PrintDocument 。
您还可以找到涵盖您问题的教程here。
其他答案建议使用 Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6 命名空间,但根据MSDN:
此命名空间使Visual Basic 6.0 Printer代码无需在升级项目中进行修改即可运行;它不适用于新的开发。对于新开发,请使用PrintDocument组件。
答案 2 :(得分:1)
来自MSDN:
Dim Printer As New Printer
Dim msg As String = "String to measure"
Printer.Print(Printer.TextHeight(msg) & " by " & _
Printer.TextWidth(msg) & " twips")
Printer.EndDoc()
答案 3 :(得分:1)
VB.NET中的打印与VB6非常不同。这是让您入门的示例代码。我建议你考虑"打印"改为PDF,例如使用PDFSharp库。 PDFSharp更像是VB6打印机对象,您可以获得该文档的PDF作为额外的奖励。
''' <summary>
''' Bare bones printout
''' </summary>
''' <remarks></remarks>
Public Class SimplePrintout
'USAGE:
'Dim spo As New SimplePrintout
'spo.PrintPreview()
Public Sub Print(Optional ByVal PrinterName As String = "")
'create the document object
Using pdcNew As New Printing.PrintDocument
'wire up event handlers to handle pagination
AddHandler pdcNew.PrintPage, AddressOf PrintPage
Using docOutput As Printing.PrintDocument = pdcNew
If PrinterName > "" Then
docOutput.PrinterSettings.PrinterName = PrinterName
End If
docOutput.Print()
End Using
End Using
End Sub
''' <summary>
''' Preview the Report on screen
''' </summary>
''' <remarks></remarks>
Public Sub PrintPreview(Optional ByVal Owner As Form = Nothing)
'create the document object
Using pdcNew As New Printing.PrintDocument
'wire up event handlers to handle pagination
AddHandler pdcNew.PrintPage, AddressOf PrintPage
Using ppvPreview As New PrintPreviewDialog
ppvPreview.Document = pdcNew
ppvPreview.FindForm.WindowState = FormWindowState.Maximized
If IsNothing(Owner) Then
ppvPreview.ShowDialog()
Else
ppvPreview.ShowDialog(Owner)
End If
End Using
End Using
End Sub
Sub PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim g As Graphics = e.Graphics 'shortcut
Dim x As Single = e.MarginBounds.Left '"Cursor" location
Dim y As Single = e.MarginBounds.Top '"Cursor" location
'g.DrawRectangle(Pens.Black, e.MarginBounds) '>>DEBUG: use this line to check margins
Dim fnt1 As New Font(System.Drawing.FontFamily.GenericSansSerif, 12, FontStyle.Regular, GraphicsUnit.Point)
g.DrawString("Simple printout line 1" & vbCrLf & " after CRLF", fnt1, Brushes.Black, x, y)
y += fnt1.GetHeight(g)
y += fnt1.GetHeight(g)
g.DrawString("Simple printout line 2", fnt1, Brushes.Black, x, y)
y += fnt1.GetHeight(g)
g.DrawString("Simple printout line 3", fnt1, Brushes.Black, x, y)
y += fnt1.GetHeight(g)
e.HasMorePages = False
End Sub
End Class