Private Sub PrintXSD(lngXsid As Long)
On Error GoTo PrintErr
Open "LPT1" For Output As #1
Print #1, "XXX服装专卖店"
Print #1, "--------------------------------"
'此外省略....................................
Close #1
Exit_PrintXSD:
Exit Sub
PrintErr:
MsgBox Err.Number & "--" & Err.Description
Resume Exit_PrintXSD
End Sub
Dim Status As String
Dim StatusString As String
Dim TimeUp As Boolean
Private Sub Command1_Click()Sub Command1_Click()Sub Command1_Click()Sub Command1_Click()
On Error Resume Next
Dim i As Long
Status = "" '状态返回值
StatusString = ""
' Poll for 8 byte status
Do While Len(Status) < 8
DoEvents
Status = MSComm1.Input
Loop
For i = 1 To 9
StatusString = StatusString & Right("00" & " " & Hex(Asc(Mid$(Status, i, 1))), 2)
'StatusString = StatusString & Hex(Asc(Mid$(Status, i, 1))) & " "
Next i
Text1.Text = StatusString
MSComm1.PortOpen = False
End Sub
Private Sub Form_Unload()Sub Form_Unload()Sub Form_Unload()Sub Form_Unload(Cancel As Integer)
Close
End Sub
Private Sub Wait()Sub Wait()Sub Wait()Sub Wait(Interval As Long)
Timer1.Interval = Interval
Timer1.Enabled = True
TimeUp = False
Do
DoEvents
Loop While Not TimeUp
End Sub
Private Sub Timer1_Timer()Sub Timer1_Timer()Sub Timer1_Timer()Sub Timer1_Timer()
Timer1.Interval = 0
Timer1.Enabled = False
TimeUp = True
End Sub
----------------------------------------------------------------------
2号代码:
Option Explicit
Declare Function MapPhysToLin Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysSize As Long, ByRef PhysMemHandle) As Long
Declare Function UnmapPhysicalMemory Lib "WinIo.dll" (ByVal PhysMemHandle, ByVal LinAddr) As Boolean
Declare Function GetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByRef PhysVal As Long) As Boolean
Declare Function SetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysVal As Long) As Boolean
Declare Function GetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByRef PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function SetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByVal PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function InitializeWinIo Lib "WinIo.dll" () As Boolean
Declare Function ShutdownWinIo Lib "WinIo.dll" () As Boolean
Declare Function InstallWinIoDriver Lib "WinIo.dll" (ByVal DriverPath As String, ByVal Mode As Integer) As Boolean
Declare Function RemoveWinIoDriver Lib "WinIo.dll" () As Boolean
Public IOStat As Boolean
'************************************************************
'* 函数名称:GetPrnStat *
'* 功能:根据打印机的内存地址,检测打印机的目前工作状态 *
'* 参数:lptport: 要检测的打印机的端口号,如LPT1: *
'* 返回值:打印机的工作状态值。 *
'* 0:正常 1:缺纸 2:无联系 3:异常(其他错误) *
'* 调用:本模块中的API函数InitializeWinIo和GetPortVal *
'* 备注:检测的内存地址,是在打印端口所在的基地址上加1; *
'* 作者:谷霖 *
'* LPT1口的基地址为&H378;LPT2口的基地址为&H278 *
'************************************************************
Public Function GetPrnStat(ByVal LptPort As String) As Long
Dim PrnAddr As Long
On Error Resume Next
If IOStat = False Then IOStat = InitializeWinIo()
If IOStat Then
If UCase(LptPort) = "LPT1:" Then
PrnAddr = &H379
ElseIf UCase(LptPort) = "LPT2:" Then
PrnAddr = &H279
End If
GetPortVal PrnAddr, GetPrnStat, 1
Else
GetPrnStat = &HFF
End If
GetPrnStat = GetPrnStat And &HF8
Select Case GetPrnStat
Case &H68, &H58, &H70
GetPrnStat = 1 '缺纸
Case &H78
GetPrnStat = 2 '无联系
Case &HD8
GetPrnStat = 0 '正常
Case Else
GetPrnStat = 3 '异常
End Select
End Function
'*************************************************************************
'* 函数功能:检查打印机的状态主函数 *
'* 输入参数:PrintName 要检测的打印机名称 *
'* 输出参数:checkprinterr *
'* 检查结果(0:正常 1:打印机缺纸 2:打印机无联系 3:打印机异常 *
'* 4:没有安装打印机 5:打印机名称错误) *
'*************************************************************************
Public Function CheckPrintErr(ByVal PrintName As String) As Long
'CheckPrintErr参数说明
'0:没有错误
'1:打印机无联系
'2:打印机缺纸
'3:没有安装打印机
Dim printjieguo As Long
Dim i As Long, k As Long
On Error GoTo ErrCheckPrint
If Printers.Count = 0 Then
CheckPrintErr = 4 '没有安装打印机
Exit Function
End If
'检测发票打印机是否可以联系
For i = 0 To Printers.Count - 1
If (Printers(i).DeviceName = PrintName) Then
k = k + 1
Exit For
End If
Next
If k = 0 Then '打印机名称错误
CheckPrintErr = 5
Exit Function
End If
Set Printer = Printers(i)
CheckPrintErr = GetPrnStat(Printer.Port)
Exit Function
ErrCheckPrint:
CheckPrintErr = 3
Exit Function
End Function
Private Type DOCINFO
pDocName As String
pOutputFile As String
pDatatype As String
End Type
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
"OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
ByVal pDefault As Long) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" Alias _
"StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _
pcWritten As Long) As Long作者: zyp 时间: 2011-2-15 09:26 本帖最后由 zyp 于 2011-2-15 10:08 编辑
谢谢tmtony 站长
我测试了一下,
StartDocPrinter Lib "winspool.drv" Alias _
"StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
pDocInfo As DOCINFO) As Long
代码中那个hPrinter参数为要进行打印的打印机(指定一个已打开的打印机的句柄), baidu一下好像需要用到:
Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
"OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
ByVal pDefault As Long) As Long
来取得, 这对于不安装驱动程序而是直接输出到LPT1并口的不知如何取得lhPrinter
另外,对于pDocInfo这个参数的使用也不得要领;