|
8 I& M0 t% E& r, h
前几天写程序用到几个打印的API函数.把研究成果和大家分享 , m# N! ~5 t7 @9 k
建议大家结合MSDN看看
: V. h! A$ @( @% m% u. [Private Type PRINTER_INFO_1 $ j* \( @/ L9 U$ p
Flags As Long
$ R- E% j/ ]4 k( x7 b4 o4 C pDescription As Long 7 i7 V0 Q2 g0 r# S4 f
pName As Long
4 c0 T% w- q% g& @# |" H; D pComment As Long
. h9 ^% `- w- U2 f. aEnd Type
. `) o1 w) z T U. ]% |Private Declare Function PrinterProperties Lib "winspool.drv" (ByVal hWnd As
# \* h, d2 c1 q4 w/ G) e" I+ ZLong, ByVal hPrinter As Long) As Long
* V% r9 s* Z# s5 bPrivate Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As
2 v _9 }. k- S* b0 _/ z9 @Long) As Long
# k; A( n# Z Y# h" p7 qPrivate Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" 5 i, V; x7 b! l; e0 z
(ByVal pPrinterName As String, phPrinter As Long, pDefault As Long) As Long ' ~0 u* e: n: _5 _# Z" l; Q
Private Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (
7 W8 }: ^5 A" n F; nByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As
0 J1 ] p2 J; p3 d. N' E1 [9 W; f. ZLong, pcbNeeded As Long) As Long / S: K. S& T5 z+ v( ~. Q9 t0 h
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _ + e# x: t( b2 K9 W
(ByVal lpString1 As String, ByVal lpString2 As Long) As Long
: }+ @8 |( Z9 q4 {% @. O- sPrivate Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpSt
& }) `. I# `+ L6 qring As Long) As Long 2 R! R; L$ p4 t
GetPrinter函数返回的数据即我们想要的数据在参数pPrinter而且是一个地址指针指向
) P' o2 i) e5 |' h# ]! w! r% k一个PRINTER_INFO_1结构.结构的pDescription 是指向打印机描述字符串的指针.
( E8 ?$ D# y, Q2 l: ^0 v在VB只好用CopyMemory,lstrcpy 和 lstrlen得到这个字符串.
$ ?/ d1 C$ H: }% e8 U9 G下面是 例程:得到打印机的类型名
& W) J/ O7 g* Q9 u% o'pDeviceName是打印机名
3 ?% _) J0 b3 i6 d2 l( J l'pType 是返回的打印机 Description中的打印机类型.这个串里还有打印机名和驱动程序 7 `8 P, a j" t3 H
Private Sub GetPrint(pDeviceName, pType As String) 9 c7 e$ L- B, }& i& w
Dim pIn As PRINTER_INFO_1
: U+ n; ~$ G4 P9 _ Dim s As String * 256
* l7 v! R( G9 g Dim r As Long, phPrinter As Long, r1 As Long ) P0 T1 H& z1 z& l% j7 s- v$ V! s
On Error Resume Next
; v' B# Q6 w+ C8 n3 Z r = OpenPrinter(pDeviceName & Chr(0), phPrinter, 0&)
' c- `/ T% J2 |2 v9 d If r = 0 Then Exit Sub
5 Q+ {- B. B& F$ Y) w r = GetPrinter(phPrinter, 1, 0, 0, r1)
" \, ?5 X$ k: o; t& |9 {/ T ReDim pPrinter(r1) As Byte
[) x# R( z: P3 X4 X% |- c0 w r = GetPrinter(phPrinter, 1, pPrinter(0), r1, r1) / }+ o* ?7 b5 Q. _$ Q/ D
If r = 0 Then Exit Sub 7 H: V% V, x2 ~3 a* t8 j
r = ClosePrinter(phPrinter) 9 p ~. j1 U9 L) q7 Q& N8 E
CopyMemory pIn, pPrinter(0), Len(pIn) 4 y; X( o5 \4 F; L
. B0 W R+ p2 W- z
r1 = lstrlen(pIn.pDescription) # B! b; y4 I; q
r = lstrcpy(s, pIn.pDescription)
! i. f' n9 p' |# G9 y! l If r = 0 Then Exit Sub
- |0 {% W ]) l0 ~* ?+ { pType = Left(s, r1)
! j9 w6 \! p7 j; } r1 = InStr(1, pType, ",")
6 z7 ^. ^5 m$ _% z. U0 D0 z r = InStr(r1 + 1, pType, ",")
- J. Q8 }7 ?0 `+ i% t pType = Mid(pType, r1 + 1, r - r1 - 1)
4 X& h8 d+ J7 x4 o2 j& D# x# o: PEnd Sub
\4 D' I6 K7 R9 [3 cPrinterProperties函数比较简单.你试试吗? |