VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 如何改变 Windows 预设的打印机?
发表评论(0)作者:, 平台:, 阅读:10882, 日期:2000-03-12
如何改变 Windows 预设的打印机?

在 VB 里面, 原本改变预设打印机的方法是:(假设安装有两种打印机(驱动程序))

Set Printer = Printers(0) ' 将预设打印机设定成第一种打印机

Set Printer = Printers(1) ' 将预设打印机设定成第二种打印机

但实际上以上叙述有时候不会成功(原因不详), 为了能够成功地改变预设打印机,以下是调用 Windows API 的方法:(补充说明: 此一解决方案适用于 Windows 95, 至于 NT 设定预设打印机的方法请叁考 98/04/05 的每周小技巧)

1. API 的声明:

Const HWND_BROADCAST = &HFFFF&

Const WM_WININICHANGE = &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

注:如果以上的声明放在「一般模块」底下, 应在 Const 之前加上 Public 保留字, 并且将 Private 保留字去掉。

2. 程序范例:

PrinterName = "您想设定的打印机名称"

Dim S As String, length As Long, hKey As Long

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")

以笔者的所安装的打印机为例, 含有 "HP LaserJet 4L" 及 "HP LaserJet 5L (Traditional)" 两种, 若想将预设打印机设定成 "HP LaserJet 4L", 则须将以上程序的「PrinterName = "您想设定的打印机名称"」改成:

PrinterName = "HP LaserJet 4L"