资源描述:
《VB中打印机选择》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、.(1)假设你的COMMONDIALOG控件叫CMDiag用如下代码可以使你选择的打印机成为默认打印机"Printer"OnErrorResumeNextCMDiag.PrinterDefault=TrueCMDiag.CancelError=TrueCMDiag.ShowPrinterIfErr.Number<>0ThenExitSub(2)VB 里面, 原本改变预设打印机的方法是:(假设安装有两种打印机(驱动程式)) Set Printer = Printers(0) ' 将预设打印机设定成第一种打印机 S
2、et Printer = Printers(1) ' 将预设打印机设定成第二种打印机 但实际上以上叙述有时候不会成功(原因不详), 为了能够成功地改变预设打印机, 以下是呼叫Windows API 的方法:( 此一解决方案适用於 Windows 95,98) 1. API 的宣告: Const HWND_BROADCAST = &HFFFF& Const WM_WININICHANGE = &H1A Private Declare Function GetProfileString Lib "
3、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 _ "Wri
4、teProfileStringA" (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, _ lP
5、aram 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", "devi
6、ce", PrinterName & "," & S) Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, &H7FFF&, ByVal "windows") .. 至於改变 NT 预设打印机的方法, 则是改变登录资料库(Registry)打印机的设定, 在登录资料库中纪录 预设打印机的 Value 是: HKEY_CURRENT_USER SoftwareMicrosoftWindowsNTCurrentVersionWindows subkey 的D
7、evice value (3)VB中获取指定打印机的各种纸张类型及大小放入一个MSFlexGrid,名称为fgd1,列数为4'Option ExplicitPrivate Const DC_MAXEXTENT = 5Private Const DC_MINEXTENT = 4Private Const DC_PAPERNAMES = 16Private Const DC_PAPERS = 2Private Const DC_PAPERSIZE = 3Private Declare Function DeviceCapa
8、bilities Lib "winspool.drv" Alias "DeviceCapabilitiesA" (ByVal lpDeviceName As String, ByVal lpPort As String, ByVal iIndex As Long, lpOutput As Any, lpDevMode As Any