使用点阵打印机从Vb.net打印原始文本

时间:2011-08-10 08:18:26

标签: vb.net

我希望通过将每行的原始文本传递给vb.net中的点阵来逐行打印文本。可能吗

像c中的Printf之类的东西

1 个答案:

答案 0 :(得分:-1)

我不打算提供答案,但作为补充答案 -

以下是(非托管)VB6代码,它可以执行您想要的操作,但可以轻松转换为更友好的.NET:

      Public Type DOCINFO
          pDocName As String
          pOutputFile As String
          pDatatype As String
      End Type

Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long  
Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, ByVal pDefault As Long) As Long
Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pDocInfo As DOCINFO) As Long 
Declare Function StartPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long

Public Function PrintRawData(ByVal sPrinter As String, ByVal sDocName As String, ByVal sData As String) As Boolean
On Error GoTo PrintErr:

          Dim lhPrinter As Long, lReturn As Long, lpcWritten As Long
          Dim lDoc As Long, sWrittenData As String
          Dim MyDocInfo As DOCINFO
          Dim pOutput As Printer
          Dim p As Printer

            For Each p In Printers
                If p.DeviceName = sPrinter Then
                    Set pOutput = p
                    GoTo StartPrinting
                End If
            Next p

            MsgBox "Unable to find the specified printer [" & sPrinter & _
            "] in the list of currently installed printers" & vbCrLf & _
            "Printing will be aborted", vbCritical
            Exit Function

StartPrinting:
          lReturn = OpenPrinter(pOutput.DeviceName, lhPrinter, 0)

          If lReturn = 0 Then
              MsgBox "Print was unsuccessful. Make sure there is a printer installed on the port you are trying to print to"
              Exit Function
          End If

          MyDocInfo.pDocName = sDocName
          MyDocInfo.pOutputFile = vbNullString
          MyDocInfo.pDatatype = vbNullString
          lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
          Call StartPagePrinter(lhPrinter)

          sWrittenData = sData

          lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, Len(sWrittenData), lpcWritten)
          lReturn = EndPagePrinter(lhPrinter) 'need this??
          lReturn = EndDocPrinter(lhPrinter)
          lReturn = ClosePrinter(lhPrinter)

          Exit Function

PrintErr:
    MsgBox "Print was unsuccessful. Make sure there is a printer installed on the port you are trying to print to"
    Exit Function

End Function

要使用它,您需要使用任何驱动程序安装使用正确端口的打印机(我通常使用Generic / text Only驱动程序),然后按如下方式调用它,将Hello替换为要发送到打印机的数据包括控制字符等:

PrintRawData "Generic / Text Only", "My Document", "Hello"