/ x; e8 ~+ ]1 M0 Z- M9 e" [. l
前几天写程序用到几个打印的API函数.把研究成果和大家分享
) q8 O# b" _. M; h4 X3 ^; e建议大家结合MSDN看看
& P; u: m6 y5 p% i& W; v& k9 VPrivate Type PRINTER_INFO_1
% ?4 u! S1 l( g' T7 f0 `' iFlags As Long
pDescription As Long
4 T2 {3 J1 Y' }& K0 F" RpName As Long
/ V0 a1 Z" G7 j$ A) spComment As Long
4 k) S0 a* r1 U+ c1 H. IEnd Type
: _; i9 V0 T( i0 h7 H( n& |Private Declare Function PrinterProperties Lib "winspool.drv" (ByVal hWnd As
Long, ByVal hPrinter As Long) As Long
, e2 N. m, @0 h' @& B; j! J1 VPrivate Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As
Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA"
& a# i" k. I+ ?* F% {1 \(ByVal pPrinterName As String, phPrinter As Long, pDefault As Long) As Long
0 V+ Q4 t) I0 D2 \2 yPrivate Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (
ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As
Long, pcbNeeded As Long) As Long
8 B$ G' [7 u9 n n( bPrivate Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
, n) O- ]- w9 C(ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpSt
$ j/ X- n6 ~$ A7 {1 \ring As Long) As Long
GetPrinter函数返回的数据即我们想要的数据在参数pPrinter而且是一个地址指针指向
0 M) v, C3 e. M. ]# s一个PRINTER_INFO_1结构.结构的pDescription 是指向打印机描述字符串的指针.
在VB只好用CopyMemory,lstrcpy 和 lstrlen得到这个字符串.
9 B1 n; n* `$ k2 F" j$ h; D下面是 例程:得到打印机的类型名
% ]% t/ [& r7 L! ~+ E3 V5 k3 {'pDeviceName是打印机名
'pType 是返回的打印机 Description中的打印机类型.这个串里还有打印机名和驱动程序
- k$ h$ M* v9 D+ M( c" }! iPrivate Sub GetPrint(pDeviceName, pType As String)
Dim pIn As PRINTER_INFO_1
+ i) g3 C0 P2 ~2 F3 ODim s As String * 256
; G5 Z8 |" I0 PDim r As Long, phPrinter As Long, r1 As Long
3 q/ c" c7 v( U9 I5 z0 `2 NOn Error Resume Next
8 m/ }+ |. h( |1 S* I8 Wr = OpenPrinter(pDeviceName & Chr(0), phPrinter, 0&)
1 t, O/ i3 R5 N, q8 K! C1 YIf r = 0 Then Exit Sub
$ h d* K# C2 |/ p) n% F3 X" Pr = GetPrinter(phPrinter, 1, 0, 0, r1)
ReDim pPrinter(r1) As Byte
, u* f9 q" J& v) x+ N* Er = GetPrinter(phPrinter, 1, pPrinter(0), r1, r1)
6 o/ t# o# g9 @& D. r2 q/ vIf r = 0 Then Exit Sub
r = ClosePrinter(phPrinter)
CopyMemory pIn, pPrinter(0), Len(pIn)
t& J. ~0 W+ n0 N 5 `8 |8 `% `4 c4 ^2 V+ C& Er1 = lstrlen(pIn.pDescription)
' {# Q/ \. G- qr = lstrcpy(s, pIn.pDescription)
' @. O8 X2 e# QIf r = 0 Then Exit Sub
pType = Left(s, r1)
' f( Q. }' G' kr1 = InStr(1, pType, ",")
$ P& n& y9 h" f0 @/ q2 ^r = InStr(r1 + 1, pType, ",")
pType = Mid(pType, r1 + 1, r - r1 - 1)
End Sub
PrinterProperties函数比较简单.你试试吗?
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |