vb中如何获取系统默认打印机的名称?

vb中如何获取系统默认打印机的名称?,第1张

Const HKLM = &H80000002 '定义根键常数

'其他常用根键 Const HKCR = &H80000000 , Const HKCU = &H80000001

Set oReg = GetObject("winmgmts:\\.\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"

oReg.EnumKey HKLM, strKeyPath, arrSubKeys '枚举子项,赋值给数组arrSubKeys

For Each subkey In arrSubKeys

Print subkey

Next

For Each x In Printers

FindPrinter strPrint, x.DeviceName

next

Function FindPrinter(ByRef PrinterName As String, Optional ByVal PrinterDescription As String = "") As Boolean

' 查指定打印机

Dim objRegistry As Object

Dim arrSubKeys As Variant

Dim subkey As Variant

Dim KeyValue As Variant

If Len(PrinterDescription) >0 Then

Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")

objRegistry.EnumValues &H80000001, "Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts", arrSubKeys

For Each subkey In arrSubKeys

If InStr(1, subkey, PrinterDescription) Then

objRegistry.GetStringValue &H80000001, "Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts\", subkey, KeyValue

PrinterName = subkey &" 在 " &Mid(KeyValue, InStr(KeyValue, ",") + 1, InStr(KeyValue, ":,") - InStr(KeyValue, ","))

FindPrinter = True

Exit For

End If

Next

Set objRegistry = Nothing

End If

End Function

如果是自己设计的程序,是可以实现的,调用打印机后,写注册表或是写入一个配置文件来实现判断,如果要设置其他外部程序调用,那得看那个程序本身有没有配置之类的文件或注册表项,如果有,你就写一个程序去修改他的配置文件就可以了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11108914.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存