说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

VB如何改变 Windows 预设的打印机

HTML文档下载 WORD文档下载 PDF文档下载
VB如何改变 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"



备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