|
1 F$ l% R5 p7 p0 o* W/ c- d% v前几天写程序用到几个打印的API函数.把研究成果和大家分享 , w6 M" ~! `, A
建议大家结合MSDN看看
4 W- c6 r! l' B V5 H* ~ D6 QPrivate Type PRINTER_INFO_1
* @1 k9 S$ q J6 c M7 T3 s- V8 K Flags As Long
u% k' a7 \ U- k pDescription As Long }6 _8 |' l/ N. l/ `" g
pName As Long
) d/ `1 u: h4 { pComment As Long . U2 P% }* F8 j# x
End Type
/ R1 B9 W0 c n( qPrivate Declare Function PrinterProperties Lib "winspool.drv" (ByVal hWnd As
1 i) H4 X2 G) s/ R4 BLong, ByVal hPrinter As Long) As Long ! ]/ x- v# b! j! W
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As
: R0 E A- p3 Q& HLong) As Long
7 b+ t2 t( ]$ `, z( hPrivate Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA"
' Q3 L/ @! x# y3 U2 N" m2 Y(ByVal pPrinterName As String, phPrinter As Long, pDefault As Long) As Long
1 a R3 ?9 Y; h* jPrivate Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (
; R% p; x: `+ m* VByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As
0 @. K3 X1 B8 w4 RLong, pcbNeeded As Long) As Long
, `5 c( a: E. W- |Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
2 ?, f5 o& E* W (ByVal lpString1 As String, ByVal lpString2 As Long) As Long 0 s7 `% D6 R# h6 W
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpSt
, h8 |6 M, {# n3 G4 r- {ring As Long) As Long
0 d6 R0 A' w/ @# e& |# `( FGetPrinter函数返回的数据即我们想要的数据在参数pPrinter而且是一个地址指针指向 , U! c- f& k! R
一个PRINTER_INFO_1结构.结构的pDescription 是指向打印机描述字符串的指针. z3 ]0 E( r" t2 ~
在VB只好用CopyMemory,lstrcpy 和 lstrlen得到这个字符串. $ Q+ r! M' t4 _ d* O) @- F
下面是 例程:得到打印机的类型名 ) P2 ~ s$ g8 x. o; m c
'pDeviceName是打印机名 : R! e* N' @. e: a5 u; \
'pType 是返回的打印机 Description中的打印机类型.这个串里还有打印机名和驱动程序
+ c Y$ _# |% ?5 H+ n3 y# ZPrivate Sub GetPrint(pDeviceName, pType As String)
: H. f. I* ]/ _& M Dim pIn As PRINTER_INFO_1 7 X) w( c$ ?; m& @2 |9 c
Dim s As String * 256
: P( t2 @6 _0 J/ u, j7 ~9 T Dim r As Long, phPrinter As Long, r1 As Long # y# `0 P' x {" ]2 ]5 {8 k* \/ ]5 t
On Error Resume Next
4 G! }: v7 Z3 A/ l r = OpenPrinter(pDeviceName & Chr(0), phPrinter, 0&)
, O; @2 p2 H% l( d: U5 c If r = 0 Then Exit Sub 8 w5 x3 c H* ?0 b; P& H2 V) [
r = GetPrinter(phPrinter, 1, 0, 0, r1) 5 m% }7 j8 f" T' |$ O
ReDim pPrinter(r1) As Byte
' z4 h3 t" ~2 h- @ r = GetPrinter(phPrinter, 1, pPrinter(0), r1, r1) ) q0 @- b6 C$ e
If r = 0 Then Exit Sub : l' ~. y/ b9 J/ }8 r" p
r = ClosePrinter(phPrinter) 1 ~5 ?* \* G% z' m
CopyMemory pIn, pPrinter(0), Len(pIn)
0 R: Z3 |0 ]8 d6 p- d& m' V
& w. `% v8 W5 A7 X# W- p6 ] r1 = lstrlen(pIn.pDescription) 4 S/ O; K6 }. A* x4 ^/ R: R* b
r = lstrcpy(s, pIn.pDescription)
6 t4 u, K4 Y. |' w If r = 0 Then Exit Sub " a. ]! l: z$ ]7 I2 l% j% j2 L4 C
pType = Left(s, r1) - ?$ T* \' Y- {5 d
r1 = InStr(1, pType, ",")
+ ? H7 m; A; O7 S4 m r = InStr(r1 + 1, pType, ",") % ~ A) ]1 H' j) z+ \, o
pType = Mid(pType, r1 + 1, r - r1 - 1) ; g0 q0 e/ {- @, d' w
End Sub ; P9 z+ L0 X+ H- u" z' r
PrinterProperties函数比较简单.你试试吗? |