VB 獲取默認打印機的狀態


如何獲取默認打印機的狀態,包括缺紙、卡紙、無鏈接等狀態,還有將某文件打印後,如何得知打印成功? 
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函數

 

 

用vb編程怎麼得到打印機列表內容?spa

在VB中有一個Printers對象,包含了本機安裝的全部打印機。 For Each p In Printers    MsgBox p.DeviceNameNext
相關文章
相關標籤/搜索