|
! D$ v3 ?! D& R0 [% n1 a1 w
前几天写程序用到几个打印的API函数.把研究成果和大家分享 8 o) H2 W j! t# c- m
建议大家结合MSDN看看 ) T0 r7 T: i$ R' K2 _
Private Type PRINTER_INFO_1
0 f4 g+ ~8 g2 W' t Flags As Long
$ G6 W6 R" r( ^7 I pDescription As Long
& i p# ^" n d: u) F& q4 I pName As Long " O9 O3 |5 h+ t% C
pComment As Long
# N) a! d' ]& X, P qEnd Type
8 ^( J2 K0 J( R" d; oPrivate Declare Function PrinterProperties Lib "winspool.drv" (ByVal hWnd As
7 a: ^) x; D, I; {: u! z3 eLong, ByVal hPrinter As Long) As Long : m, x* ?: W" U
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As
# {* N }3 m. {# e" k- VLong) As Long ' S3 B$ T3 `7 R0 b# j, J
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" - Q6 \9 i. g2 W5 b& V6 A
(ByVal pPrinterName As String, phPrinter As Long, pDefault As Long) As Long
3 _& x! ^% F( i5 XPrivate Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (
& D, M, x' ?4 _0 q7 \8 fByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As ) S- V% ~( E/ \0 {2 t/ d# s
Long, pcbNeeded As Long) As Long
6 R- D9 u6 U7 Y& D8 xPrivate Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
- n' ]* _+ f1 x# E7 d' s) S" g (ByVal lpString1 As String, ByVal lpString2 As Long) As Long . e2 D0 s. _6 J3 q5 Y
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpSt 5 L, P' z, J$ ^
ring As Long) As Long ( e4 g9 @. Q* ~, R) F- E0 `/ m
GetPrinter函数返回的数据即我们想要的数据在参数pPrinter而且是一个地址指针指向
' h; P( c$ {& X( ]6 J8 }一个PRINTER_INFO_1结构.结构的pDescription 是指向打印机描述字符串的指针.
9 H& j$ r) e' r) _ y( X在VB只好用CopyMemory,lstrcpy 和 lstrlen得到这个字符串. `3 c' A# }& o3 J1 }
下面是 例程:得到打印机的类型名
~2 _- B; b* v- m8 r'pDeviceName是打印机名
. r/ x5 l. B' s0 H) P/ T'pType 是返回的打印机 Description中的打印机类型.这个串里还有打印机名和驱动程序 6 }1 ?1 F; w" t1 ^7 Q! J
Private Sub GetPrint(pDeviceName, pType As String) - u7 I9 q. o8 O; X. U% Q, _
Dim pIn As PRINTER_INFO_1 ; D+ P3 [7 x; Z5 |
Dim s As String * 256
2 H9 h) y" I/ q4 Q$ d2 y% I Dim r As Long, phPrinter As Long, r1 As Long
! `: _& g" {% v+ I* s4 \7 { On Error Resume Next
' A8 Y1 F2 _7 B3 s r = OpenPrinter(pDeviceName & Chr(0), phPrinter, 0&) : q0 P9 E. ? |% r- o& K
If r = 0 Then Exit Sub 4 }6 V. n1 T+ n6 o
r = GetPrinter(phPrinter, 1, 0, 0, r1) 3 O! n9 M- D8 }$ j( {% a7 H. f6 d; a
ReDim pPrinter(r1) As Byte
* M: ?; b9 P, [: b3 g/ l7 R/ o r = GetPrinter(phPrinter, 1, pPrinter(0), r1, r1) : U9 }: q1 J2 g, [3 j3 a$ c: }
If r = 0 Then Exit Sub 6 e' i9 x( N Z& P
r = ClosePrinter(phPrinter)
# G% \: L; D* o0 i6 i4 E& F+ u CopyMemory pIn, pPrinter(0), Len(pIn)
1 z: k N/ \8 _5 _ U, I# F" D
" r+ b& e- |) T* L/ S# P r1 = lstrlen(pIn.pDescription)
' O8 q0 B& o* N$ a; H) M r = lstrcpy(s, pIn.pDescription)
: R% X7 ^% W' U7 M0 j If r = 0 Then Exit Sub # L3 |. u3 d! M2 F: v. t- Q# R
pType = Left(s, r1) ! O G5 y* c" l& O( R2 |$ p+ Y
r1 = InStr(1, pType, ",") 4 E8 p+ A% ^: r$ J
r = InStr(r1 + 1, pType, ",")
% ~7 _+ ]; x1 m* p" @. i pType = Mid(pType, r1 + 1, r - r1 - 1) ! u$ C! M j9 `# o2 J- k; j( B
End Sub 1 a0 T9 U+ C9 w5 d, N: s
PrinterProperties函数比较简单.你试试吗? |