# 请问 改变预设印表机后, 无法再恢复原预设印表机 ?

Posted by Bill on June 30, 1999 at 18:42:26:

请问 改变预设印表机后,若执行 PaperSize ,Height,Width 等 printer 属性后
,则无法再恢复原预设印表机, 为何? ,如何解决 ?

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

'--------------------------------------------
Private Sub Command1_Click()

Const HWND_BROADCAST = &HFFFF&
Const WM_WININICHANGE = &H1A
Dim S As String
Dim length As Long
Dim hKey As Long
Dim PrinterName As String

Dim OldPrinter As String
Dim X As Printer
Dim z(5) As String
Dim i As Integer
Dim TempPrint As String

i = 0
For Each X In Printers
z(i) = X.DeviceName
i = i + 1
Next

'设定一欲改变之印表机明称
PrinterName = z(1)

'取得原先预设之印表机
OldPrinter = Printer.DeviceName

'修改预设印表机
S = String(80, Chr(0))
length = GetProfileString("devices", PrinterName, "", S, Len(S))
S = Left(S, length)
Call WriteProfileString("windows", "device", PrinterName & "," & S)
Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, &H7FFF&, ByVal "windows")

'取得改变后之印表机名称
TempPrint = Printer.DeviceName

'随意设定下列三项属性
'**************************
Printer.PaperSize = 256
Printer.Height = 1400
Printer.Width = 10000
'************************

'修改回原先预设印表机
PrinterName = OldPrinter

S = String(80, Chr(0))
length = GetProfileString("devices", PrinterName, "", S, Len(S))
S = Left(S, length)
Call WriteProfileString("windows", "device", PrinterName & "," & S)
Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, &H7FFF&, ByVal "windows")

'取得改变后之印表机名称
TempPrint = Printer.DeviceName

End Sub

Follow Ups: