⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 apiprinter.cls

📁 几个不错的VB例子
💻 CLS
📖 第 1 页 / 共 2 页
字号:


Public Property Get DefaultPriority() As Long

Call RefreshPrinterInfo(2)
DefaultPriority = mPRINTER_INFO_2.DefaultPriority

End Property

Public Property Get Description() As String

Call RefreshPrinterInfo(1)
Description = mPRINTER_INFO_1.pDescription

End Property


Public Property Get DeviceContext() As ApiDeviceContext

Dim hPrinterDC As Long
Dim DCPrinter As New ApiDeviceContext

hPrinterDC = CreateDCByLong("WINSPOOL", Me.DeviceName, vbNullString, vbNull)
If Err.LastDllError Then
    ReportError Err.LastDllError, "ApiPrinter:DeviceContext", GetLastSystemError
End If

Set DeviceContext = DCPrinter

End Property

Public Property Get DeviceName() As String

Call RefreshPrinterInfo(1)
DeviceName = mPRINTER_INFO_1.pName

End Property

Public Property Let DeviceName(ByVal newname As String)

Dim lret As Long
Dim pDef As PRINTER_DEFAULTS

If mhPrinter = 0 Then
    '\\ Need to get the printer handle for the printer device named
    lret = OpenPrinter(newname, mhPrinter, pDef)
End If

End Property

Public Property Get Direct() As Boolean

Direct = Attributes And PRINTER_ATTRIBUTE_DIRECT

End Property

Public Property Get DriverName() As String

Call RefreshPrinterInfo(2)
DriverName = mPRINTER_INFO_2.pDriverName

End Property

Public Property Get Location() As String

Call RefreshPrinterInfo(2)
Location = mPRINTER_INFO_2.pLocation

End Property

Public Property Get Parameters() As String

Call RefreshPrinterInfo(2)
Parameters = mPRINTER_INFO_2.pParameters

End Property

Public Property Get PendingJobsCount() As Long

Call RefreshPrinterInfo(2)
PendingJobsCount = mPRINTER_INFO_2.JobsCount

End Property

Public Property Get PortName() As String

Call RefreshPrinterInfo(2)
PortName = mPRINTER_INFO_2.pPortName

End Property

Friend Property Let PrinterHandle(ByVal newhandle As Long)

If newhandle <> mhPrinter Then
    mhPrinter = newhandle
End If

End Property


Public Property Get PrintJobs() As Collection

Dim colThis As Collection
Dim oPrintJob As ApiPrintJob

Dim lret As Long
Dim pcbSizeRequired As Long, pcbBytesReturned As Long
Dim pJobId As Long

Dim buffer() As Long

'\\ Initialise the collection of print job objects
Set colThis = New Collection

ReDim Preserve buffer(0) As Long

lret = EnumJobs(mhPrinter, 0, 255, 1, buffer(0), UBound(buffer), pcbSizeRequired, pcbBytesReturned)

If pcbSizeRequired > 0 Then
    
    '\\ Need to resize our array to cope with this data
    ReDim Preserve buffer(0 To (pcbSizeRequired / 4) + 3) As Long
    lret = EnumJobs(mhPrinter, 0, 255, 1, buffer(0), UBound(buffer) * 4, pcbSizeRequired, pcbBytesReturned)
    
    '\\ Note any error if this has failed
    If Err.LastDllError <> 0 Then
        Call ReportError(Err.LastDllError, "ApiPrinter:PrintJobs", GetLastSystemError)
    End If
    
    '\\ At this stage buffer() contains an array of JOB_INFO_1 structures
    '\\ For each job...
    For pJobId = 0 To (pcbBytesReturned - 1) 'each record is 16 bytes long
        Set oPrintJob = New ApiPrintJob
        With oPrintJob
            .PrinterHandle = mhPrinter
            .JobId = buffer(pJobId * 16)
        End With
        colThis.Add oPrintJob
    Next pJobId
    
End If

'\\ pass the collection to the caller
Set PrintJobs = colThis

End Property

Public Property Get PrintProcessor() As String

Call RefreshPrinterInfo(2)
PrintProcessor = mPRINTER_INFO_2.pPrintProcessor

End Property

Public Property Get Priority() As Long

Call RefreshPrinterInfo(2)
Priority = mPRINTER_INFO_2.Priority

End Property

Public Property Get Queued() As Boolean

