几个打印机的API函数
<P> </P><P>前几天写程序用到几个打印的API函数.把研究成果和大家分享 </P>
<P>建议大家结合MSDN看看 </P>
<P>Private Type PRINTER_INFO_1 </P>
<P> Flags As Long </P>
<P> pDescription As Long </P>
<P> pName As Long </P>
<P> pComment As Long </P>
<P>End Type </P>
<P>Private Declare Function PrinterProperties Lib "winspool.drv" (ByVal hWnd As </P>
<P>Long, ByVal hPrinter As Long) As Long </P>
<P>Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As </P>
<P>Long) As Long </P>
<P>Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" </P>
<P>(ByVal pPrinterName As String, phPrinter As Long, pDefault As Long) As Long </P>
<P>Private Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" ( </P>
<P>ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As </P>
<P>Long, pcbNeeded As Long) As Long </P>
<P>Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _ </P>
<P> (ByVal lpString1 As String, ByVal lpString2 As Long) As Long </P>
<P>Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpSt </P>
<P>ring As Long) As Long </P>
<P>GetPrinter函数返回的数据即我们想要的数据在参数pPrinter而且是一个地址指针指向 </P>
<P>一个PRINTER_INFO_1结构.结构的pDescription 是指向打印机描述字符串的指针. </P>
<P>在VB只好用CopyMemory,lstrcpy 和 lstrlen得到这个字符串. </P>
<P>下面是 例程:得到打印机的类型名 </P>
<P>'pDeviceName是打印机名 </P>
<P>'pType 是返回的打印机 Description中的打印机类型.这个串里还有打印机名和驱动程序 </P>
<P>Private Sub GetPrint(pDeviceName, pType As String) </P>
<P> Dim pIn As PRINTER_INFO_1 </P>
<P> Dim s As String * 256 </P>
<P> Dim r As Long, phPrinter As Long, r1 As Long </P>
<P> On Error Resume Next </P>
<P> r = OpenPrinter(pDeviceName & Chr(0), phPrinter, 0&) </P>
<P> If r = 0 Then Exit Sub </P>
<P> r = GetPrinter(phPrinter, 1, 0, 0, r1) </P>
<P> ReDim pPrinter(r1) As Byte </P>
<P> r = GetPrinter(phPrinter, 1, pPrinter(0), r1, r1) </P>
<P> If r = 0 Then Exit Sub </P>
<P> r = ClosePrinter(phPrinter) </P>
<P> CopyMemory pIn, pPrinter(0), Len(pIn) </P>
<P> r1 = lstrlen(pIn.pDescription) </P>
<P> r = lstrcpy(s, pIn.pDescription) </P>
<P> If r = 0 Then Exit Sub </P>
<P> pType = Left(s, r1) </P>
<P> r1 = InStr(1, pType, ",") </P>
<P> r = InStr(r1 + 1, pType, ",") </P>
<P> pType = Mid(pType, r1 + 1, r - r1 - 1) </P>
<P>End Sub </P>
<P>PrinterProperties函数比较简单.你试试吗? </P>
页:
[1]