|
# V& P: a+ Q1 k( W0 U v
前几天写程序用到几个打印的API函数.把研究成果和大家分享
. Z! ~2 _/ W; a1 g1 A建议大家结合MSDN看看 . S: D9 D) e* l. Q9 d. _
Private Type PRINTER_INFO_1
3 S! K6 e4 r7 i0 a2 Y Flags As Long
: n( D; v) r) I( ]! y pDescription As Long 7 D2 a Y' u8 j: f1 J* P {' P
pName As Long & P% Y0 U g3 d& g
pComment As Long 5 |2 \7 i3 j7 d+ O6 V. D) b
End Type
/ q G3 j3 ~1 _( J9 {Private Declare Function PrinterProperties Lib "winspool.drv" (ByVal hWnd As
/ {4 q4 G8 K3 d6 qLong, ByVal hPrinter As Long) As Long 2 X( f! N" k" i: c, ?" o. P3 W y
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As
/ _; _, U7 t* ?9 `Long) As Long & F% j0 W, e1 o8 {: r6 r
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" 8 F' W9 [" t, W) x7 j; `
(ByVal pPrinterName As String, phPrinter As Long, pDefault As Long) As Long
. q' S8 V6 G3 RPrivate Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (
2 _) Y0 d7 a1 m8 `) FByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As 5 G, \) W. J, W5 |' P; x- q$ [
Long, pcbNeeded As Long) As Long
. Y0 O( g6 B% w l/ j; h6 H0 C( T+ CPrivate Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
6 s; D! B8 V$ f' C6 @ (ByVal lpString1 As String, ByVal lpString2 As Long) As Long 7 k! m# O9 h1 e! O2 M
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpSt
( f' t; g% E. Y* `2 H( R5 A' N" @7 mring As Long) As Long . R1 @+ y3 I; l' a; c3 u8 {
GetPrinter函数返回的数据即我们想要的数据在参数pPrinter而且是一个地址指针指向 + ]5 p% e, b+ E& B* A; B
一个PRINTER_INFO_1结构.结构的pDescription 是指向打印机描述字符串的指针. 2 G" V0 z# j$ g% L
在VB只好用CopyMemory,lstrcpy 和 lstrlen得到这个字符串.
* H0 `) s8 g9 Q# F, j下面是 例程:得到打印机的类型名 & \5 S; Q5 k2 v
'pDeviceName是打印机名 ! J6 P% y7 K( O' C$ h/ |
'pType 是返回的打印机 Description中的打印机类型.这个串里还有打印机名和驱动程序 - k, U+ f9 Q/ z- ?" P; b
Private Sub GetPrint(pDeviceName, pType As String) + g m8 m' T4 {; a. Y ^
Dim pIn As PRINTER_INFO_1 - r+ o4 }0 S2 i- G% A
Dim s As String * 256 + @, c1 D% C0 [, U) c- G Y T, R, L
Dim r As Long, phPrinter As Long, r1 As Long
9 f% q( Y! p/ Z8 q+ a On Error Resume Next
$ a& E" S4 `, P& s0 M r = OpenPrinter(pDeviceName & Chr(0), phPrinter, 0&)
" k8 ^0 v2 N# N: ^. A If r = 0 Then Exit Sub 3 O% n8 @2 M5 j; q# {4 w4 _" y
r = GetPrinter(phPrinter, 1, 0, 0, r1)
' Q4 i) u4 `' O6 R" B7 Q1 R ReDim pPrinter(r1) As Byte 8 o3 s* D: x) U6 I; D4 t; O8 o8 _# P
r = GetPrinter(phPrinter, 1, pPrinter(0), r1, r1)
8 u7 `3 S4 V$ u. h If r = 0 Then Exit Sub " a& n1 B7 n: R% Y; B$ x
r = ClosePrinter(phPrinter)
/ _5 c9 x V; f CopyMemory pIn, pPrinter(0), Len(pIn)
J3 J M- j5 R! C+ \8 h& [7 m& @; r& H" U, a- f
r1 = lstrlen(pIn.pDescription) " \/ t C1 v* k, Q0 a9 M) [
r = lstrcpy(s, pIn.pDescription)
' Q8 ` M* m6 s# Q9 P& |: h If r = 0 Then Exit Sub * s9 ]6 ~0 M" b- n% \/ e4 k* b
pType = Left(s, r1) ( z0 V- S9 G$ o# ?0 h& S* {
r1 = InStr(1, pType, ",") * T5 V$ F" g& |/ r8 Q
r = InStr(r1 + 1, pType, ",")
3 i1 E" ~, K6 n m/ h. r$ ] pType = Mid(pType, r1 + 1, r - r1 - 1)
" g) S9 U$ i6 c! uEnd Sub + }7 A- S# I: M7 m/ E
PrinterProperties函数比较简单.你试试吗? |