Queued = Attributes And PRINTER_ATTRIBUTE_QUEUED

End Property


Private Sub RefreshPrinterInfo(ByVal index As Integer)

Dim lret As Long
Dim SizeNeeded As Long

Dim buffer() As Long

ReDim Preserve buffer(0 To 1) As Long
lret = GetPrinterApi(mhPrinter, index, buffer(0), UBound(buffer), SizeNeeded)
ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long
lret = GetPrinterApi(mhPrinter, index, buffer(0), UBound(buffer) * 4, SizeNeeded)


If index < 1 Or index > 9 Then
    Debug.Print "Error in call to ApiPrinter.RefreshPrinterInfo - invalid index"
Else
    Select Case index
    Case 1
        With mPRINTER_INFO_1
            .flags = buffer(0)
            .pDescription = StringFromPointer(buffer(1), 1024)
            .pName = StringFromPointer(buffer(2), 1024)
            .pComment = StringFromPointer(buffer(3), 1024)
        End With
    Case 2
        With mPRINTER_INFO_2
            .pServerName = StringFromPointer(buffer(0), 1024)
            .pPrinterName = StringFromPointer(buffer(1), 1024)
            .pShareName = StringFromPointer(buffer(2), 1024)
            .pPortName = StringFromPointer(buffer(3), 1024)
            .pDriverName = StringFromPointer(buffer(4), 1024)
            .pComment = StringFromPointer(buffer(5), 1024)
            .pLocation = StringFromPointer(buffer(6), 1024)
            .pDevMode = buffer(7)
            .pSepFile = StringFromPointer(buffer(8), 1024)
            .pPrintProcessor = StringFromPointer(buffer(9), 1024)
            .pDatatype = StringFromPointer(buffer(10), 1024)
            .pParameters = StringFromPointer(buffer(11), 1024)
            .pSecurityDescriptor = buffer(12)
            .Attributes = buffer(13)
            .Priority = buffer(14)
            .DefaultPriority = buffer(15)
            .StartTime = buffer(16)
            .UntilTime = buffer(17)
            .Status = buffer(18)
            .JobsCount = buffer(19)
            .AveragePPM = buffer(20)
        End With
    Case 3
        With mPRINTER_INFO_3
            .pSecurityDescriptor = buffer(0)
        End With
    Case 4
        With mPRINTER_INFO_4
            .pPrinterName = StringFromPointer(buffer(0), 1024)
            .pServerName = StringFromPointer(buffer(1), 1024)
            .Attributes = buffer(2)
        End With
    Case 5
        With mPRINTER_INFO_5
            .pPrinterName = StringFromPointer(buffer(0), 1024)
            .pPortName = StringFromPointer(buffer(1), 1024)
            .Attributes = buffer(2)
            .DeviceNotSelectedTimeout = buffer(3)
            .TransmissionRetryTimeout = buffer(4)
        End With
    Case 6
        With mPRINTER_INFO_6
            .dwStatus = buffer(0)
        End With
    Case 7
        With mPRINTER_INFO_7
            .pszObjectGUID = StringFromPointer(buffer(0), 1024)
            .dwAction = buffer(1)
        End With
    Case 8
        With mPRINTER_INFO_8
            .pDevMode = buffer(0)
        End With
    Case 9
        '\\ Currently this structure is the same as 8...
        With mPRINTER_INFO_9
            .pDevMode = buffer(0)
        End With
    End Select
    If Err.LastDllError <> 0 Then
        ReportError Err.LastDllError, "ApiPrinter:RefreshPrinterInfo", GetLastSystemError
    End If
End If

End Sub

Public Property Get SeparatorFile() As String

Call RefreshPrinterInfo(2)
SeparatorFile = mPRINTER_INFO_2.pSepFile

End Property

Public Property Get ServerName() As String

Call RefreshPrinterInfo(4)
ServerName = mPRINTER_INFO_4.pServerName

End Property


Public Property Get ShareName() As String

   Call RefreshPrinterInfo(2)
   ShareName = mPRINTER_INFO_2.pShareName

End Property


'\\ --[Status]---------------------------------------------------------------------------------
'\\ Returns the status of the selected printer when it is called.
'\\ -------------------------------------------------------------------------------------------
Public Property Get Status() As Printer_Status

    Call RefreshPrinterInfo(2)
    Status = mPRINTER_INFO_2.Status

End Property


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -