Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Function GetDeviceName() As String
Dim di As String
def$ = String(128, 0)
di = GetProfileString("WINDOWS", "DEVICE", "", def$, 127)
GetDeviceName = Trim(def$)
GetDeviceName = Left(GetDeviceName, InStr(GetDeviceName, ",") - 1)
End Function
Private Sub Command1_Click()
Dim a As String
MsgBox GetDeviceName
a = GetDeviceName()
Call setDefaultPrinter_To_System("PDF995")
End Sub
Public Sub setDefaultPrinter_To_System(devcName As String)
Dim X As Printer
Dim WshNetwork As Object
Dim k As Long
k = 0
For Each X In Printers
If X.DeviceName = devcName Then
' Set printer as system default.
Set Printer = X
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.setDefaultPrinter (Printers(k).DeviceName)
' Stop looking for a printer.
Exit For
End If
k = k + 1
Next
End Sub
Function GetDeviceName() As String
Dim di As String
def$ = String(128, 0)
di = GetProfileString("WINDOWS", "DEVICE", "", def$, 127)
GetDeviceName = Trim(def$)
GetDeviceName = Left(GetDeviceName, InStr(GetDeviceName, ",") - 1)
End Function
Private Sub Command1_Click()
Dim a As String
MsgBox GetDeviceName
a = GetDeviceName()
Call setDefaultPrinter_To_System("PDF995")
End Sub
Public Sub setDefaultPrinter_To_System(devcName As String)
Dim X As Printer
Dim WshNetwork As Object
Dim k As Long
k = 0
For Each X In Printers
If X.DeviceName = devcName Then
' Set printer as system default.
Set Printer = X
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.setDefaultPrinter (Printers(k).DeviceName)
' Stop looking for a printer.
Exit For
End If
k = k + 1
Next
End Sub
这段VB6代码展示了如何获取和设置系统的默认打印机。通过`GetProfileString`函数获取当前默认打印机,然后使用`setDefaultPrinter_To_System`函数可以将指定打印机设为默认打印机。
9968

被折叠的 条评论
为什么被折叠?



