Какая функция АПИ может изменить в системе принтер по умолчанию?
Должно же быть что-то типа SetDefaultPrinter(Имя принтера).
Мне нужно, чтобы изменился принтер для всех программ.
Option Explicit
Private Const HWND_BROADCAST As Long = &HFFFF
Private Const WM_WININICHANGE As Long = &H1A
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
Private Declare Function WriteProfileString Lib "kernel32" _
Alias "WriteProfileStringA" _
(ByVal lpszSection As String, _
ByVal lpszKeyName As String, _
ByVal lpszString As String) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lparam As Any) As Long
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'Private Sub Form_Load()
' ProfileLoadWinIniList List1, "PrinterPorts"
' Command1.Enabled = False
'End Sub
'Private Sub Command1_Click()
' Call SetDefaultPrinterWinNT
'End Sub
'Private Sub List1_Click()
' Command1.Enabled = List1.ListIndex > -1
'End Sub
Public Sub SetDefaultPrinter(ByVal printername As String, _
ByVal DriverName As String, _
ByVal PrinterPort As String, _
Optional fullstr As String = "")
Dim DeviceLine As String
If Len(fullstr) = 0 Then
DeviceLine = printername & "," & DriverName & "," & PrinterPort
Else
DeviceLine = fullstr
End If
'Ñîõðàíèòü èíôîðìàöèþ î íîâîì ïðèíòåðå â ñåêöèè
'[WINDOWS] ôàéëà WIN.INI äëÿ êëþ÷à DEVICE=
Call WriteProfileString("windows", "Device", DeviceLine)
'Çàñòàâèòü âñå ïðèëîæåíèÿ ïåðåçàãðóçèòü INI-ôàéë
Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal "windows")
End Sub
Private Sub GetDriverAndPort(ByVal Buffer As String, _
DriverName As String, PrinterPort As String)
Dim posDriver As Long
Dim posPort As Long
DriverName = ""
PrinterPort = ""
'Èìÿ äðàéâåðà èäåò âíà÷àëå ñòðîêè
posDriver = InStr(Buffer, ",")
If posDriver > 0 Then
'Ïîëó÷åíèå èìåíè äðàéâåðà
DriverName = Left(Buffer, posDriver - 1)
'Èìÿ ïîðòà èäåò ïîñëå çàïÿòîé
posPort = InStr(posDriver + 1, Buffer, ",")
If posPort > 0 Then
'Ïîëó÷åíèå èìåíè ïîðòà
PrinterPort = Mid(Buffer, posDriver + 1, _
posPort - posDriver - 1)
End If
End If
End Sub
'Ñîçäàåì listbox èç äàííûõ win.ini
Public Function ProfileLoadWinIniList(lst As ListBox, _
lpSectionName As String) As Long
Dim success As Long
Dim nsize As Long
Dim lpKeyName As String
Dim ret As String
ret = Space$(8102)
nsize = Len(ret)
success = GetProfileString(lpSectionName, vbNullString, _
"", ret, nsize)
'Âîçðàùàåìàÿ ñòðîêà ÿâëÿåòñÿ ñïèñêîì, ðàçäåëåííûì
'íóëåâûìè ñèìâîëàìè
If success Then
ret = Left$(ret, success)
Do Until ret = ""
lpKeyName = StripNulls(ret)
lst.AddItem lpKeyName
Loop
End If
'âåðíåì ÷èñëî ïðèíòåðîâ (êîë-âî ýëåìåíòîâ)
ProfileLoadWinIniList = lst.ListCount
End Function
Private Function StripNulls(startstr As String) As String
Dim pos As Long
pos = InStr(startstr$, Chr$(0))
If pos Then
StripNulls = Mid$(startstr, 1, pos - 1)
startstr = Mid$(startstr, pos + 1, Len(startstr))
End If
End Function
Public Function SetDefaultPrinterWinNT(printername As String) As String
Dim Buffer As String
Dim DeviceName As String
Dim DriverName As String
Dim PrinterPort As String
Dim r As Long
Dim ret As String, nsize As Long
ret = Space$(8102)
nsize = Len(ret)
Call GetProfileString("windows", "Device", "", ret, nsize)
ret = Mid(ret, 1, InStr(1, ret, Chr(0)) - 1)
Buffer = Space(1024)
'PrinterName = List1.Text
Call GetProfileString("PrinterPorts", _
printername, "", _
Buffer, Len(Buffer))
GetDriverAndPort Buffer, DriverName, PrinterPort
If DriverName <> "" And PrinterPort <> "" Then
SetDefaultPrinter printername, DriverName, PrinterPort
End If
SetDefaultPrinterWinNT = ret
End Function
Сейчас этот форум просматривают: Google-бот и гости: 27