版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、如何设置哪些打印机是系统默认打印机 注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的 和由机器翻译的文章,以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。 它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级 机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误 使用所引起的任何直接的,或间接的可能的问题负责。如果您发现了错误并希望帮助我们提高机器翻译技术,请 完成文章末尾的在线调查。 点击这里察看该文章的英文版:26676
2、7 (http:/ 文章编号:266767 最后修改:2006年8月23日 修订 :3.3 本文介绍如何以编程方式设置哪些打印机是系统默认打印机。并非所有应用程序或组件可选择要使用特定打印 机。这使得经常需要更改为系统默认打印机以便应用程序或组件将使用所需打印机。 回到顶端 更多信息 注意:此代码更改为整个系统默认打印机。这样所有应用程序,没有指定打印机,甚至当前运行,的应用程 序使用此相同默认。由于这个原因,可能要拥有您记得以前默认,然后将其打印作业完成时返回代码。 以下代码示例提供一种来确定哪些打印机可,并以指定一个作为系统默认打印机。 回到顶端 分步示例 1. VisualBasic中启
3、动新标准 EXE工程。默认情况下创建 Form 1 。 向项目添加一个新模块并插入以下代码: Public Const HWND_BROADCAST = &HFFFF Public Co nst WM_WININICHANGE = & H1A con sta nts for DEVMODE structure Public Co nst CCHDEVICENAME = 32 Public Con st CCHFORMNAME = 32 2. con sta nts for DesiredAccess member of PRINTER_DEFAULTS Public Const STANDAR
4、D_RIGHTS_REQUIRED = & HFOOOO Public Const PRINTER_ACCESS_ADMINISTER = &H4 Public Const PRINTER_ACCESS_USE = &H8 Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _ PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE) constant that goes into PRINTER_INFO_5 Attributes member to set it as defa
5、ult Public Const PRINTER_A TTRIBUTE_DEFAULT = 4 Constant for OSVERSIONINFO.dwPlatformId Public Const VER_PLATFORM_WIN32_WINDOWS = 1 Public Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String *
6、128 End Type Public Type DEVMODE dmDeviceName As String * CCHDEVICENAME dmSpecVersion As Integer dmDriverVersion As Integer dmSize As Integer dmDriverExtra As Integer dmFields As Long dmOrientation As Integer dmPaperSize As Integer dmPaperLength As Integer dmPaperWidth As Integer dmScale As Integer
7、dmCopies As Integer dmDefaultSource As Integer dmPrintQuality As Integer dmColor As Integer dmDuplex As Integer dmYResolution As Integer dmTTOption As Integer dmCollate As Integer dmFormName As String * CCHFORMNAME dmLogPixels As Integer dmBitsPerPel As Long dmPelsWidth As Long dmPelsHeight As Long
8、dmDisplayFlags As Long dmDisplayFrequency As Long dmICMMethod As Long dmICMIntent As Long dmMediaType As Long dmDitherType As Long dmReserved1 As Long dmReserved2 As Long / Windows 95 only / Windows 95 only / Windows 95 only / Windows 95 only / Windows 95 only / Windows 95 only End Type Public Type
9、PRINTER_INFO_5 pPrinterName As String pPortName As String Attributes As Long DeviceNotSelectedTimeout As Long TransmissionRetryTimeout As Long End Type Public Type PRINTER_DEFAULTS pDatatype As Long pDevMode As Long DesiredAccess As Long End Type Declare Function GetProfileString Lib kernel32 _ Alia
10、s GetProfileStringA _ (ByVal lpAppName As String, _ ByVal lpKeyName As String, _ ByVal lpDefault As String, _ ByVal lpReturnedString As String, _ ByVal nSize As Long) As Long Declare Function WriteProfileString Lib kernel32 _ Alias WriteProfileStringA _ (ByVal lpszSection As String, _ ByVal lpszKeyN
11、ame As String, _ ByVal lpszString As String) As Long Declare Function SendMessage Lib user32 _ Alias SendMessageA _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lparam As String) As Long Declare Function GetVersionExA Lib kernel32 (lpVersionInformation As OSVERSIONINFO) As In
12、teger Public Declare Function OpenPrinter Lib winspool.drv _ Alias OpenPrinterA _ (ByVal pPrinterName As String, _ phPrinter As Long, _ pDefault As PRINTER_DEFAULTS) As Long Public Declare Function SetPrinter Lib winspool.drv _ Alias SetPrinterA _ (ByVal hPrinter As Long, _ ByVal Level As Long, _ pP
13、rinter As Any, _ ByVal Command As Long) As Long Public Declare Function GetPrinter Lib winspool.drv _ Alias GetPrinterA _ (ByVal hPrinter As Long, _ ByVal Level As Long, _ pPrinter As Any, _ ByVal cbBuf As Long, _ pcbNeeded As Long) As Long Public Declare Function lstrcpy Lib kernel32 _ Alias lstrcp
14、yA _ (ByVal lpString1 As String, _ ByVal lpString2 As Any) As Long Public Declare Function ClosePrinter Lib winspool.drv _ (ByVal hPrinter As Long) As Long Public Sub SelectPrinter(NewPrinter As String) Dim Prt As Printer For Each Prt In Printers If Prt.DeviceName = NewPrinter Then Set Printer = Prt
15、 Exit For End If Next End Sub 3. Form 1 上放置一个 CommandButton 和 ListBox 。 4. 将以下代码添加到 Form 1 的常规声明部分: Option Explicit Private Function PtrCtoVbString(Add As Long) As String Dim sTemp As String * 512, x As Long x = lstrcpy(sTemp, Add) If (InStr(1, sTemp, Chr(0) = 0) Then PtrCtoVbString = Else PtrCtoVbS
16、tring = Left(sTemp, InStr(1, sTemp, Chr(0) - 1) End If End Function Private Sub SetDefaultPrinter(ByVal PrinterName As String, _ ByVal DriverName As String, ByVal PrinterPort As String) Dim DeviceLine As String Dim r As Long Dim l As Long DeviceLine = PrinterName & , & DriverName & , & PrinterPort S
17、tore the new printer information in the WINDOWS section of the WIN.INI file for the DEVICE= item r = WriteProfileString(windows, Device, DeviceLine) Cause all applications to reload the INI file: l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, windows) End Sub Private Sub Win95SetDefaultPrinter(
18、) Dim Handle As Long handle to printer Dim PrinterName As String Dim pd As PRINTER_DEFAULTS Dim x As Long Dim need As Long bytes needed Dim pi5 As PRINTER_INFO_5 your PRINTER_INFO structure Dim LastError As Long determine which printer was selected PrinterName = List1.List(List1.ListIndex) none - ex
19、it If PrinterName = Then Exit Sub End If set the PRINTER_DEFAULTS members pd.pDatatype = 0& pd.DesiredAccess = PRINTER_ALL_ACCESS Or pd.DesiredAccess Get a handle to the printer x = OpenPrinter(PrinterName, Handle, pd) failed the open If x = False Then error handler code goes here Exit Sub End If Ma
20、ke an initial call to GetPrinter, requesting Level 5 (PRINTER_INFO_5) information, to determine how many bytes you need x = GetPrinter(Handle, 5, ByVal 0&, 0, need) dont want to check Err.LastDllError here - its supposed to fail with a 122 - ERROR_INSUFFICIENT_BUFFER redim t as large as you need ReD
21、im t(need 4) As Long and call GetPrinter for keepers this time x = GetPrinter(Handle, 5, t(0), need, need) failed the GetPrinter If x = False Then error handler code goes here Exit Sub End If set the members of the pi5 structure for use with SetPrinter. PtrCtoVbString copies the memory pointed at by
22、 the two string pointers contained in the t() array into a Visual Basic string. The other three elements are just DWORDS (long integers) and dont require any conversion pi5.pPrinterName = PtrCtoVbString(t(0) pi5.pPortName = PtrCtoVbString(t(1) pi5.Attributes = t(2) pi5.DeviceNotSelectedTimeout = t(3
23、) pi5.TransmissionRetryTimeout = t(4) this is the critical flag that makes it the default printer pi5.Attributes = PRINTER_ATTRIBUTE_DEFAULT call SetPrinter to set it X = SetPrinter(Handle, 5, pi5, 0) If X = False Then SetPrinter failed MsgBox SetPrinter Failed. Error code: & Err.LastDllError Exit S
24、ub Else If Printer.DeviceName List1.Text Then Make sure Printer object is set to the new printer SelectPrinter (List1.Text) End If End If and close the handle ClosePrinter (Handle) End Sub Private Sub GetDriverAndPort(ByVal Buffer As String, DriverName As _ String, PrinterPort As String) Dim iDriver
25、 As Integer Dim iPort As Integer DriverName = PrinterPort = The driver name is first in the string terminated by a comma iDriver = InStr(Buffer, ,) If iDriver 0 Then Strip out the driver name DriverName = Left(Buffer, iDriver - 1) The port name is the second entry after the driver name separated by
26、commas. iPort = InStr(iDriver + 1, Buffer, ,) If iPort 0 Then Strip out the port name PrinterPort = Mid(Buffer, iDriver + 1, _ iPort - iDriver - 1) End If End If End Sub Private Sub ParseList(lstCtl As Control, ByVal Buffer As String) Dim i As Integer Dim s As String Do i = InStr(Buffer, Chr(0) If i
27、 0 Then s = Left(Buffer, i - 1) If Len(Trim(s) Then lstCtl.AddItem s Buffer = Mid(Buffer, i + 1) Else If Len(Trim(Buffer) Then lstCtl.AddItem Buffer Buffer = End If Loop While i 0 End Sub Private Sub WinNTSetDefaultPrinter() Dim Buffer As String Dim DeviceName As String Dim DriverName As String Dim
28、PrinterPort As String Dim PrinterName As String Dim r As Long If List1.ListIndex -1 Then Get the printer information for the currently selected printer in the list. The information is taken from the WIN.INI file. Buffer = Space(1024) PrinterName = List1.Text r = GetProfileString(PrinterPorts, PrinterName, , _ Buffer, Len(Buffer) Parse the driver name and port name out of the buffer GetDriverAndPort Buffer, DriverName, PrinterPort If DriverName And PrinterPort Then SetDefaultPrinter List1.Text, DriverName, PrinterPort If P
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年定制机床设备供应及服务协议版B版
- 2024年地标性房产项目合作开发合同书样本版B版
- 2024年度分手后商标权转移合同
- 2024年专业给水工程劳务分包协议规范版
- 2024年定制钢构厂房承建协议样式
- 2024年度乙方的秘密资料使用合同2篇
- 2024年度体育场馆辅材供应与施工合同2篇
- 2024年城市建筑渣土运输承包协议模板版B版
- 2024年事业单位聘用员工协议范本版
- 2024年度国际货物买卖合同(2024版)
- 美育浸润行动背景下中小学美育教育模式的构建与优化
- 定制家具正规合同范例
- 第六单元(单元测试)-2024-2025学年统编版语文六年级上册
- 汽油柴油一书一签
- 22066kV变电站电气设计
- 计件工资存在问题原因和完善措施探析
- Metso造纸机纸机通风系统造纸通风抄纸培训
- 应急避难场所设计规范
- 砖混结构房屋施工方案(完整版)
- 第四章_大气扩散浓度估算模式
- 血氧饱和度监测操作流程
评论
0/150
提交评论