vb.net 名称“打印机”未声明为 .NET 的 VB6
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26534205/
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
Name 'Printer' is not declared VB6 to .NET
提问by nate
I am upgrading VB6 to .NET after the upgrade I get the compile error:
升级后我将 VB6 升级到 .NET 我收到编译错误:
Name 'Printer' is not declared
My code in VB6 is something like this:
我在 VB6 中的代码是这样的:
THeight = Printer.TextHeight("#")
What is the correct way to declare a printer in .NET?
在 .NET 中声明打印机的正确方法是什么?
Note: I tried to download the printer power pack, but was unable to get it to work.
注意:我尝试下载打印机电源组,但无法使其工作。
回答by nate
Visual Basic 6.0 had an intrinsic Printer object that you could use without explicitly declaring it. In contrast, the Printer Compatibility library behaves like any other .NET Framework object; you must explicitly declare a .NET Framework Printer object before you can use it.
Visual Basic 6.0 有一个固有的 Printer 对象,您无需显式声明即可使用它。相比之下,打印机兼容性库的行为与任何其他 .NET Framework 对象一样;您必须先显式声明 .NET Framework 打印机对象,然后才能使用它。
After you upgrade your project, you could add Printer object like this:
升级项目后,您可以像这样添加打印机对象:
1) On the Project menu, click Add Reference.
1) 在项目菜单上,单击添加引用。
2) In the Add Reference dialog box, on the .NET tab, click Microsoft.VisualBasic.PowerPacks.Printing.Printer, and then click OK.
2) 在“添加引用”对话框的“.NET”选项卡上,单击“Microsoft.VisualBasic.PowerPacks.Printing.Printer”,然后单击“确定”。
3) In the Code Editor, add the following statement at the top of the module that contains your Visual Basic 6.0 Printer code:
3) 在代码编辑器中,在包含 Visual Basic 6.0 打印机代码的模块顶部添加以下语句:
Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6
导入 Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6
4) Add the following code at the top of the procedure that contains Printer code:
4) 在包含打印机代码的程序顶部添加以下代码:
Public Printer As New Printer
公共打印机作为新打印机
回答by Stijn
Take a look at PrintDocumentin the System.Drawing.Printingnamespace.
看看的PrintDocument在System.Drawing.Printing命名空间。
You can also find a tutorial here, which covers your problem.
您还可以在此处找到教程,其中涵盖了您的问题。
The other answers are suggesting using the Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6namespace, but as per MSDN:
其他答案建议使用Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6命名空间,但根据 MSDN:
This namespace enables Visual Basic 6.0 Printer code to run without modification in an upgraded project; it is not intended for new development. For new development, use the PrintDocument component.
此命名空间使 Visual Basic 6.0 打印机代码无需修改即可在升级项目中运行;它不是为了新的发展。对于新开发,请使用 PrintDocument 组件。
回答by jac
From MSDN:
来自 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()
Full documentation here: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.powerpacks.printing.compatibility.vb6.printer.textheight.aspx
完整文档在这里:http: //msdn.microsoft.com/en-us/library/microsoft.visualbasic.powerpacks.printing.compatibility.vb6.printer.textheight.aspx
回答by SSS
Printing in VB.NET is very different to VB6. Here's sample code to get you started. I'd recommend you consider "printing" to PDF instead, e.g. using the PDFSharp library. PDFSharp is more like the VB6 printer object, and you get a PDF of the document as an added bonus.
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

