VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 如何改变 Windows 预设的打印机?
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:10517, 日期:2001-09-17
如何改变 Windows 预设的打印机?


作者: 王国荣 

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

             Set Printer = Printers(0) 注释: 将预设打印机设定成第一种打印机
             Set Printer = Printers(1) 注释: 将预设打印机设定成第二种打印机

  但实际上以上叙述有时候不会成功(原因不详), 为了能够成功地改变预设打印机,
以下是呼叫Windows API 的方法:( 此一解决方案适用於 Windows 95)

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

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


  至於改变 NT 预设打印机的方法, 则是改变登录资料库(Registry)打印机的设定,
在登录资料库中纪录
              预设打印机的 Value 是:

  HKEY_CURRENT_USER
  \Software\Microsoft\WindowsNT\CurrentVersion\Windows subkey 的
  Device value