|
5 \; ?. s- x) j- A: c s% C
前几天写程序用到几个打印的API函数.把研究成果和大家分享 6 e/ m1 b# q9 x' w# L5 C) `4 j9 G7 \
建议大家结合MSDN看看
i `0 f. V/ C$ cPrivate Type PRINTER_INFO_1 ! g) ]0 L& U1 [- h' A( U
Flags As Long
" p. ?+ A0 c0 F0 S pDescription As Long
$ g( z. l/ X5 e% W pName As Long , ?4 T7 V& Y" [1 M0 D8 `- x
pComment As Long 7 d% D/ B3 ^- K) U9 k5 u& G
End Type
8 Q G# x9 b# aPrivate Declare Function PrinterProperties Lib "winspool.drv" (ByVal hWnd As
- e* n# \! g+ Q2 T+ YLong, ByVal hPrinter As Long) As Long B- D1 p) Q2 L4 w- F( u% Z, |" c
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As
1 [( e/ u1 c. i: q! ULong) As Long
( U' c" O9 Q5 W- Z. OPrivate Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" 6 _4 u9 Z) i7 f" q+ I' W y
(ByVal pPrinterName As String, phPrinter As Long, pDefault As Long) As Long 1 {( F8 T, k6 H' M6 D
Private Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" ( 2 g- t7 r k; Z+ }5 T
ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As 0 H3 K) B+ s/ y) b& Y) _ X
Long, pcbNeeded As Long) As Long 6 P7 q3 C. i) y
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _ % }5 _4 r+ t, L4 G; j2 s* r
(ByVal lpString1 As String, ByVal lpString2 As Long) As Long ( M3 Z" @0 f5 i j- J6 M
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpSt
0 k5 K2 U, E! e3 l- Wring As Long) As Long
. X2 ^( p; f5 LGetPrinter函数返回的数据即我们想要的数据在参数pPrinter而且是一个地址指针指向 % j/ O5 u* F7 U. }$ z- I
一个PRINTER_INFO_1结构.结构的pDescription 是指向打印机描述字符串的指针.
0 @. c. A' y0 ~5 N$ W在VB只好用CopyMemory,lstrcpy 和 lstrlen得到这个字符串. 9 Q' a$ e& e6 E: O% M: ~ B) i) j; J4 B
下面是 例程:得到打印机的类型名
7 j# M& P @% |0 g" F2 O0 \'pDeviceName是打印机名
5 s& {8 A) u5 K4 p( O& c, h'pType 是返回的打印机 Description中的打印机类型.这个串里还有打印机名和驱动程序 2 N j3 L( |0 d b
Private Sub GetPrint(pDeviceName, pType As String) 5 ]8 ^5 @7 M% l* Q1 f( z( ?' ?& R
Dim pIn As PRINTER_INFO_1 3 ?. V% r6 H- _& R* p& Z- R D
Dim s As String * 256 1 ^3 c ~/ k" _1 a
Dim r As Long, phPrinter As Long, r1 As Long . |( F) I# ?: K, t% _( v* Q
On Error Resume Next # h5 d+ ~! c }: ^# m0 W
r = OpenPrinter(pDeviceName & Chr(0), phPrinter, 0&)
2 n* J/ E, H' T7 _+ T0 D If r = 0 Then Exit Sub
5 d4 s1 s0 u( f3 y r = GetPrinter(phPrinter, 1, 0, 0, r1) $ I: x* P) p7 F; J9 A
ReDim pPrinter(r1) As Byte 1 _0 x. H2 ~- z3 g
r = GetPrinter(phPrinter, 1, pPrinter(0), r1, r1) 7 ^: K" C. I% X7 w& o& w
If r = 0 Then Exit Sub 9 p+ c3 i9 a+ |, }
r = ClosePrinter(phPrinter)
" x; `9 n) O. A/ O5 R. P" c& h CopyMemory pIn, pPrinter(0), Len(pIn)
+ `- P5 s2 g Y' q
' z. l! l2 Q8 ~ r1 = lstrlen(pIn.pDescription) 1 ?# r$ q) q# @! g% a: Q5 a1 Q
r = lstrcpy(s, pIn.pDescription)
- c- X9 x0 S. C' n) ?! P If r = 0 Then Exit Sub
' H! D& l( R) O6 P c# ? pType = Left(s, r1)
5 o3 e" K. e3 L: @) {1 R" ^2 U r1 = InStr(1, pType, ",")
2 h( _: U* C! l; v r = InStr(r1 + 1, pType, ",") + s' y' j8 y3 K" w% \* J
pType = Mid(pType, r1 + 1, r - r1 - 1) ! b$ q+ A6 o, I8 R! M! Q& V% a( i
End Sub
/ }, h8 R8 s K' j; e% T! OPrinterProperties函数比较简单.你试试吗? |